from includes import api, helpers

post_ids = helpers.read_json_file('ids')
posts = helpers.read_json_file('posts')
errors = helpers.read_json_file('errors')

pages = api.get_pages()

for index, page_url in enumerate(pages):

    print('{} of {}'.format(index + 1, len(pages)))

    for post in api.get_posts(page_url):
        if post['href'] == 'https://moviebaz.tv/%d9%85%d9%88%d9%88%db%8c-%d8%a8%d8%a7%d8%b2/':
            continue
            
        print('getting post info from: {}'.format(post['href']))

        post_info = api.get_post(post['href'])

        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(post['href'], 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('posts', posts)

        helpers.save_json_file('ids', post_ids)

print('process finished.')