This repository has been archived by the owner on Aug 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
326 lines (296 loc) · 13.2 KB
/
app.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
from datetime import date
from flask import Flask, render_template, request, Response, g
import xml.etree.ElementTree as ET
import sys
import urllib.request
import urllib.parse
from configparser import ConfigParser
import redis
import os
config = ConfigParser()
app = Flask(__name__)
redis_url = os.getenv('REDIS_URL', 'redis://localhost:6379')
def initdb():
db = redis.from_url(redis_url)
return db
@app.before_request
def before_request():
g.db = initdb()
# check if SID exists in config.ini
if config.read('config.ini') != [] and config.get('IB', 'sid') != '':
sid = config.get('IB', 'sid')
# check if SID exists in environment variable
elif os.environ.get('SID') is not None:
sid = os.environ.get('SID')
# else get guest SID from IB
else:
data = urllib.parse.urlencode(
{'username': 'guest', 'password': '', 'output_mode': 'xml'}).encode('utf-8')
source = urllib.request.urlopen(
url='https://inkbunny.net/api_login.php', data=data)
sid = ET.parse(source).getroot()[0].text
# datadict is flags used for IB search, root is xml builder,
#cachename is key name for redis cache
def ibrss(datadict,root,cachename):
rating = datadict.get('rating')
items = list(root[7])
vitemtitle = []
vitemid = []
vitemlink = []
vitemthumb = []
vitemtype = []
vitemdesc = []
vitemuser = []
vitemrating = []
vitempubdate = []
vitemimg = []
vrsstitle = 'Inkbunny - '
filename = 'IB'
# add contents of elements to their lists
for content in root.iter('title'):
vitemtitle.append(content.text)
for content in root.iter('submission_id'):
vitemid.append(content.text)
for content in root.iter('username'):
vitemuser.append(content.text)
for content in root.iter('type_name'):
vitemtype.append(content.text)
for content in root.iter('rating_id'):
vitemrating.append(content.text)
for content in root.iter('file_url_full'):
vitemimg.append(content.text)
# add link to vitemlink
for i in vitemid:
vitemlink.append('https://inkbunny.net/s/' + i)
# make RSS title. If no query found, revert to 'ALL posts'
# make filename for unique rss files.
flags = ['text', 'keyword_id', 'username',
'user_id', 'favs_user_id', 'pool_id']
for key in flags:
if key in datadict:
if key == 'text':
vrsstitle = vrsstitle + 'Search Query: "' + \
datadict.get(key) + '"; '
elif key == 'keyword_id':
tmpdata = {'sid': sid, 'output_mode': 'xml', 'show_pools': 'yes', 'count_limit': '1',
'submission_ids': root[7][0].find('submission_id').text}
tmpdata = urllib.parse.urlencode(tmpdata).encode('utf-8')
tmpsource = urllib.request.urlopen(
url='https://inkbunny.net/api_submissions.php', data=tmpdata)
tmptree = ET.parse(tmpsource)
tmproot = tmptree.getroot()
for i in tmproot[3][0].find('keywords'):
if i[0].text == datadict.get(key):
vrsstitle = vrsstitle + 'Keyword: ' + i[1].text + '; '
elif key == 'username':
vrsstitle = vrsstitle + 'User: ' + datadict.get(key) + '; '
elif key == 'user_id':
vrsstitle = vrsstitle + 'User: ' + vitemuser[0] + '; '
elif key == 'favs_user_id':
tmpdata = {'sid': sid, 'output_mode': 'xml','count_limit': '1',
'user_id': datadict.get(key)}
tmpdata = urllib.parse.urlencode(keydata).encode('utf-8')
tmpsource = urllib.request.urlopen(
url='https://inkbunny.net/api_search.php', data=tmpdata)
tmptree = ET.parse(tmpsource)
tmproot = tmptree.getroot()
vrsstitle = vrsstitle + 'Favourites by ' + \
tmproot[7][0].find('username').text + '; '
elif key == 'pool_id':
tmpdata = {'sid': sid, 'output_mode': 'xml', 'count_limit':'1',
'show_pools': 'yes', 'submission_ids': '1776649'}
tmpdata = urllib.parse.urlencode(tmpdata).encode('utf-8')
tmpsource = urllib.request.urlopen(
url='https://inkbunny.net/api_submissions.php', data=tmpdata)
tmptree = ET.parse(tmpsource)
tmproot = tmptree.getroot()
for i in tmproot[3][0].find('pools'):
if i[0].text == datadict.get(key):
vrsstitle = vrsstitle + 'Pool: ' + \
i[1].text + ' by ' + vitemuser[0] + '; '
if vrsstitle == 'Inkbunny - ':
vrsstitle = vrsstitle + 'ALL posts'
vrsstitle = vrsstitle.strip('; ')
for i in items:
filenamelength = len(i.findtext('file_name'))
# find thumbnail and append to vitemthumb
if i.find('thumbnail_url_huge_noncustom') != None:
vitemthumb.append(i.find('thumbnail_url_huge_noncustom').text)
elif i.find('thumbnail_url_huge') != None:
vitemthumb.append(i.find('thumbnail_url_huge').text)
# if no thumbnail found, use default thumbnail
elif i.findtext('file_name')[filenamelength - 3:filenamelength] in ['png', 'jpg', 'gif', 'swf', 'flv', 'mp4', 'mp3', 'doc', 'rtf', 'txt']:
if i.findtext('file_name')[filenamelength - 3:filenamelength] in ['png', 'jpg', 'gif']:
vitemthumb.append(
'https://au.ib.metapix.net/images78/overlays/nofile.png')
elif i.findtext('file_name')[filenamelength - 3:filenamelength] in ['doc', 'rtf', 'txt']:
vitemthumb.append(
'https://au.ib.metapix.net/images78/overlays/writing.png')
elif i.findtext('file_name')[filenamelength - 3:filenamelength] in ['flv', 'mp4']:
vitemthumb.append(
'https://au.ib.metapix.net/images78/overlays/video.png')
elif i.findtext('file_name')[filenamelength - 3:filenamelength] in ['swf']:
vitemthumb.append(
'https://au.ib.metapix.net/images78/overlays/shockwave.png')
elif i.findtext('file_name')[filenamelength - 3:filenamelength] in ['mp3']:
vitemthumb.append(
'https://au.ib.metapix.net/images78/overlays/audio.png')
else:
vitemthumb.append(
'https://au.ib.metapix.net/images78/overlays/nofile.png')
# add description to vitemdesc
for i in range(len(items)):
vitemdesc.append('<a href="' + vitemlink[i] + '"><img src="' + vitemthumb[i] + '"></a><p><a href="' + vitemlink[i] + '">Submission</a> | <a href="' + vitemimg[i] + '">Direct</a></p>Type: '
+ vitemtype[i] + ' <br/><br/><a href="http://inkbunny.net/' + vitemuser[i] + '">By ' + vitemuser[i] + '</a>')
# create integer to day string for vitempubdate
def dayint(di):
if di == 0:
return 'Mon'
elif di == 1:
return 'Tue'
elif di == 2:
return 'Wed'
elif di == 3:
return 'Thu'
elif di == 4:
return 'Fri'
elif di == 5:
return 'Sat'
elif di == 6:
return 'Sun'
# create integer to month string for vitempubdate
def monint(mi):
if mi == 1:
return 'Jan'
elif mi == 2:
return 'Feb'
elif mi == 3:
return 'Mar'
elif mi == 4:
return 'Apr'
elif mi == 5:
return 'May'
elif mi == 6:
return 'Jun'
elif mi == 7:
return 'Jul'
elif mi == 8:
return 'Aug'
elif mi == 9:
return 'Sep'
elif mi == 10:
return 'Oct'
elif mi == 11:
return 'Nov'
elif mi == 12:
return 'Dec'
# convert create_datetime to valid datetime for RSS
for i in items:
ibdate = i[4].text
yyyy = ibdate[0:4]
mm = ibdate[5:7]
dd = ibdate[8:10]
hhmmss = ibdate[11:19]
tz = ibdate[len(ibdate) - 3:len(ibdate)] + '00'
day = dayint(date(int(yyyy), int(mm), int(dd)).weekday())
month = monint(int(mm))
pubdate = day + ', ' + dd + ' ' + month + ' ' + yyyy + ' ' + hhmmss + ' ' + tz
vitempubdate.append(pubdate)
# RSS builder
rss = ET.Element('rss', attrib={'version': '2.0'})
channel = ET.Element('channel')
rss.append(channel)
rsstitle = ET.SubElement(channel, 'title')
rsstitle.text = vrsstitle
rsslink = ET.SubElement(channel, 'link')
for i in range(len(items)):
# skip submissions if posts don't fit the rating
if rating == '001':
if vitemrating[i] == '0': continue
elif vitemrating[i] == '1': continue
elif rating == '011':
if vitemrating[i] == '0': continue
elif rating == '110':
if vitemrating[i] == '2': continue
elif rating == '010':
if vitemrating[i] == '0': continue
elif vitemrating[i] == '2': continue
elif rating == '100':
if vitemrating[i] == '1': continue
elif vitemrating[i] == '2': continue
elif rating == '101':
if vitemrating[i] == '1': continue
# elif rating == '111'
item = ET.Element('item')
channel.append(item)
itemtitle = ET.SubElement(item, 'title')
itemtitle.text = vitemtitle[i]
itemlink = ET.SubElement(item, 'link')
itemlink.text = vitemlink[i]
itemdesc = ET.SubElement(item, 'description')
itemdesc.text = vitemdesc[i]
itemguid = ET.SubElement(item, 'guid', isPermaLink='false')
itemguid.text = vitemid[i]
itempubdate = ET.SubElement(item, 'pubDate')
itempubdate.text = vitempubdate[i]
feed = ET.tostring(rss, encoding='utf-8', method='xml')
g.db.set(cachename, feed, ex=86400) # set feed cache to expire in 24 hours unless if updated.
return Response(feed, mimetype='application/rss+xml')
@app.route('/')
def home():
return render_template('home.html')
@app.route('/search')
def searchquery():
invalid = ''
cachename = ''
keylist = list(request.args.keys())
# returns nested list. [('username', [u'ww'])] [0][1][0] = ww
flaglist = list(request.args.lists())
flags = ['rating', 'sid', 'output_mode', 'field_join_type', 'text', 'string_join_type', 'keywords', 'title', 'description', 'md5', 'keyword_id',
'username', 'user_id', 'favs_user_id', 'unread_submissions', 'type', 'sales', 'pool_id', 'orderby', 'dayslimit', 'random', 'scraps']
for i in keylist:
if i not in flags:
invalid += i + ', '
for flag in flaglist:
if 'sid' in flag:
return 'User defined SIDs are restricted for this app.<br>Please define the SID in <code>config.ini</code> or in the <code>SID</code> environment variable'
if request.args.get('rating') == '000':
return 'Oi m8 wth are ya tryna do? Break ma code?'
elif request.args.get('rating') not in ['001','011','010','110','101','100','111',None]:
invalid += 'rating'
if invalid == '': #no errors in url query
# rebuild url query from flags
data = {}
if isinstance(flaglist, tuple): # patch for single arguement
flaglist = [flaglist]
for flag in flaglist:
for value in flag[1]:
cachename = cachename + flag[0] + ':' + value + ','
data[flag[0]] = value
cachename = str(cachename.strip(',')) # make unique name for redis
data.update({'sid': sid, 'output_mode': 'xml'})
datadict = data.copy() #clone dictionarys - datadict for ibrss() and data for API request
if 'rating' in data: del data['rating'] #remove custom flag 'rating' before using in API request
url = 'https://inkbunny.net/api_search.php'
data = urllib.parse.urlencode(data).encode('utf-8')
root = ET.parse(urllib.request.urlopen(url=url, data=data)).getroot()
if root[1].tag == 'error_message':
return 'Error: ' + root[1].text
elif len(list(root[7])) == 0:
return 'No submissions found. Try again when your search has at least one submission.'
try:
if ET.fromstring(g.db.get(cachename))[0][2][3].text == root[7][0].find('submission_id').text:
source = ''
return Response(g.db.get(cachename), mimetype='application/rss+xml') #already exists, return feed from cache
else:
return ibrss(datadict,root,cachename) # latest post doesn't match, update feed
except TypeError: #doesnt exist in cache
return ibrss(datadict,root,cachename)
except IndexError: #unknown error in the flags (bypassed the error checker) last resort.
g.db.delete(cachename)
return ibrss(datadict,root,cachename)
else:
return 'Invalid flag(s): ' + invalid.strip(', ') + '<br>Please check the URL and try again.'
if __name__ == '__main__':
port = int(os.environ.get('PORT', 5000))
app.run(host='0.0.0.0', port=port)