Files
Hentai_manga_parser/Manga_import.py

53 lines
1.9 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
from pymongo import MongoClient
from pymongo.errors import DuplicateKeyError
import full_img_manga as fim
import Serch_H
def import_from_json():
# Получаем данные
hentai_data = fim.process_hentai_data(hent_data=Serch_H.get_data()) # Это должен быть словарь {название: данные}
num = 29915 + len(hentai_data)
# Проверяем структуру данных
if not isinstance(hentai_data, dict):
print("Ошибка: Данные должны быть в формате словаря {название: данные}")
return
# Обрабатываем данные напрямую без process_hentai_data
for manga_title, manga_data in hentai_data.items():
try:
# Добавляем оригинальное название
manga_data["original_title"] = manga_title
# Проверяем наличие обязательных полей
if "id" not in manga_data:
manga_data["id"] = num
num -= 1
# Добавляем в базу
collection.insert_one(manga_data)
print(f"Успешно добавлено: {manga_title} (ID: {manga_data['id']})")
except DuplicateKeyError:
print(f"Дубликат, пропускаем: {manga_title}")
except Exception as e:
print(f"Ошибка с '{manga_title}': {str(e)}")
if __name__ == "__main__":
# Подключение к MongoDB
client = MongoClient("mongodb://localhost:27017/")
db = client["Manga"]
collection = db["Hentai_Manga"]
# Создаем индексы
collection.create_index("id", unique=True)
collection.create_index("original_title")
# Запускаем импорт
print("Начало импорта...")
import_from_json()
print("Импорт завершен!")