-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtrss.py
71 lines (61 loc) · 2.32 KB
/
trss.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
import json
import re
import os
import logging
import feedparser
import transmissionrpc
LOG_LEVELS = {
'NOTSET': logging.NOTSET,
'DEBUG': logging.DEBUG,
'INFO': logging.INFO,
'WARNING': logging.WARNING,
'ERROR': logging.ERROR,
'CRITICAL': logging.CRITICAL
}
CONFIG_FILE = file(os.path.join(os.path.dirname(__file__), 'config.json'))
CONFIG = json.load(CONFIG_FILE)
# Feed configuration: each feed is a dict with expected keys
FEEDS = CONFIG['feeds']
# Setup logger
logger = logging.getLogger(__name__)
logger.setLevel(LOG_LEVELS.get(CONFIG['logging'].get('level', 'INFO'), logging.INFO))
handler = logging.FileHandler(CONFIG['logging'].get('file', '/var/log/trss.log'))
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
handler.setFormatter(formatter)
logger.addHandler(handler)
def process_feed(url, pattern, dl_dir, subdirs, subdir_pattern, subdir_match_index, tc):
try:
feed = feedparser.parse(url)
except Exception:
logger.error('Failed to fetch feed for feed %s', url, exc_info=True)
try:
r = re.compile(pattern)
except Exception:
logger.error('Failed to compile regular expression %s for feed %s', pattern, url, exc_info=True)
entries = [e for e in feed.entries if r.match(e.title)]
logger.debug('Fetched %d matching entries', len(entries))
for e in entries:
if subdirs:
try:
p = re.compile(subdir_pattern)
except Exception:
logger.error('Failed to compile regular expression %s for feed %s', subdir_pattern, url, exc_info=True)
m = p.match(e.title)
path = dl_dir + m.group(subdir_match_index)
else:
path = dl_dir
if not os.path.isdir(path):
try:
os.makedirs(path)
except Exception:
logger.error('Failed to create working directory %s for feed %s', path, url, exc_info=True)
tc.add_torrent(e.link, download_dir=path)
logger.info('Added torrent %s at path %s', e.title, path)
try:
client = transmissionrpc.Client(**CONFIG['transmission'])
except Exception:
logger.error('Failed to connect to daemon', exc_info=True)
for feed in FEEDS:
logger.info('Processing feed %s', feed['url'])
feed.update({'tc' : client})
process_feed(**feed)