A simple flask app that combines Tump's tweets and related CNN articles.
I decided to do some sentiment analysis on the messages. I used the nltk library developed by Bird, Steven, Edward Loper and Ewan Klein (2009), Natural Language Processing with Python. O’Reilly Media Inc.
On the left of the card, it is shown the source of that information, whether its from Twitter or CNN. If the model finds it to be negative sentences on average, it's backgound turns red. If positive, green. Neutrals are blank.
This is assuming that you're using bash
- Make yourself a virtual environment:
python -m venv .venv
Enter it:
source .venv/bin/activate
- Install pip requirements:
pip install -r requirements.txt
-
Add the environment variables:
There are 4 variables required to access twitter and one for flask:
- CKEY Consumer Key
- CSECRET Consumer Secret
- ATOKEN Authentication Token
- TSECRET Token Secret
- FLASK_APP Required by Flask
You have to get them from https://developer.twitter.com/
export -x FLASK_APP=main.py
export -x CKEY=XX
export -x CSECRET=XX
export -x ATOKEN=XX
export -x TSECRET=XX
-
That's it.
Running it:
gunicorn wsgi
-
Runtime Configurations
Let's say you want to tune it a bit, there are a few things that come to mind:
-
cnn.py The constructor has the following signature:
def __init__(self, article_class, feeds = [], search_terms = []):
feeds
is a optional list of RSS urls that are added to the pre-select 3 cnn sources.search_terms
is another optional argument, takes in strings that, if found in the title or summary, considers the article as related to Trump. -
twitter.py
def __init__(self, article_class, twitter_account = 25073877):
The optional argument twitter_account is the twitter id of the desired user whose tweets will be queried. Defaults to @realDonaldtrump.
-