-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.py
179 lines (168 loc) · 7.16 KB
/
functions.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
import re
import urllib
import requests
import json
import os
def process_filename (name):
strip = '[\\s(\\[\\-_][0-9]{4}x[0-9]{3}[\\s)\\]\\-_]?,[\\s(\\[\\-_][0-9]{4}x[0-9]{4}[\\s)\\]\\-_]?,[\\s(\\[\\-_][0-9]{3}x[0-9]{3}[\\s)\\]\\-_]?,^\\[[^\]]+\\],v[0-9]+'.split(',')
title = name
for ex in strip:
title = re.sub(re.compile(ex), r'', title)
title = re.sub(r'_', r' ', title)
print title
searchObj = re.match(r'(.*)\s?\-\s?([0-9]{2,3})[\s\-_](\[[0-9]{3,4}p\]).*', title)
if searchObj:
return {
'title': searchObj.group(1).strip(),
'episode': searchObj.group(2).strip(),
'res': searchObj.group(3).strip()
}
else:
searchObj = re.match(r'(.*)\s?\-\s?([0-9]{2,3})[\s\-_].*', title)
if searchObj:
return {
'title': searchObj.group(1).strip(),
'episode': searchObj.group(2).strip(),
'res': "res unknown"
}
return False
def get_my_shows ():
with open('myshows.cfg', 'r') as content_file:
content = content_file.read().split(',')
if len(content) == 1 and content[0].strip() == "":
return []
for i,c in enumerate(content):
content[i] = urllib.unquote_plus(c)
return content
def add_to_my_shows (title):
with open('myshows.cfg', 'r') as content_file:
content = content_file.read().split(',')
if len(content) == 1 and content[0].strip() == "":
content = []
content.append(urllib.quote_plus(title))
with open('myshows.cfg', 'w') as content_file:
content_file.write(','.join(content))
def remove_from_my_shows (title):
with open('myshows.cfg', 'r') as content_file:
content = content_file.read().split(',')
if len(content) == 1 and content[0].strip() == "":
content = []
for i,c in enumerate(content):
if title == urllib.unquote_plus(c):
del content[i]
with open('myshows.cfg', 'w') as content_file:
content_file.write(','.join(content))
def get_my_streams ():
with open('mystreams.cfg', 'r') as content_file:
content = content_file.read().split(',')
if len(content) == 1 and content[0].strip() == "":
return []
for i,c in enumerate(content):
content[i] = urllib.unquote_plus(c)
return content
def add_to_my_streams (title):
with open('mystreams.cfg', 'r') as content_file:
content = content_file.read().split(',')
if len(content) == 1 and content[0].strip() == "":
content = []
content.append(urllib.quote_plus(title))
with open('mystreams.cfg', 'w') as content_file:
content_file.write(','.join(content))
def remove_from_my_streams (title):
with open('mystreams.cfg', 'r') as content_file:
content = content_file.read().split(',')
if len(content) == 1 and content[0].strip() == "":
content = []
for i,c in enumerate(content):
if title == urllib.unquote_plus(c):
del content[i]
with open('mystreams.cfg', 'w') as content_file:
content_file.write(','.join(content))
def arrange_into_tiles (feed, my_shows, tiles = None):
if tiles == None:
tiles = { 'my_shows': [], 'other': [] }
for ent in feed['entries']:
info = process_filename(ent['title'])
if info == False:
print "ignored"
#tiles['other']['Other Shows'].append({ 'title': ent['title'], 'link': ent['link'], 'date': ent['published'] })
else:
info['link'] = ent['link']
info['date'] = ent['published']
if info['title'] in my_shows:
added = False
for show in tiles['my_shows']:
if show['title'] == info['title']:
for ep in show['episodes']:
if ep['episode'] == info['episode']:
ep['files'][info['res']] = info
added = True
if not added:
show['episodes'].append({ 'episode': info['episode'], 'files': { info['res']: info } })
added = True
if not added:
tiles['my_shows'].append({ 'title': info['title'], 'episodes': [ { 'episode': info['episode'], 'files': { info['res']: info } } ] })
else:
added = False
for show in tiles['other']:
if show['title'] == info['title']:
for ep in show['episodes']:
if ep['episode'] == info['episode']:
ep['files'][info['res']] = info
added = True
if not added:
show['episodes'].append({ 'episode': info['episode'], 'files': { info['res']: info } })
added = True
if not added:
tiles['other'].append({ 'title': info['title'], 'episodes': [ { 'episode': info['episode'], 'files': { info['res']: info } } ] })
return tiles
def get_daisuki_tiles (my_streams):
shows = json.loads(requests.get('http://www.daisuki.net/fastAPI/anime/search').text)
tiles = { 'my_shows': [], 'other': [] }
for show in shows['response']:
show['title'] = show['title'].encode('ascii', 'ignore').decode('ascii');
if show['title'] in my_streams:
show['episodes'] = get_daisuki_episodes(show['ad_id'])
tiles['my_shows'].append(show)
else:
tiles['other'].append(show)
return tiles
def get_daisuki_episodes (ad_id):
page = requests.get('http://www.daisuki.net/anime/detail/'+ad_id).text
print 'http://www.daisuki.net/anime/detail/'+ad_id
episodes = []
for i in range(1000):
m = re.search('class="episodeNumber"><a href="(.*)">'+str(i+1)+'</a>', page)
if m:
episodes.append('http://www.daisuki.net/'+m.group(1))
else:
m = re.search('class="episodeNumber">'+str(i+1)+'</p>\n\s+<div class="play"><a href="(.*)"></a>', page)
if m:
episodes.append('http://www.daisuki.net/'+m.group(1))
else:
break
return episodes
def get_files_from (folder):
relevant_path = folder
included_extenstions = ['mkv','avi','mp4','mpg' ] ;
file_names = [] #[fn for fn in os.listdir(relevant_path) if any([fn.endswith(ext) for ext in included_extenstions])];
for root, dirs, files in os.walk(folder, followlinks=True):
for file in files:
if any([file.endswith(ext) for ext in included_extenstions]):
file_names.append(file)
ret = []
for f in file_names:
info = process_filename(f)
if info:
info['path'] = folder+'/'+f
added = False
for s in ret:
if s['title'] == info['title']:
s['episodes'][int(info['episode'])] = info
if s['max_episode'] < info['episode']:
s['max_episode'] = info['episode']
added = True
break
if not added:
ret.append({ 'title': info['title'], 'max_episode': info['episode'], 'episodes': { int(info['episode']): info } })
return ret