-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbot.py
More file actions
163 lines (118 loc) · 6.61 KB
/
bot.py
File metadata and controls
163 lines (118 loc) · 6.61 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# encoding: utf-8
# the above lets python know that we're ok using characters that aren't exclusively english
#################################################
# importing stuff
"""
One of the most powerful parts of python is its ability to easily import libraries like
the above. Theoretically you can run any of the following via pythonanywhere:
- https://www.pythonanywhere.com/batteries_included/
If you're running your own server you can import anything....
A joke on this theme can be found here: https://xkcd.com/353/
"""
import tweepy # library for handling twitter commands
import time # library for adding in delays or working (you guessed it) specific times
#################################################
# Authenticating
"""
You will need to create a twitter developer account to add the values below:
To do so, create a twitter account for your bot at twitter.com, and then visit: https://apps.twitter.com/
Enter the name and description of your app, and under the website option you can put down whatever you want.
Once the app is created, head to the Keys and Access Tokens tab.
This is where we will get our keys to authorize the bot and they should be pasted below:
"""
consumer_key = ""
consumer_secret = ""
access_token = ""
access_token_secret = ""
# this is code that we use from the `tweepy` library to authenticate with twitter. It's the equivalent of logging in w/ a password
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
#################################################
# Reading Tweets
"""
You have lots of different options here.
More information can be found in the tweepy docs: https://tweepy.readthedocs.io/en/v3.5.0/api.html
But we'll demonstrate a few here:
"""
a_single_users_tweets = api.user_timeline("russelneiss") # Returns the 20 most recent statuses posted for the given user
trending_subjects_on_twitter = api.trends_place(1) # the number here corresponds to a specific WOEID (where on earth ID) 1 == whole world. 2513983 == brandeis, Israel == 23424852 etc. More can be found here: http://www.woeidlookup.com/
#search queries are most easily generated by using twitter's advanced search page, and then copying the query from the resulting URL
#https://twitter.com/search-advanced
simple_search_term = api.search("brandeis")
more_complicated_search_term = api.search("🇳🇮%20israel%20-nicaragua%20-🇮🇱%20-nicaraguan-filter%3Aretweets")
"""
all of the above are objects that need to be parsed to be made more human readable...
some examples are below. Just remove the triple quotes at they top and bottom of each section you want to test.
for data that looks impossible to read -- copy and paste it into this site: http://jsbeautifier.org/
and it will be formatted more nicely.
"""
"""
for tweet in a_single_users_tweets: #i.e. go through every tweet in a_single_users_tweets (from above) one at a time and do the following:
print tweet.text #instead of `text` try `id` or `retweet_count` or `created_at` or simply just use `tweet` without a modifier.
"""
"""
trends = trending_subjects_on_twitter[0]['trends'] # the difference here in structure is based on the data from tweepy we get back in the api
for topic in trends:
print topic["name"] + " " + str(topic["tweet_volume"]) # the str() function is required to convert `tweet_volume` into a string that can be merged with the text string from `name`
"""
"""
for tweet in simple_search_term:
print tweet.user.screen_name #in addition to the examples from above in single_user_tweets try exploring the kind of data you get back with tweet.user and its subcategories
"""
#################################################
# Processing/remixing tweets
"""
This is where the magic happens. You take whatever data you've gleaned from above -- and remix it into something else.
Some exceptional examples:
- https://twitter.com/pentametron
- https://twitter.com/RealPressSecBot
- https://twitter.com/_grammar_
- https://twitter.com/thatsNotIsrael
- https://twitter.com/amiritebot
- https://twitter.com/accidental575
- http://mollywhite.net/bots#yourevalued *** This one was really lovely -- but was banned by twitter for TOS violations. Take note.
- https://twitter.com/ShabBOTinator *** This was a previous student bot -- also now banned for TOS violations
You also theoretically have the ability to take your data from elsewhere and manipulate it before pushing it back out:
- https://twitter.com/congressedits
- https://twitter.com/Stl_Manifest
- https://twitter.com/everyword
- https://twitter.com/censusAmericans
- https://twitter.com/exosaurs
- https://twitter.com/TwoHeadlines
- https://twitter.com/PowerVocabTweet
- https://twitter.com/Loving_Israel ** built by students -- stopped working when Google changed their API
Use the above for inspiration -- but you're on your own here -- :)
Happy to give some advice if you come up with an idea -- but this is where the creative work comes in....
"""
#################################################
# Posting tweets
"""
in general by this point you've crafted some sort of tweet from the content you've generated above
to post it it's simpy a matter of just dropping the variable in the code below.
(Don't forget to uncomment when you're ready to actually tweet)
Hacker tip: if you want a tweet to come up quoted -- you simply need to post its URL.
Its URL is always in the form:
https://twitter.com/THE_USER_WHO_TWEETED_IT/status/THE_ID_OF_THE_TWEET
From the code above you should be able to generate that pretty easily.
"""
#api.update_status(tweetVariable+" "+"or some plain text string, or some combination of both")
#RTs are the easiest of the three, just pass the ID number:
#api.retweet(id)
#you can also upload images or animated gifs etc using the code here: https://tweepy.readthedocs.io/en/v3.5.0/api.html#API.update_with_media
#################################################
# closing details
#
# Read the docs @ https://tweepy.readthedocs.io/en/v3.5.0/api.html
# you can do many many many more things including auto follow, get your followers and interact with them
# reply to people tweeting at you etc....
#
# you may have noticed that this script theoretically tweets only once.
#
# also you can use other APIs to generate input data for tweets
# Many can be found here: https://www.programmableweb.com/category/all/apis
# If you're interested in a Torah oriented bot you can pull from my work: https://github.com/Sefaria/Sefaria-Project/wiki/API-Documentation
#
# some additional bot building info can be found here: http://tinysubversions.com/2013/09/how-to-make-a-twitter-bot/
#
######################################################