-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsingular.py
executable file
·238 lines (187 loc) · 6.84 KB
/
singular.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
#!/usr/bin/env python3
"""
System for creating Plex library from RSS feed of bittorrents
- Includes pattern matching and episode tracking
- This file will be run on a cron during unused times
+ All days at 3AM
"""
import feedparser
import json
import os
import sys
import time
import urllib.request
import shutil
import datetime
import glob
PATH = ""
if "SINGULAR_PATH" in os.environ:
PATH = os.environ["SINGULAR_PATH"]
if "win32" in sys.platform.lower():
DELIM = "\\" # Windows specific
cmd = """..{}..{}aria2{}aria2c.exe --seed-time=0 """.format(
DELIM, DELIM, DELIM)
else:
DELIM = "/" # Linux or Mac specific
cmd = """aria2c --seed-time=0 """.format(DELIM, DELIM, DELIM)
CONFIG = PATH + DELIM + "settings.json"
LOG = "singular.log"
DOWNLOAD = "data"
if "SINGULAR_DOWNLOAD" in os.environ:
DOWNLOAD = os.environ["SINGULAR_DOWNLOAD"]
def log(item):
"""
Logging function
"""
print(item.encode("utf-8", "ignore"))
with open(LOG, "a+") as f:
f.write(
"["
+ str(datetime.datetime.now())
+ "] "
+ str(item.encode("utf-8", "ignore"))
+ "\r\n"
)
def read_json():
"""
Parses in the JSON file
"""
return json.load(open(CONFIG, encoding="utf8"))
def read_rss(feed_url):
"""
Reads in the RSS feed from the given url
"""
log("Reading RSS")
return [feedparser.parse(url).entries for url in feed_url]
def is_match(title, matches):
"""
Checks if the title is a match for the season
"""
for match in matches:
if match.lower().replace(" ", "") in title.lower().replace(" ", ""):
# log ("Match found!" + title + " and " + match )
return match
return False
def direct_download_torrent(url):
"""Downloads a torrent directly"""
if not os.path.exists(DOWNLOAD + DELIM + "direct"):
os.mkdir(DOWNLOAD + DELIM + "direct")
full_cmd = "cd " + DOWNLOAD + DELIM + "direct && "
full_cmd += cmd + '"' + url + '"' + " > /download_output"
log("Direct download full command: " + full_cmd)
os.system(full_cmd)
with open("/download_output") as f:
log(f.read())
log("Direct Download Complete")
return 0
def download_torrent(url, match_name, title, replacements=[]):
"""
Downloads the given torrent
"""
match = match_name.replace(" ", "_")
if match not in os.listdir(DOWNLOAD):
os.system("mkdir " + DOWNLOAD + DELIM + match)
if title in os.listdir(DOWNLOAD + DELIM + match):
log(title + " already found. Ending")
return 1
if len([path for path in os.listdir(DOWNLOAD + DELIM + match) if title in path]):
log(title + " already found upon deeper inspection. Ending")
return 1
full_cmd = "cd " + DOWNLOAD + DELIM + match + " && "
full_cmd += cmd + '"' + url + '"' + " > /download_output "
log("Full command: " + full_cmd)
os.system(full_cmd)
with open("/download_output") as f:
log(f.read())
# Fix for if they use weird naming conventions
# Copying the files over
for item in glob.glob(DOWNLOAD + DELIM + match + DELIM + "*"):
if ".torrent" not in item:
real_name = item.replace("Episode ", "0")
for replacement in replacements:
log("Replacement: " + str(replacement))
real_name = real_name.replace(replacement[0], replacement[1])
if item != real_name:
log("Copying " + item + " to " + real_name)
try:
shutil.copy(item, real_name)
except Exception:
log("Failed during rename")
log("Download complete")
return 0
def thumb(match):
"""
Generates a thumbnail for the given file
Used for creation of thumbnails for the blog post
This basically only works since mkdir fails if an existing directory of thumbnails already exists
"""
path = DOWNLOAD + DELIM + match.replace(" ", "_") + DELIM
log("Starting thumb for " + str(path))
for item in glob.glob(path + "/*"):
if ".torrent" not in item and ".jpg" not in item and item[-1] != "/":
log('Starting command for ' + str(match))
os.system("cd " + path + " && mkdir '" + item.split(".")[0] + "' && cd '" + item.split(
".")[0] + "' && ffmpeg -i '" + item + "' -vf scale=720:405,fps=1/15 img%03d.jpg")
log("Finished creating thumb")
else:
log("Found either a torrent or an existing file in : " + item)
def parseName(item):
"""
Regex to return the proper name from a particular format from a particular feed
"""
try:
return item.split("]")[1].split("-")[0].strip()
except Exception:
log(str(sys.exc_info()[1]))
return -1
def artwork():
"""
Goes through each directory in data, download the artwork
"""
config = read_json()
for item in config["shows"]:
filename = item[0].replace(" ", "_")
if filename in os.listdir(DOWNLOAD):
if "show.jpg" not in os.listdir(DOWNLOAD + DELIM + filename):
log("""Thumbnail URL for {}: {}""".format(item, item[1]))
opener = urllib.request.build_opener()
opener.addheaders = [("User-agent", "Mozilla/5.0")]
urllib.request.install_opener(opener)
urllib.request.urlretrieve(
item[1], DOWNLOAD + DELIM + filename + DELIM + "show.jpg"
)
urllib.request.urlretrieve(
item[1], DOWNLOAD + DELIM + filename + DELIM + "poster.jpg"
)
def main():
"""
Main loop
"""
config = read_json()
with open(LOG, "w") as f:
f.write("[Starting]\r\n")
# Direct downloads
for url in config["direct"]:
log("Direct download for " + str(url))
direct_download_torrent(url)
# Feed Downloads
for feed in read_rss(config["rss_feed"]):
for item in feed:
match = is_match(item.title, [x[0] for x in config["shows"]])
do_thumb = 1
if match:
try:
replacements = [x[2]['replacements'] for x in config["shows"] if x[0] == match and len(x) > 2 and "replacements" in x[2]]
do_thumb = download_torrent(
item.links[0].href, match, item.title, replacements=replacements)
except Exception:
log("Download error: " + str(sys.exc_info()[1]))
do_thumb = 1
temp = [x for x in config["shows"] if x[0] == match and len(
x) > 2 and "thumbnail" in x[2] and str(x[2]["thumbnail"]) == "1"]
if temp != []:
log("Generating thumbnail...")
thumb(match)
artwork()
if __name__ == "__main__":
main()