Cleansy/store_stickers.py
2024-11-12 12:16:28 +03:30

45 lines
1.4 KiB
Python

import os
import json
from src.data.database import StickerDatabase
def load_json(filename):
with open(filename, 'r') as f:
return json.load(f)
def get_sticker_packs(index):
sticker_pack_list = []
for i in index['packs']:
sticker_pack_list.append(i)
return sticker_pack_list
def get_sticker_ids(stickerpack):
stickers_list = []
for i in stickerpack['stickers']:
url = i['url']
sticker_id = url.split('/')[-1]
stickers_list.append(sticker_id)
return stickers_list
if __name__ == '__main__':
username = input("Enter username: ")
packs_directory = input("Enter directory containing index.json file: ")
index_file = os.path.join(packs_directory, 'index.json')
sticker_db = StickerDatabase()
index_data = load_json(index_file)
sticker_packs = get_sticker_packs(index_data)
for pack in sticker_packs:
print(f"Sticker pack: {pack}")
sticker_pack_file = os.path.join(packs_directory, pack)
sticker_pack_data = load_json(sticker_pack_file)
sticker_pack_title = sticker_pack_data['net.maunium.telegram.pack']['short_name']
sticker_ids = get_sticker_ids(sticker_pack_data)
print(f"Sticker pack title: {sticker_pack_title}")
for sticker in sticker_ids:
sticker_db.insert_sticker(username, sticker_pack_title, sticker)
print(f"Added sticker {sticker} to the database.")