-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathghetto.py
353 lines (289 loc) · 10.5 KB
/
ghetto.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
#/usr/bin/env python
# -*- coding: utf-8 -*-
import re
import os
import sys
import shutil
import requests
import json
from argparse import ArgumentParser, HelpFormatter
from subprocess import call
from lxml import etree
from crontab import CronTab
VERSION="0.1.0"
DESCRIPTION = """
A tool for (automatized) torrent files downloading from RSS feeds to use
with torrent clients which don't have built-in RSS functionality.
Supports multiple configurations, update crontab by itself to grab feeds
automatically.
"""
CONFIG_ROOT = os.path.join(os.path.expanduser('~'), '.config/ghetto')
DEFAULT_TORRRENT_DIR = os.path.join(os.path.expanduser('~'), 'torrent')
DEFAULT_UPDATE_INTERVAL = 10
def confirm(text, default=True):
"""
Console confirmation dialog based on raw_input.
"""
if default:
legend = "[y]/n"
else:
legend = "y/[n]"
res = ""
while (res != "y") and (res != "n"):
res = raw_input(text + " ({}): ".format(legend)).lower()
if not res and default:
res = "y"
elif not res and not default:
res = "n"
if res[0] == "y":
return True
else:
return False
def ensure_dir(d):
"""
Check does directory exist, and create an empty one if not.
"""
if not os.path.exists(d):
os.makedirs(d)
def read_file(fname):
"""
Read file, convert wildcards into regular expressions, skip empty lines
and comments.
"""
res = []
try:
with open(fname, 'r') as f:
for line in f:
line = line.rstrip('\n').rstrip('\r')
if line and (line[0] != '#'):
regexline = ".*" + re.sub("\*", ".*", line) + ".*"
res.append(regexline.lower())
except IOError:
pass
return res
def drop_it(title, filters, blacklist):
"""
The found torrents should be in filters list and shouldn't be in blacklist.
"""
title = title.lower()
matched = False
for f in filters:
if re.match(f, title):
matched = True
if not matched:
return True
for b in blacklist:
if re.match(b, title):
return True
return False
def do_list():
"""
CLI action "list configurations".
"""
dirs = os.walk(CONFIG_ROOT).next()[1]
if dirs:
print "List of available configurations:\n"
for d in dirs:
print " * {}".format(d)
else:
print "No configurations available."
def do_create(config, config_dir):
"""
CLI action "create new configuration".
"""
if os.path.exists(config_dir):
print "Configuration '{}' already exists.".format(config)
exit(1)
os.makedirs(config_dir)
print "Configuration directory created."
url = raw_input("RSS URL for processing []: ")
torrent_dir = raw_input("Output directory for found .torrent files [{}]: "\
.format(DEFAULT_TORRRENT_DIR)) or DEFAULT_TORRRENT_DIR
update_interval = raw_input("Update interval (mins) [{}]: "\
.format(DEFAULT_UPDATE_INTERVAL)) or DEFAULT_UPDATE_INTERVAL
editor = os.environ["EDITOR"]
config_filter = os.path.join(config_dir, 'filter')
if confirm("Do you want to create filters list?", False):
call([editor, config_filter])
print "Filter configuration has been saved."
config_blacklist = os.path.join(config_dir, 'blacklist')
if confirm("Do you want to create blacklist?", False):
call([editor, config_filter])
print "Blacklist configuration has been saved."
config_file = os.path.join(config_dir, 'config')
config_data = json.dumps({
"url": url,
"torrent_dir": torrent_dir,
"update_interval": update_interval
}, sort_keys=True, indent=4, separators=(',', ': '))
with open(config_file, 'w') as f:
f.write(config_data)
ct = CronTab(user=True)
cmd = "{} {} -e {}".format(sys.executable,
os.path.abspath(__file__),
config)
job = ct.new(command=cmd)
job.minute.every(update_interval)
job.enable()
ct.write()
print "Crontab updated."
print "Config '{}' has been saved.".format(config)
def do_update(config, config_dir):
"""
CLI action "update new configuration".
"""
if not os.path.exists(config_dir):
print "Configuration '{}' does not exist.".format(config)
exit(1)
config_file = os.path.join(config_dir, 'config')
with open(config_file, 'r') as f:
old_config_data = json.load(f)
old_url = old_config_data['url']
old_torrent_dir = old_config_data['torrent_dir']
old_update_interval = old_config_data['update_interval']
url = raw_input("RSS URL for processing [{}]: "\
.format(old_url)) or old_url
torrent_dir = raw_input("Output directory for found .torrent files [{}]: "\
.format(old_torrent_dir)) or old_torrent_dir
update_interval = raw_input("Update interval (mins) [{}]: "\
.format(old_update_interval)) or old_update_interval
editor = os.environ["EDITOR"]
config_filter = os.path.join(config_dir, 'filter')
if confirm("Do you want to edit filters list?", False):
call([editor, config_filter])
print "Filter configuration has been saved."
config_blacklist = os.path.join(config_dir, 'blacklist')
if confirm("Do you want to edit blacklist?", False):
call([editor, config_filter])
print "Blacklist configuration has been saved."
config_data = json.dumps({
"url": url,
"torrent_dir": torrent_dir,
"update_interval": update_interval
}, sort_keys=True, indent=4, separators=(',', ': '))
with open(config_file, 'w') as f:
f.write(config_data)
ct = CronTab(user=True)
for job in ct:
if re.match('.*ghetto.*\-e\s{}'.format(config), job.command):
ct.remove(job)
cmd = "{} {} -e {}".format(sys.executable,
os.path.abspath(__file__),
config)
new_job = ct.new(command=cmd)
new_job.minute.every(update_interval)
new_job.enable()
ct.write()
print "Crontab updated."
print "Configuration '{}' has been updated.".format(config)
def do_remove(config, config_dir):
"""
CLI action "remove configuration".
"""
if not os.path.exists(config_dir):
print "Configuration '{}' does not exist.".format(config)
exit(1)
if confirm("Confirm removal of the configuration '{}'".format(config)):
shutil.rmtree(config_dir)
print "Configuration '{}' has been removed.".format(config)
else:
print "Removal cancelled."
def do_exec(config, config_dir):
"""
CLI action "process the feed from specified configuration".
"""
if not os.path.exists(config_dir):
print "Configuration '{}' does not exist.".format(config)
exit(1)
print "The parser for '{}' config has been initialized.".format(config)
config_file = os.path.join(config_dir, 'config')
with open(config_file, 'r') as f:
config_data = json.load(f)
url = config_data['url']
torrent_dir = config_data['torrent_dir']
ensure_dir(torrent_dir)
filters_file = os.path.join(config_dir, 'filter')
filters = read_file(filters_file)
blacklist_file = os.path.join(config_dir, 'blacklist')
blacklist = read_file(blacklist_file)
print "Fetching URL {}".format(url)
r = requests.get(url)
if r.status_code != 200:
print "Failed to fetch RSS feed."
xml = r.text.encode('utf-8')
parser = etree.XMLParser(ns_clean=True, recover=True, encoding='utf-8')
tree = etree.fromstring(xml, parser)
items = tree.xpath('//item')
downloaded = 0
for e in items:
e_title = e.xpath('title')[0].text
e_link = e.xpath('link')[0].text
if not drop_it(e_title, filters, blacklist):
downloaded += 1
target_file = os.path.join(torrent_dir, e_title + '.torrent')
r = requests.get(e_link, stream=True)
with open(target_file, 'wb') as f:
for chunk in r.iter_content(4096):
f.write(chunk)
print "Items found: {}, items downloaded: {}."\
.format(len(items), downloaded)
def do_filter(config, config_dir):
"""
CLI action "run editor for filters list".
"""
if not os.path.exists(config_dir):
print "Configuration '{}' does not exist.".format(config)
exit(1)
editor = os.environ["EDITOR"]
config_filter = os.path.join(config_dir, 'filter')
call([editor, config_filter])
print "Filter configuration has been updated."
def do_blacklist(config, config_dir):
"""
CLI action "run editor for blacklist".
"""
if not os.path.exists(config_dir):
print "Configuration '{}' does not exist.".format(config)
exit(1)
editor = os.environ["EDITOR"]
config_blacklist = os.path.join(config_dir, 'blacklist')
call([editor, config_blacklist])
print "Blacklist configuration has been updated."
def action(act, config):
"""
CLI action preprocessor
"""
if not config:
pass
elif act is "list":
do_list()
else:
config_dir = os.path.join(CONFIG_ROOT, config)
globals()["do_" + act](config, config_dir)
def main():
ensure_dir(CONFIG_ROOT)
parser = ArgumentParser(version="%(prog)s {}".format(VERSION),
description=DESCRIPTION,
formatter_class=lambda prog:
HelpFormatter(prog,max_help_position=50))
parser.add_argument("-l", "--list", action="store_true",
help="list of available configs")
parser.add_argument("-c", "--create", metavar="CONFIG",
help="create a new config")
parser.add_argument("-u", "--update", metavar="CONFIG",
help="update an existent config")
parser.add_argument("-r", "--remove", metavar="CONFIG",
help="remove the config")
parser.add_argument("-e", "--exec", metavar="CONFIG",
help="run parser with the specified config")
parser.add_argument("-f", "--filter", metavar="CONFIG",
help="edit filters list for the specified config")
parser.add_argument("-b", "--blacklist", metavar="CONFIG",
help="edit blacklist for the specified config")
if len(sys.argv) == 1:
parser.print_help()
exit(0)
args = vars(parser.parse_args())
[globals()["action"](key, arg) for key, arg in args.iteritems()]
if __name__ == "__main__":
main()