-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.py
More file actions
97 lines (76 loc) · 2.62 KB
/
test.py
File metadata and controls
97 lines (76 loc) · 2.62 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
92
93
94
95
96
97
"""
adapted from: https://twarc-project.readthedocs.io/en/latest/api/library/
"""
# imports
import datetime
import os
import argparse
from typing import Tuple, Optional
from twarc.client2 import Twarc2
from twarc.expansions import ensure_flattened
from flask import Flask, request, render_template, jsonify
from get_followers import get_user_following_tweets
import re
import botometer
def parser():
# argument parser
ap = argparse.ArgumentParser()
ap.add_argument('-e', '--env', type=str,
default='.env', help='environment configuration file that contains \
user keys for twitter api v2')
args = ap.parse_args()
return args
# flask instance
app = Flask(__name__)
# twarc instance placeholder
t = None
pattern = re.compile('[\W_]+', re.UNICODE)
def run_results(search_results):
res = ''
for page in search_results:
# Do something with the whole page of results:
# print(page)
# or alternatively, "flatten" results returning 1 tweet at a time, with expansions inline:
for tweet in ensure_flattened(page):
# print tweet text
# res = res + '\n' + re.sub('[\W_]+', ' ', tweet['text']) + '\n'
res = res + '\n' + tweet['text'] + '\n'
# Stop iteration prematurely, to only get 1 page of results.
break
return res
@app.route('/')
def my_form():
return render_template('index.html')
@app.route('/form_post', methods=['POST', 'GET'])
def form_post():
if (request.method == 'POST'):
if (request.form['id'] == 'search-bar'):
query = request.form['query']
if (query):
search_results = t.search_recent(query=query, start_time=None,
end_time=None, max_results=10)
res = run_results(search_results)
return jsonify(result=res)
if (request.form['id'] == 'user-search'):
query = request.form['query']
if (query):
user_ids = get_user_following_tweets([query], t, 10, 5)
return jsonify(result=str([key for key in user_ids[query]['following'].keys()]))
if (request.method == 'GET'):
print('get')
return ("nothing")
if __name__ == "__main__":
# get arguments
args = parser()
# parse configuration file
f = open(args.env)
toks = [line.split('=')[1].strip('\n') for line in f]
# initialize twarc2 instance
t = Twarc2(
toks[1], # consumer token
toks[2], # consumer secret
toks[3], # access token
toks[4], # access token secret
toks[0]) # bearer token
# start flask app
app.run(port=8000)