Home > Net >  Filter for duplicate requests
Filter for duplicate requests

Time:12-14

When I access the API often, I get the same answers!

These requests are duplicated in telegram messages!

How do I filter out the duplicates?

def start_data():

    response = requests.get(
        url="http://api...."
        )
    
    result = []

    data = response.json()
    items = data.get("data")
    for i in items:
        id = i.get("id")
        name = i.get("name")
        price = i.get("price")
        
        result.append({
            "id":id,
            "name":name,
            "price":price
            })

    with open("input.json", "w") as f:
        json.dump(result, f)

My json file: Key id is unique.

[
    {
        "id": "48683035",
        "name": "Cosmos",
        "price": 24.0
    },
    {
        "id": "48683027",
        "name": "Bar",
        "price": 13.0
    } ]

My telegram messaging function Scheduled once every 5 seconds

async def scheduled(wait_for): 
    while True: 
        await asyncio.sleep(wait_for)
        
        start_data()
        users = user_db.get_user()
        
        with open("input.json") as file:
            data = json.load(file)

        for s in users:
            if len(data) >= 1:
                for item in data:
                    send =f'{hbold("ID: ")}{item.get("id")}\n' \
                        f'{hbold("Name: ")}{item.get("name")}\n' \
                        f'{hbold("Price: ")}{item.get("price")}'
                    await bot.send_message(s[1], send)
            else:
                print("None data...")

CodePudding user response:

You can use set to store existing ids/objects.

all_ids = set()
for i in items:
    id = i.get("id")
    if id in all_ids:
        continue
    (...)
    all_ids.add(id)
  • Related