from includes import api, helpers

post_ids = helpers.read_json_file('ids')
posts = helpers.read_json_file('posts')
errors = helpers.read_json_file('errors')

links = [
    'https://mv.appversion6.workers.dev/%d8%af%d8%a7%d9%86%d9%84%d9%88%d8%af-%d8%b3%d8%b1%db%8c%d8%a7%d9%84-sahipsizler/'
]

for link in links:

    post_info = api.get_post(link)

    if 'post_id' not in post_info:
        continue

    if not post_info['imdb_id']:
        print('imdb id not found')

        if errors.get(post_info['post_id']):
            post_info = api.get_post_imdb(link, errors[post_info['post_id']])

            if 'post_id' not in post_info:
                continue

            del errors[post_info['post_id']]
            helpers.save_json_file('errors', errors)
        else:
            errors[post_info['post_id']] = ''
            helpers.save_json_file('errors', errors)
            continue

    if not post_info['download_links']:
        print('no download links found')

    post_ids[post_info['post_id']] = post_info['imdb_id']

    if post_info['once']:
        posts[post_info['post_id']] = post_info
    else:
        posts[post_info['post_id']]['download_links'] = post_info['download_links']
        posts[post_info['post_id']]['trailer'] = post_info['trailer']

    helpers.save_json_file('ids', post_ids)

    helpers.save_json_file('posts', posts)

print('process finished.')
