forked from Astalaseven/twitter-rss
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
87 lines (73 loc) · 2.45 KB
/
server.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from flask import Flask, render_template, request, redirect, url_for
import twitter_rss
import config
app = Flask(__name__)
@app.route('/')
def index():
return render_template('index.tpl')
@app.route('/user', methods=['POST'])
def handle_user():
feed = request.form['user_data']
path = request.path
return redirect(url_for('feed_to_xml', feed=feed, path=path))
@app.route('/htag', methods=['POST'])
def handle_htag():
feed = request.form['user_data']
path = request.path
return redirect(url_for('feed_to_xml', feed=feed, path=path))
@app.route('/<path>/<feed>.xml')
def feed_to_xml(feed, path):
try:
with open(config.XML_DIR + path + '/' + feed + '.xml') as tweets:
tweets = tweets.read()
if not tweets:
err = 'Cache is empty'
error = render_template('index.tpl', err=err)
else:
error = tweets
except IOError:
try:
if path == 'user':
tweets = twitter_rss.UserTweetGetter(feed)
elif path == 'htag':
tweets = twitter_rss.HashtagTweetGetter(feed)
error = tweets.to_rss().encode('utf-8')
except AttributeError:
err = 'User not found'
error = render_template('index.tpl', err=err)
write_data_to_file(tweets, feed, path)
save_feed_for_updating(feed, path)
return error
def write_data_to_file(tweets, feed, path):
err=None
try:
data = tweets.to_rss().encode('utf-8')
print 'File does not exist: Creating feed...'
with open(config.XML_DIR + path + '/' + feed + '.xml', 'w') as cache:
cache.write(data)
cache.close()
error = tweets.to_rss()
except IOError:
err = 'File could not be written'
except AttributeError:
err = 'User not found'
error = render_template('index.tpl', err=err)
return error
def save_feed_for_updating(feed, path):
try:
with open(config.XML_DIR + path + '/' + path + '.txt', 'a') as save:
save.write(feed + '\n')
save.close()
except IOError:
print 'Could not save ' + feed + ' in ' + path
@app.errorhandler(404)
def page_not_found(code):
err = 'User not found'
error = render_template('index.tpl', err=err)
return error
if __name__ == "__main__":
# app.run(debug=True)
app.run(host='0.0.0.0')
# app.run()