#!/usr/bin/env python
# coding=utf-8

import logging
logging.basicConfig(level=logging.INFO)
import sys
reload(sys)
sys.setdefaultencoding('utf-8')

import config
from utils import *
import rutracker_api as api

data = api.get_forum_torrents_status(2092)
#print len(data.keys())
zero_seeds_torrent_ids = [thread for thread in data.keys()
    if len(data[thread]) == 2 and data[thread][1] == 0 and data[thread][0] in (2, 8)]
print len(zero_seeds_torrent_ids)

with open('/tmp/sha1', 'rt') as f:
    already_have_downloads = set(f.read().splitlines())

zero_seeds_torrent_hashes = api.get_tor_hash(zero_seeds_torrent_ids)
zero_seeds_torrent_ids = [torrent_id for torrent_id in zero_seeds_torrent_ids
    if zero_seeds_torrent_hashes[torrent_id].lower() not in already_have_downloads]
print len(zero_seeds_torrent_ids)

for i in range(0, len(zero_seeds_torrent_ids)):
    print 'processing {} out of {}'.format(i, len(zero_seeds_torrent_ids))
    id = zero_seeds_torrent_ids[i]
    with open('/tmp/dl/{}.torrent'.format(id), 'wb') as f:
torrents = config.torrents_source(*config.torrents_source_args)
torrents_meta = []

# first, fetch topic ids by infohash
infohashes = [os.path.splitext(os.path.basename(torrent_fname))[0] for torrent_fname in torrents]
thread_ids = set(api.get_topic_id(infohashes).values())

db_record = {
    'ts': datetime.datetime.now(),
    'forums': {}
}

# fetch torrents' stats from server
for forum_id in config.keeped_forums:
    torrents_stats = api.get_forum_torrents_status(forum_id)
    torrents_stats = { int(id): torrents_stats[id] for id in torrents_stats.keys() }

    # form stat for local torrents
    local_torrents = { thread_id: torrents_stats[thread_id][1]
        for thread_id in thread_ids
        if thread_id in torrents_stats and len(torrents_stats[thread_id]) > 1
    }

    # and remote stat, consisted of dying torrents absent locally
    remote_torrents = { thread_id: torrents_stats[thread_id][1]
        for thread_id in torrents_stats.keys()
        if thread_id not in thread_ids and
            len(torrents_stats[thread_id]) == 2 and
            torrents_stats[thread_id][1] < 3 and
            torrents_stats[thread_id][0] in (2, 8)