-
Notifications
You must be signed in to change notification settings - Fork 1
/
torrent-crawl.py
72 lines (55 loc) · 2.42 KB
/
torrent-crawl.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import sys
import os
import ConfigParser
import json
import argparse
from whatapi import WhatAPI
def main():
parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter, prog='sowsbetter')
parser.add_argument('-s', '--snatches', type=int, help='minimum amount of snatches required before transcoding',
default=5)
parser.add_argument('-b', '--better', type=int, help='better transcode search type',
default=3)
parser.add_argument('-c', '--count', type=int, help='backlog max size', default=5)
parser.add_argument('--config', help='the location of the configuration file',
default=os.path.expanduser('~/.sowsbetter/config'))
parser.add_argument('--cache', help='the location of the cache',
default=os.path.expanduser('~/.sowsbetter/cache-crawl'))
args = parser.parse_args()
config = ConfigParser.SafeConfigParser()
try:
open(args.config)
config.read(args.config)
except:
print("Please run sowsbetter once")
sys.exit(2)
username = config.get('whatcd', 'username')
password = config.get('whatcd', 'password')
torrent_dir = os.path.expanduser(config.get('whatcd', 'torrent_dir'))
print("Logging in to Bemaniso.ws ...")
api = WhatAPI(username, password)
try:
cache = json.load(open(args.cache))
except:
cache = []
json.dump(cache, open(args.cache, 'wb'))
while len(cache) < args.count:
print("Refreshing better.php and finding {0} candidates".format(args.count - len(cache)))
for torrent in api.get_better(args.better):
if len(cache) >= args.count:
break
print("Testing #{0}".format(torrent['id']))
info = api.get_torrent_info(torrent['id'])
if info['snatched'] < args.snatches:
continue
print("Fetching #{0} with {1} snatches".format(torrent['id'], info['snatched']))
with open(os.path.join(torrent_dir, '%i.torrent' % torrent['id']), 'wb') as f:
f.write(api.get_torrent(torrent['id']))
torrent['hash'] = info['infoHash'].upper()
torrent['done'] = False
cache = json.load(open(args.cache))
cache.append(torrent)
json.dump(cache, open(args.cache, 'wb'))
print("Nothing left to do")
if __name__ == '__main__':
main()