@s1zen

Как сравнить 2 списка со словорями?

Что мне надо: мне надо получить файл со значениями которого нет в result.json, записать значения в new.json. После этого new.json записать в result.json. Это функция write_to_file

Помогите реализовать, очень надо
Все что я смог сделать.

from fake_useragent import UserAgent
from itertools import groupby
from pprint import pprint
import requests
import asyncio
import aiohttp
import json



headers = {
    "accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
    "accept-language": "ru,en-GB;q=0.9,en;q=0.8,en-US;q=0.7",
    "user-agent": UserAgent().random
}



async def write_to_file(new_dict, page_json):
    with open("new.json", "w", encoding="utf-8") as file:
        json.dump(new_dict, file, indent=4, ensure_ascii=False)
        
    with open("new.json", "r", encoding="utf-8") as file:
        try:
            new_dict = json.load(file)
            print("ok new")
        except json.decoder.JSONDecodeError:
            print("not ok new")
            new_dict = []
        
    with open("result.json", "r+", encoding="utf-8") as file:
        try:
            try:
                result_file = json.load(file)
                print("ok file")

            except json.decoder.JSONDecodeError:
                result_file = []
                print("not ok file")


            all_dict = result_file + new_dict
            # result_file += data_unique_dict
            # pprint(all_dict)
            # pprint(new_dict)
            data_unique_dict = []
            for item in new_dict:
                for i in all_dict:
                    if item["book_link"] == i["book_link"]:
                        continue
                    else:
                        data_unique_dict.append({"book_link": item["book_link"]})
            data_unique_dict = [el for el, _ in groupby(data_unique_dict)]
            
            
            pprint(data_unique_dict)

        except json.decoder.JSONDecodeError as ex:
            print(ex)
            result_file = new_dict
            json.dump(result_file, file, indent=4, ensure_ascii=False)
            

    
async def get_pages():
    kakao_books_dict = []
    new_dict = []
    page_count = int(requests.get("https://api2-page.kakao.com/api/v2/store/today_up/list?category=10&subcategory=0&page=0", headers=headers).json()["total_count"] / 24) + 1
    for page in range(page_count):
        async with aiohttp.ClientSession(headers=headers) as session:
            async with session.get(f"https://api2-page.kakao.com/api/v2/store/today_up/list?category=10&subcategory=0&page={page}") as response:
                page_json = await response.json()
                # await get_data(page_json, new_dict) 
                for i in range(len(page_json["list"])):
                    if page_json["list"][i]["badge"] == "BT03":
                        book_name = page_json["list"][i]["title"]
                        book_link = "https://page.kakao.com/home?seriesId=" + str(page_json["list"][i]["series_id"])
                        # await there_is_new(book_name, book_link, new_dict)

                        # если есть файл
                        try:
                            with open("result.json", "r", encoding="utf-8") as file:    
                                result_json = json.load(file)
                            for item in result_json:
                                if item["book_link"] != book_link:
                                    new_dict.append({"book_link":book_link})
                                else:
                                    continue
                                    
                        # если нет файла
                        except (json.decoder.JSONDecodeError, FileNotFoundError):
                            result_json = []
                            if len(result_json) == 0:
                                new_dict.append({"book_link":book_link})
                            else:
                                for item in result_json:
                                    if item["book_link"] != book_link:
                                        new_dict.append({"book_link":book_link})
                                    else:
                                        new_dict = []
                                        break

                        new_dict = [el for el, _ in groupby(new_dict)]


    await write_to_file(new_dict, page_json)

    
async def main():
    await get_pages()


if __name__ == "__main__":
    try:
        loop = asyncio.new_event_loop()
        asyncio.set_event_loop(loop)
        loop.run_until_complete(main())
    except KeyboardInterrupt as error:
        print("[INFO] Принудительная остановка возмножно вы нажали ctrl + c")
  • Вопрос задан
  • 102 просмотра
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы