In the previous article, I wrote how to create a database of people who RT a specific account: A 1-second Python learning beginner creates a database of people who RT a specific account on Twitter
It seems that it was pasted on 2ch: [Good news] Programming beginners create a program to create a database of patriotic warriors who retweet DAPPI
When I made the title exaggerated as "1 second" in the sense of a beginner, it seems that it made the people of 2ch angry, so I will correct it from this time. I'm sorry.
When I looked at the thread, I found something like this. So, in this article, I will try to create something that blocks accounts at once using the DB created in the previous article.
It's a lot easier than the previous article.
ex2.py
# -*- coding:utf-8 -*-
import tweepy
import pyodbc
#API private key
CK = '****' #Consumer key
CKS = '****' #Consumer secret
AT = '****' #Access token
ATS = '****' #Access token secret
#Main routine
def main(total):
conn_str = 'Driver={{Microsoft Access Driver (*.mdb, *.accdb)}};Dbq={0};'.format("****.accdb")
#Fixed phrase for DB operation
conn = pyodbc.connect(conn_str)
cur = conn.cursor()
#A boilerplate for connecting to the twitter API
auth = tweepy.OAuthHandler(CK, CKS)
auth.set_access_token(AT, ATS)
api = tweepy.API(auth, wait_on_rate_limit=True)
sql = "SELECT screen_name FROM RTers ;"
cur.execute(sql)
row=cur.fetchone()
while row:
print("block:" +str(row[0][1:]))
api.create_block(str(row[0][1:]))
total=total+1
row=cur.fetchone()
if total==10:
break
print(str(total)+"Block cases")
#Close DB
cur.close()
conn.close()
return(total)
if __name__ == '__main__':
total=0
total=main(total) #Call the main function here
Almost all of the source code used last time has been stripped of unnecessary processing. Connect to the database and Twitter API and based on the information pulled from the database The target account is blocked by the Twitter API.
This is the only new one I used this time.
api.create_block(str(row[0][1:]))
create_block () is easy to use, the ID of the account to be blocked (the part after @) Just pass it as an argument. (Example: api.create_block ("chili_in"))
It will be like this.
This time, because it is a test, the execution of the program is stopped after processing 10 cases. You can block all accounts recorded in the DB by deleting the following part in the code.
if total==10:
break
Recommended Posts