-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtweeter.py
executable file
·87 lines (73 loc) · 3.59 KB
/
tweeter.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
# encoding: utf-8
# Copyright (c) 2022 Grant Hadlich
#
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
from twitterutils.twitterutils import tweet
from utils.utils import pull_tweets
from utils.plot_states import plot_state_sentiment
from utils.utils import parse_tweets
from utils.utils import create_composite
from time import sleep
from datetime import datetime
from sentimentmodels.get_models import get_models
from collections import defaultdict
if __name__ == "__main__":
now = datetime.now()
date = now.strftime("%Y-%m-%d")
tweets_per_state = 4000
print("Loading Models")
models = get_models()
print("Loaded Models: " + str([model.name() for model in models]))
print("Pulling Tweets from Twitter")
directory, tweets_pulled = pull_tweets(date, tweets_per_state)
# If tweets pulled can't be determined, just assign a number
if tweets_pulled == None:
tweets_pulled = tweets_per_state*51
model_data = dict()
model_tweet_content = dict()
model_filenames = dict()
for model in models:
print("Running Model: " + model.name())
model_type = model.model_name_long()
data, top_state, _ = parse_tweets(model, directory)
filename = plot_state_sentiment(data, model.name(), date, directory)
model_filenames[model.name()] = filename
top_state = "#"+top_state.replace(" ", "")
model_data[model.name()] = data
text = f"I analyzed the sentiment on Twitter for each state + DC from the last week using a {model_type}."
text += f"\nWhich state had the most positive mentions this week? It was {top_state}!"
text += f"\n#NLP #Python #ML"
model_tweet_content[model.name()] = text
# Create Composite Model
data, top_state = create_composite(model_data)
filename = plot_state_sentiment(data, "Composite", date, directory)
top_state = "#"+top_state.replace(" ", "")
print("Creating Initial Tweet")
text = f"This week I pulled {tweets_pulled} tweets on US State mentions. Most positive state was {top_state} according to an ensemble model! In the replies are the individual models."
text += "\nGitHub: https://github.com/ghadlich/StateSentiment"
text += "\n#NLP #Python #ML"
previous_id = tweet(text, image_path=filename, enable_tweet=True)
for model_name in model_tweet_content:
print("Tweeting Model: " + model_name)
previous_id = tweet(model_tweet_content[model_name], image_path=model_filenames[model_name], in_reply_to_status_id=previous_id, enable_tweet=True)
print("Completed Tweets")
#while True:
# sleep(3600*12-10)