-
Notifications
You must be signed in to change notification settings - Fork 5
/
app.py
99 lines (85 loc) · 3.23 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
import os, copy, datetime
from libfeed import tinyfeed, storage
from flask import (Flask, render_template, url_for, request, redirect, jsonify)
app = Flask(__name__)
app.config['SECRET_KEY'] = os.urandom(32)
#Storage facilities
dataobj = storage.DataStore(os.path.join('store', 'data.json'))
dataobj.initialize()
#RSS feed facilities
feedobj = tinyfeed.TinyFeed()
def latestvideos():
marked = []
#sample = dataobj.needUpdate()
#if sample:
# updated = feedobj.processSources(sample, dataobj.storedict)
# dataobj.store(updated)
datadict = copy.deepcopy(dataobj.storedict)
for sourceID in datadict:
newvideos = feedobj.filterEntries(datadict[sourceID]['entries'], 100)
if not newvideos: marked.append(sourceID)
else: datadict[sourceID]['entries'] = newvideos
for sourceID in marked: del datadict[sourceID]
return datadict
@app.context_processor
def ctxtprocessor():
def fancyDate(datestr):
return feedobj.formatDate(datestr)
return {'fancyDate': fancyDate}
@app.errorhandler(Exception)
def error_handler(error):
return jsonify({'success': False,
'code': error.code if hasattr(error, 'code') else None,
'details': '{}: {}'.format(error.__class__.__name__, error)
})
@app.route('/addfeed', methods = ['POST'])
def addfeed():
temp = {}
sourceID = request.args.get('sourceID')
temp[sourceID] = feedobj.processSource(sourceID)
dataobj.store(temp)
return jsonify({'success': True})
@app.route('/remfeed', methods = ['POST'])
def remfeed():
sourceID = request.args.get('sourceID')
dataobj.delete(sourceID)
return jsonify({'success': True})
@app.route('/update', methods = ['POST'])
def updatefeed():
sourceID = request.args.get('sourceID')
if sourceID == 'home':
datadict = feedobj.processSources(dataobj.storedict.keys(), dataobj.storedict)
else:
datadict = {sourceID: feedobj.processSource(sourceID, dataobj.storedict)}
dataobj.store(datadict)
return jsonify({'success': True})
@app.route('/download', methods = ['POST'])
def downloadfeed():
sourceID = request.args.get('sourceID')
if sourceID == 'home':
datadict = feedobj.processSources(dataobj.storedict.keys())
else:
datadict = {sourceID: feedobj.processSource(sourceID)}
dataobj.store(datadict)
return jsonify({'success': True})
@app.route('/feed')
def contentfeed():
sourceID = request.args.get('sourceID')
feed = copy.deepcopy(dataobj.storedict[sourceID])
for entry in feed['entries']:
entry['published'] = feedobj.formatDate(entry['published'])
return jsonify(feed)
@app.route('/filter')
def filterfeed():
sourceID = request.args.get('sourceID')
filterBy = request.args.get('filterBy', type = int)
feed = copy.deepcopy(dataobj.storedict[sourceID])
feed['entries'] = feedobj.filterEntries(feed['entries'], filterBy)
for entry in feed['entries']:
entry['published'] = feedobj.formatDate(entry['published'])
return jsonify(feed)
@app.route('/')
def index():
return render_template('index.html', sources = dataobj.sourceList(), datadict = latestvideos())
if __name__ == '__main__':
app.run(host = '0.0.0.0', port = 80, debug = False)