forked from evilhero/harpoon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
harpoonshot.py
executable file
·175 lines (159 loc) · 6.33 KB
/
harpoonshot.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
#!/usr/bin/env python
# This file is part of Harpoon.
#
# Harpoon is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Harpoon is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Harpoon. If not, see <http://www.gnu.org/licenses/>.
import os
import re
import json
import requests
import time
import sys
import ConfigParser
import logging
from logging import handlers
##config
#this is required here to get the log path below
datadir = os.path.dirname(os.path.realpath(__file__))
config = ConfigParser.RawConfigParser()
config.read(os.path.join(datadir, 'conf', 'harpoon.conf'))
log_path = config.get('general', 'logpath')
sonarr_label = config.get('sonarr', 'sonarr_label')
radarr_label = config.get('radarr', 'radarr_label')
mylar_label = config.get('mylar', 'mylar_label')
lidarr_label = config.get('lidarr', 'lidarr_label')
lazylibrarian_label = config.get('lazylibrarian', 'lazylibrarian_label')
try:
sabnzbd_enable = config.getboolean('sabnzbd', 'sab_enable')
except ValueError:
sabnzbd_enable = False
torrentfile_dir = config.get('general', 'torrentfile_dir')
# Setup file logger
logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger()
filename = os.path.join(log_path, 'harpoonshot.log')
file_formatter = logging.Formatter('%(asctime)s - %(levelname)-7s :: %(threadName)s : %(message)s', '%d-%b-%Y %H:%M:%S')
file_handler = handlers.RotatingFileHandler(filename, maxBytes=1000000, backupCount=5)
file_handler.setLevel(logging.DEBUG)
file_handler.setFormatter(file_formatter)
logger.addHandler(file_handler)
filecontent = None
try:
logger.info('Args: %s' % sys.argv)
mode = sys.argv[1]
args = sys.argv[1:]
except IndexError:
try:
if 'mylar_method' in os.environ:
method = os.environ.get('mylar_method')
if method == 'torrent':
inputfile = os.environ.get('mylar_release_hash')
label = mylar_label
filetype = '.hash'
mode = 'mylar'
elif all([method == 'nzb', sabnzbd_enable is True]):
inputfile = os.environ.get('mylar_client_id')
label = mylar_label
filetype = '.hash'
mode = 'mylar'
else:
logger.info('mylar_method is not set to torrent or nzb, so I\'m not sure what to do.')
sys.exit(1)
elif 'sonarr_release_title' in os.environ:
mode = 'sonarr'
inputfile = os.environ.get('sonarr_release_title')
label = sonarr_label
filetype = '.file'
elif 'sonarr_eventtype' in os.environ:
eventtype = os.environ.get('sonarr_eventtype')
logger.info('Called from Sonarr, but as eventtype "%s".' % eventtype)
if eventtype == 'Test':
logger.info('Exiting successfully (Sonarr Test)')
os._exit(0)
else:
logger.info('Failing, unknown type')
os._exit(1)
else:
logger.info('Unable to detect what client called harpoonshot...')
logger.info('Environment: %s' % os.environ)
#ignore non-torrent snatches...
sys.exit(1)
except:
logger.warn('Cannot determine if item came from sonarr / radarr / mylar / lidarr / lazylibrarian ... Unable to harpoon item. ')
os._exit(1)
else:
if mode == 'sonarr':
inputfile = os.environ.get('sonarr_release_title')
label = sonarr_label
filetype = '.file'
elif mode == 'radarr':
inputfile = os.environ.get('radarr_release_title')
if '//' in inputfile:
inputfile = re.sub('-', '//', inputfile).strip()
if '/' in inputfile: # FreeBSD matching
inputfile = inputfile.replace('/', '-')
label = radarr_label
filetype = '.file'
elif mode == 'lidarr':
inputfile = os.environ.get('lidarr_release_title')
if '//' in inputfile:
inputfile = re.sub('-', '//', inputfile).strip()
if '/' in inputfile:
inputfile =inputfile.replace('/', '-')
label = lidarr_label
filetype = '.file'
elif len(args) > 2:
mydict = {}
n = len(args)
while n:
try:
mydict[args[n-2]] = args[n-1]
n -= 2
except IndexError:
break
logger.debug('Dict: %s' % mydict)
if 'DownloadID' in mydict.keys(): # LazyLibrarian book or audiobook
# mode = 'lazylibrarian'
mode = ''
inputfile = mydict['DownloadID']
if len(inputfile) > 20:
inputfile = inputfile.upper()
label = lazylibrarian_label
filetype = 'hash'
filecontent = mydict
else:
logger.debug('Not enough Arguments: %s' % args)
logger.warn('Cannot determine if item came from sonarr / radarr / mylar / lidarr / lazylibrarian ... Unable to harpoon item. ')
sys.exit(1)
else:
logger.warn('Cannot determine if item came from sonarr / radarr / mylar / lidarr / lazylibrarian ... Unable to harpoon item. ')
sys.exit(1)
logger.info("Name to use: %s" % inputfile)
path = os.path.join(torrentfile_dir, label)
if os.path.exists(path):
filepath = os.path.join(path, inputfile + '.' + mode + filetype)
#create empty file with the given filename and update the mtime
try:
with open(filepath, 'w') as outfile:
os.utime(filepath, None)
if any([mode == 'sonarr', mode == 'radarr', mode == 'mylar', mode == 'lidarr']):
outfile.write(json.dumps(dict(os.environ), indent=4))
elif filecontent:
outfile.write(json.dumps(filecontent))
except e as Exception:
logger.info("Exception: %s" % e)
sys.exit(1)
else:
logger.warn('Path "%s" does not exists. Please create.' % path)
sys.exit(1)
logger.info('Successfully created .file to allow for harpooning.')