"The one that blocks all Twitter accounts in the database" created by beginners of Python learning day

Preface

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: 000.png [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.

What to do this time

When I looked at the thread, I found something like this. 001.png So, in this article, I will try to create something that blocks accounts at once using the DB created in the previous article.

code

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"))

What happens when you run

It will be like this. 01.png

02.png

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

"The one that blocks all Twitter accounts in the database" created by beginners of Python learning day
The first web app created by Python beginners
The one that displays the progress bar in Python
Basic data frame operations written by beginners in a week of learning Python
Python beginners tried implementing REST API in one day
One liner that outputs 1000000 digits of pi in Python
[Python] Outputs all combinations of elements in the list
What beginners learned from the basics of variables in python
I tried to summarize the contents of each package saved by Python pip in one line
[Understanding in the figure] Management of Python virtual environment by Pipenv
Read the standard output of a subprocess line by line in Python
A function that measures the processing time of a method in python
Solve with Python [100 selected past questions that beginners and intermediates should solve] (005 --- 009 All search: All enumeration to reduce the number of streets by devising)
Note that I understand the algorithm of the machine learning naive Bayes classifier. And I wrote it in Python.
Machine learning summary by Python beginners
Access the Twitter API in Python
About bit full search that often appears in competition pros From the eyes of beginners with python
Learn the basics of Python ① Beginners
Prepare the necessary library of python like gemfile of ruby so that the package required by Project can be done in one shot like bundle install
[Example of Python improvement] What is the recommended learning site for Python beginners?
Get a datetime instance at any time of the day in Python
Python learning memo for machine learning by Chainer until the end of Chapter 2
[Python] I tried to analyze the characteristics of thumbnails that are easy to play on YouTube by deep learning
Check the behavior of destructor in Python
[Learning memo] Basics of class by python
Learning notes from the beginning of Python 1
Tweet using the Twitter API in Python
The result of installing python in Anaconda
Read the file line by line in Python
Read the file line by line in Python
The basics of running NoxPlayer in Python
Pandas of the beginner, by the beginner, for the beginner [Python]
In search of the fastest FizzBuzz in Python
Learning notes from the beginning of Python 2
[Python] Combine all the elements in the array
You will be an engineer in 100 days --Day 29 --Python --Basics of the Python language 5
[Python] Summary of functions that return the index that takes the closest value in the array
You will be an engineer in 100 days --Day 33 --Python --Basics of the Python language 8
You will be an engineer in 100 days --Day 26 --Python --Basics of the Python language 3
Make the library created by Eigen in C ++ available from Python with Boost.Numpy.
How to read all the classes contained in * .py in the directory specified by Python
Divides the character string by the specified number of characters. In Ruby and Python.
Get Unix time of the time specified by JST regardless of the time zone of the server in Python
How to find the coefficient of the trendline that passes through the vertices in Python
You will be an engineer in 100 days --Day 32 --Python --Basics of the Python language 7
Implementation of a model that predicts the exchange rate (dollar-yen rate) by machine learning
Get the last element of the array by splitting the string in Python and PHP
You will be an engineer in 100 days --Day 28 --Python --Basics of the Python language 4