-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtweets.py
More file actions
91 lines (79 loc) · 2.4 KB
/
tweets.py
File metadata and controls
91 lines (79 loc) · 2.4 KB
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
from datetime import datetime
import logging
from flask import Blueprint, jsonify, Response, render_template, abort
from flask_cors import cross_origin
from .auth import login_exempt
from lib.tweets_base import readTweetsApiJson
from lib.Tweet import Tweet
from lib.TweetForest import TweetForest
bp = Blueprint('tweets', __name__, url_prefix='/tweets')
@bp.route('/')
@cross_origin()
@login_exempt
def root():
return jsonify({
'date': int(datetime.timestamp(datetime.now())),
'tweets': readTweetsApiJson()
})
@bp.route('/<int:id>')
def tweet(id):
tweet = Tweet.loadFromFile(id)
return jsonify(tweet.data)
@bp.route('/forest')
def forest_show():
return render_template('forest.html.j2', forest = TweetForest.fromFolder())
#return Response(str(forest), mimetype='text/plain')
@bp.route('/forest/renew')
def forest_create():
logging.warning('Manual invocation of creating forest!')
# renew forest
forest = TweetForest.fromFolder()
forest.saveApiJson()
return jsonify(readTweetsApiJson())
@bp.route('/add/<int:id>')
def add(id):
logging.warning('Manual invocation of adding tweet (id: {})!'.format(id))
tweet = Tweet.loadFromTwitter(id)
tweet.save()
# renew forest
forest = TweetForest.fromFolder()
forest.saveApiJson()
return jsonify({
'message': 'added',
'tweet': {
'id': id,
'data': tweet.data
}
})
@bp.route('/delete/<int:id>')
def delete(id):
logging.warning('Manual invocation of deleting tweet (id: {})!'.format(id))
tweet = Tweet.loadFromFile(id)
tweet.delete()
# renew forest
forest = TweetForest.fromFolder()
forest.saveApiJson()
return jsonify({
'message': 'deleted',
'tweet': {
'id': id,
'data': tweet.data
}
})
@bp.route('/all')
def all():
tweets = Tweet.loadFromFolder()
tweets.sort(key = lambda x: x.getDateTime(), reverse = True)
# [Tweet(i) for i in tweets]
return render_template('all.html.j2', tweets = tweets)
@bp.route('/stories')
def info():
tweets = readTweetsApiJson()
stories = {};
for id, info in tweets.items():
if 'story' in info:
storyId = info['story']
if storyId not in stories:
stories[storyId] = []
stories[storyId].append(Tweet.loadFromFile(id))
return render_template('stories.html.j2', stories = stories)