-
Notifications
You must be signed in to change notification settings - Fork 0
/
rsstorrent.py
executable file
·614 lines (529 loc) · 18.8 KB
/
rsstorrent.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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
#! /usr/bin/python3
# -*- coding: utf-8 -*-
"""RssTorrent.py: Monitors an RSS-feed and automatically
downloads torrents to a specified folder."""
__author__ = "Peter Asplund"
__copyright__ = "Copyleft 2017"
__credits__ = ["ikkemaniac"]
__license__ = "GPL"
__version__ = "0.8"
__maintainer__ = "Peter Asplund"
__email__ = "peterasplund@gentoo.se"
__status__ = "Stable"
# Standard includes
import os
import signal
import urllib.request
import urllib.parse
import urllib.error
import http.cookiejar
import time
import re
import logging
import configparser
import argparse
# Extra packages
import feedparser
import daemon
# import lockfile
RUNNING = True
class Child:
""" Child process """
child_id = ""
pid = ""
is_alive = False
def __init__(self, child_id, pid):
logging.debug("Created child number:\t" + str(child_id))
logging.debug("\twith pid:\t\t" + str(pid))
self.child_id = child_id
self.pid = pid
self.is_alive = True
def print_debug(self):
""" Dump all local variables """
logging.debug("Child:" + self.child_id)
logging.debug("Pid:" + self.pid)
logging.debug("Is Alive: " + self.is_alive)
class Environment:
""" Keeps track of all files and directories """
# File names
cache_file = "cache"
config_file = "rsstorrent.conf"
# Directory names
home_dir = ""
config_dir = "/.rsstorrent/"
download_dir = ""
# Full paths
cache_dir_path = ""
cache_file_path = ""
config_file_path = ""
config_dir_path = ""
def __init__(self):
self.home_dir = os.path.expanduser("~")
self.cache_dir_path = os.path.join(self.home_dir + self.config_dir)
self.config_dir_path = os.path.join(self.home_dir + self.config_dir)
self.cache_file_path = os.path.join(self.config_dir_path + self.cache_file)
self.config_file_path = os.path.join(self.config_dir_path + self.config_file)
self.download_dir = os.path.join(self.home_dir + "/Download")
if not os.path.exists(self.config_dir_path):
os.mkdir(self.config_dir_path, 0o755)
file_handle = open(self.cache_file_path, "w")
file_handle.close()
def print_debug(self):
""" Dump all local variables """
logging.debug("Home dir: " + self.home_dir)
logging.debug("Cache dir path: " + self.cache_dir_path)
logging.debug("Cache file: " + self.cache_file)
logging.debug("Cache file path: " + self.cache_file_path)
logging.debug("Download dir path: " + self.download_dir)
class Site:
""" Represents a monitored site """
feed_url = ""
login_url = ""
time_interval = 5.0
keys = []
regexp_keys = []
username = ""
password = ""
def __init__(self):
return
def print_debug(self):
""" Dump all local variables """
logging.debug("Site: ")
logging.debug("Feed url: " + self.feed_url)
logging.debug("Login url: " + self.login_url)
logging.debug("Keys: " + str(self.keys))
logging.debug("Interval (in seconds): " + str(self.time_interval))
logging.debug("---------------------")
def create_config_file(cfg_file):
""" Generate an empty config file """
with open(cfg_file, "w+") as cfg_file_handle:
# Split the file by lines to get rid of whitespace
lines = [
"[General]\n",
"# Directory which to download torrents to\n",
"download_dir = /home/username/Downloads/\n",
"\n",
"[Site1]\n",
"# Interval between checks in minutes\n",
"interval = 30\n",
"\n",
"# URL to rss feed\n",
"rss_url = http://www.urltosite.com\n",
"\n",
"# URL to site login page/script\n",
"login_url = http://www.urltosite.com/takelogin.php\n",
"\n",
"# Search keys for the parsing\n",
"keys = keys*to*search*for separated*by spaces\n",
"\n",
"# Username and Password to torrent site\n",
"username = username\n",
"password = password\n",
]
for line in lines:
cfg_file_handle.writelines(line)
def read_config_file(cfg_file, sites, env):
""" Open and parse the config file, save the words in a list. """
# Open config file
logging.info("Reading configuration file: " + cfg_file)
config = configparser.ConfigParser()
config.read(cfg_file)
sections = config.sections()
for section in sections:
site = Site()
if section == "General":
# Save name of directory to download files to
env.download_dir = config.get(section, "download_dir")
else:
# Save url to check
site.login_url = config.get(section, "login_url")
# Save url to check
site.feed_url = config.get(section, "rss_url")
# Save time interval (in seconds) for checking feeds
site.time_interval = config.getfloat(section, "interval") * 60.0
# Save list of words to look for
keys_str = str(config.get(section, "keys"))
site.keys = keys_str.split()
# Save username to site
site.username = config.get(section, "username")
# Save password to site
site.password = config.get(section, "password")
# Add to array of sites
sites.append(site)
# safety check
if len(sites) < 1:
logging.critical("Can't read config file")
exit(-1)
def site_login(site):
""" Log in to url and save cookie """
cookie_jar = http.cookiejar.CookieJar()
# build opener with HTTPCookieProcessor
opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cookie_jar))
opener.addheaders.append(
(
"User-agent",
(
"Mozilla/5.0 (X11; Linux x86_64; rv:2.0.1)"
"Gecko/20110524 Firefox/4.0.1"
),
)
)
urllib.request.install_opener(opener)
# assuming the site expects 'user' and 'pass' as query params
login_query = urllib.parse.urlencode(
{"username": site.username, "password": site.password, "login": "Log in!"}
).encode()
# perform login with params
try:
file_handle = opener.open(site.login_url, login_query)
file_handle.close()
except urllib.error.HTTPError as exception:
logging.error("HTTP Error: " + exception.code + " Site:" + site.feed_url)
except urllib.error.URLError as exception:
logging.error("URL Error: " + exception.reason + " Site:" + site.feed_url)
def update_list_from_feed(url, regexp_keys):
""" Update the feed data from its url. """
found_items = {}
# Get feed
feed = feedparser.parse(url)
if "title" not in feed.feed:
logging.error("Error connecting to feed")
return found_items
else:
logging.info("Updating Feed: " + feed["feed"]["title"])
# Loop through that list
for key in regexp_keys:
for item in feed["items"]:
if key.search(item["title"]):
logging.debug("Found match: " + item["title"])
logging.debug("\t" + item["link"])
found_items[item["title"]] = item["link"]
logging.debug("Added: " + found_items[item["title"]])
logging.debug("Updated Feed: " + feed["feed"]["title"])
return found_items
def process_download_list(cache, download_dir, input_list, options):
""" Process the list of waiting downloads. """
# Open cache to check if file has been downloaded
if not os.path.exists(cache):
if not options.cache_ignore:
logging.error("Can't find cache directory")
return
else:
logging.debug("No cache file was found, but caching was ignored anyway")
# Open cache file and start downloading
with open(cache, "a+") as cache_file_handle:
# Split the file by lines to get rid of whitespace
cached_files = cache_file_handle.read().splitlines()
for title in input_list:
filename = title + ".torrent"
http_url = input_list[title]
logging.debug("Processing: " + http_url)
logging.debug("Filename resolved to: " + filename)
if len(filename) < 1:
logging.critical(
"I was not able to find you a filename!\
The file cannot be saved!"
)
continue
if (filename in cached_files) and not options.cache_ignore:
logging.debug("File already downloaded: " + filename)
continue
if options.no_downloads:
continue
logging.info("Start downloading: " + filename)
try:
request = urllib.request.urlopen(http_url)
except urllib.error.HTTPError as exception:
msg = "HTTP Error: " + exception.code + " Line:" + http_url
logging.info(msg)
if request.geturl() != http_url:
logging.info("URL Redirect - Not allowed to download")
continue
with open(os.path.join(download_dir, filename), "w") as local_file:
local_file.write(request.read())
logging.info("Download successful: " + filename)
# Cache the downloaded file so it doesn't get downloaded again
cache_file_handle.writelines(filename + "\n")
def convert_keys_to_regexps(sites):
""" Process the list of keys and convert
to compiled regular expressions. """
for site in sites:
for key in site.keys:
site.regexp_keys.append(re.compile(key, re.IGNORECASE))
def setup_logging(env, options):
""" Setup logging to file or tty. """
log_file = ""
# Logging format for logfile and console messages
formatting = "%(asctime)s (%(process)d) %(levelname)s: %(message)s"
# set log filepath
if options.log_file:
log_file = options.log_file
else:
log_file = os.path.join(env.config_dir_path + "rsstorrent.log")
# IMPORTANT!
# It is important to define the basic file
# logging before the console logging
if options.debug:
logging.basicConfig(filename=log_file, format=formatting, level=logging.DEBUG)
elif options.verbose:
logging.basicConfig(filename=log_file, format=formatting, level=logging.INFO)
else:
logging.basicConfig(
filename=log_file, format=formatting, level=logging.CRITICAL
)
# Always print messages to the console
# In normal operating mode only CRITICAL messages will be displayed
console = logging.StreamHandler()
if options.debug:
# define a Handler which writes CRITICAL messages to the sys.stderr
console.setLevel(logging.DEBUG)
elif options.verbose:
console.setLevel(logging.INFO)
else:
console.setLevel(logging.CRITICAL)
console.setFormatter(logging.Formatter(formatting))
logging.getLogger("").addHandler(console)
return log_file
def cleanup_program():
""" Set RUNNING to false so program loop exits. """
global RUNNING
RUNNING = False
logging.info("RUNNING set to False")
def parse_cmd_arguments():
""" Parse command line arguments """
# Parse command line commands
parser = argparse.ArgumentParser()
parser.add_argument(
"-v",
"--verbose",
action="store_true",
dest="verbose",
help="Verbose logging",
default=False,
)
parser.add_argument(
"-d",
"--debug",
action="store_true",
dest="debug",
help="Print debug information to console",
default=False,
)
parser.add_argument(
"-1",
"--run-once",
action="store_true",
dest="run_once",
help="Run once, then exit",
default=False,
)
parser.add_argument(
"-D",
"--daemon",
action="store_true",
dest="daemon",
help="Run as daemon",
default=False,
)
parser.add_argument(
"-x",
"--stop",
action="store_true",
dest="stop",
help="Stop running daemon (does not work currently)",
default=False,
)
parser.add_argument(
"-l", "--logfile", dest="log_file", help="Write log to FILE", metavar="FILE"
)
parser.add_argument(
"-p",
"--pidfile",
dest="pid_file",
help="Set pidfile to FILE",
metavar="FILE",
default="/var/run/rsstorrent/rsstorrent.pid",
)
parser.add_argument(
"--cc",
"--cache-clear",
action="store_true",
dest="cache_clear",
help="Clear the cache file",
default=False,
)
parser.add_argument(
"--ci",
"--cache-ignore",
action="store_true",
dest="cache_ignore",
help="Ignore the cache and download all files again",
default=False,
)
parser.add_argument(
"--nd",
"--no-downloads",
action="store_true",
dest="no_downloads",
help="Don't actually download the files",
default=False,
)
options = parser.parse_args()
return options
def check_output_files(env, options):
""" Check sanity of output directory and handle cache cleans """
if options.cache_clear:
# clear cache file
open(env.cache_file_path, "w").close()
logging.info("Cleared cache")
exit(0)
if not os.path.exists(env.download_dir):
os.mkdir(env.download_dir, 0o755)
def do_main_program():
""" Main function. """
options = parse_cmd_arguments()
env = Environment()
log_file_path = setup_logging(env, options)
logging.info("Starting rsstorrent...")
# Read config file, if it can't find it, create it
if not os.path.exists(env.config_file_path):
create_config_file(env.config_dir_path + env.config_file)
logging.warning("There was no config file found, I just created one.")
logging.critical(
"Please check "
+ env.config_dir_path
+ env.config_file
+ " before restarting!"
)
exit(-1)
sites = []
read_config_file(env.config_file_path, sites, env)
check_output_files(env, options)
convert_keys_to_regexps(sites)
for site in sites:
site_login(site)
# Print verbose/debug output if enabled
if options.verbose:
env.print_debug()
logging.info("Ignore cache: " + str(bool(options.cache_ignore)))
for site in sites:
site.print_debug()
# Start the program (either in or without daemon mode)
if options.daemon:
context = initiate_daemon(options, env, log_file_path)
logging.debug("Entering daemon context")
with context:
logging.debug("Entered daemon context")
main_loop(env, sites, options)
logging.debug("Exited daemon context")
else:
main_loop(env, sites, options)
logging.info("Stopping rsstorrent...")
logging.info("Exting.")
def initiate_daemon(options, env, log_file_path):
""" Set up daemon context and return it """
logging.debug("Setting up daemon with pid file: " + str(options.pid_file))
# Set up some Daemon stuff
context = daemon.DaemonContext(umask=0o002, pidfile=options.pid_file)
context.signal_map = {
signal.SIGTERM: cleanup_program,
signal.SIGUSR1: cleanup_program,
signal.SIGHUP: cleanup_program,
}
if not context:
logging.critical("Unable to create daemon context. Exiting")
return -1
elif not context.pidfile:
logging.critical("Unable to create daemon context. Exiting")
return -1
if options.stop:
logging.info("Caught stop signal")
logging.info("Checking pid " + str(context.pidfile))
logging.info("Context started: " + str(context.is_open))
if context.is_open:
context.close()
else:
logging.info("No context with that pid open")
exit(0)
# Check if the daemon is already running
# or at least if it has left a pid file behind
try:
open(options.pid_file + ".lock", "r")
logging.info("pid-file exists, exiting")
exit(-1)
except IOError:
pass
# Open all important files and list them
cache_file_handle = open(env.cache_file_path, "a+")
config_file_handle = open(env.config_file_path, "a+")
if log_file_path:
log_file_handle = logging.root.handlers[0].stream.fileno()
logging.debug("Adding logging handle to files_preserve: " + log_file_path)
context.files_preserve = [
cache_file_handle,
config_file_handle,
log_file_handle,
]
else:
context.files_preserve = [cache_file_handle, config_file_handle]
return context
def child_process_loop(env, site, options):
""" Main process loop for each child """
while RUNNING:
# in child process
download_list = update_list_from_feed(site.feed_url, site.regexp_keys)
if len(download_list):
logging.debug(
"Start downloading, I found " + str(len(download_list)) + " items."
)
process_download_list(
env.cache_file_path, env.download_dir, download_list, options
)
time.sleep(site.time_interval)
logging.debug("Exiting child process")
exit(0)
def main_loop(env, sites, options):
""" Main program loop """
global RUNNING
RUNNING = True
children = []
num_children = 0
# Main loop
for site in sites:
num_children += 1
# Fork for each site
logging.debug("Forking process")
child_pid = os.fork()
if child_pid:
# still in the parent process
logging.debug("Created fork with pid " + str(child_pid))
logging.debug("Create child process for: " + site.feed_url)
# Save child information to be able to check on it later
ctmp = Child(num_children, child_pid)
children.append(ctmp)
else:
child_process_loop(env, site, options)
if options.run_once:
RUNNING = False
while RUNNING:
logging.debug("Looking into children...")
for child in children:
pid = os.waitpid(child.pid, os.WNOHANG)[1]
if pid < 0:
child.is_alive = False
break
time.sleep(60)
# Kill children and exit
for child in children:
terminate_process(child.pid)
logging.info("Exiting")
exit(0)
def terminate_process(pid):
""" Terminate process with pid """
os.kill(pid, signal.SIGKILL)
try:
if __name__ == "__main__":
do_main_program()
# catch keyboard exception
except KeyboardInterrupt:
logging.critical("Keyboard Interrupted!")