-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
51 lines (40 loc) · 1.56 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
from flask import Flask, render_template, request
import requests
import json
import bs4
from bs4 import BeautifulSoup
from requests_oauthlib import OAuth1
app = Flask(__name__)
@app.route('/')
def home():
return render_template("base.html")
@app.route('/player')
def player():
player_query = request.args.get("q", None)
if not player_query:
return f"Couldn't find tweets for {player_query}"
auth_params = {
'app_key':'rCguhoSNkQGrj2WfowC0iI4rj',
'app_secret':'nMU6MarVejiIpTPCx7aYrHwuiawbRywh13lXhXOuymbz77BieZ',
'oauth_token':'992987135723687936-bJDIDdI8frBErPbyx6V9rYuneWtm3CX',
'oauth_token_secret':'tkYmrFRQJFWYpS33AzAighKPIjTin4UJCOEcWRWeLvH2G'
}
# Creating an OAuth Client connection
auth = OAuth1(
auth_params['app_key'],
auth_params['app_secret'],
auth_params['oauth_token'],
auth_params['oauth_token_secret']
)
q = f'{player_query} -filter:retweets AND -filter:replies'
# url according to twitter API
url_rest = "https://api.twitter.com/1.1/search/tweets.json"
params = {'q': q, 'count': 8, 'lang': 'en', 'result_type': 'recent'}
results = requests.get(url_rest, params=params, auth=auth)
tweets = results.json()
messages = [BeautifulSoup(tweet['text'], 'html5lib').get_text() for tweet in tweets['statuses']]
print(messages)
"""Return homepage."""
return render_template('player.html', q=q, tweets=tweets, messages=messages)
if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0', port=os.environ.get('PORT', 5000))