Try using Tweepy [Python2.7]

I found a Twitter client for Python called tweepy, so I played around with it. In the first place, I don't usually use Twitter, so in my case, I can't use it when I made it. .. ..

First of all, as an implementation ・ Token issuance ・ Delete tweets ・ Unrequited love or follow ・ Follow users who are following a specific account The definition of unrequited love is here

  1. A state in which you are following and the other party is not following.
  2. The other party is following and you are not following. will do.

I will explain the function name and processing respectively. deletion(self, id, count=200) --id = account id (@XXX) --count = number This function gets the tweet of id specified by argument id by argument count Delete. This function works if the token is the same as that of id. Of course, you cannot delete other people's tweets.

destroy(self, id) --id = account id (@XXX) This function unfollows a user whose id specified by the argument id is unrequited love (definition 1).

create(self, id) --id = account id (@XXX) This function follows a user whose id specified by the argument id is unrequited love (definition 2).

follow(self, id) --id = account id (@XXX) This function follows the user who is following the user with the id specified by the argument id.


class Token:
	def __init__(self, consumer_key, consumer_secret):
		self.auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
	def GET(self):
		redirect_url = self.auth.get_authorization_url()
		print "URL: %s" % (str(redirect_url))
		self.auth.get_access_token(raw_input('code: ').strip())
		return [self.auth.access_token, self.auth.access_token_secret]

class Twitter_Class:
	def __init__(self, consumer_key, consumer_secret, access_token, access_secret):
		auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
		auth.set_access_token(access_token, access_secret)
		self.api = tweepy.API(auth)

	def deletion(self, id, count=200):
		try:
			map(self.api.destroy_status, [i.id for i in self.api.user_timeline(id, count=count)])
		except Exception, e:
			print e

	def destroy(self, id):
		try:
			friends_ids = self.api.friends_ids(id)
			followers_ids = self.api.followers_ids(id)
			list_ = []
			for i in friends_ids:
				if not i in followers_ids:
					list_.append(i)
			map(self.api.destroy_friendship, list_)
		except Exception, e:
			print e

	def create(self, id):
		try:
			followers_ids = self.api.followers_ids(id)
			friends_ids = self.api.friends_ids(id)
			list_ = []
			for i in followers_ids:
				if not i in friends_ids:
					list_.append(i)
			map(self.api.create_friendship, list_)
		except Exception, e:
			print e

	def follow(self, id):
		try:
			friends = self.api.friends_ids(id)
			count = 0
			for i in friends:
				if count < 50:
					self.api.create_friendship(i)
				else:
					[time.sleep(18) for i in range(100)]
					count = 0
		except Exception, e:
			print e

Recommended Posts

Try using Tweepy [Python2.7]
Try using Kubernetes Client -Python-
Try python
Try using Pleasant's API (python / FastAPI)
Try using LevelDB in Python (plyvel)
Try using Python argparse's action API
Try using the Python Cmd module
Try using Leap Motion in Python
Try using Amazon DynamoDB from Python
Try using Tkinter
Try using docker-py
Try using cookiecutter
Try using PDFMiner
Start using Python
Try using geopandas
Try using Selenium
Try using scipy
Python> try: / except:
Try using pandas.DataFrame
Try using django-swiftbrowser
Try using tf.metrics
Try using PyODE
Scraping using Python
Try using the Wunderlist API in Python
Try mathematical formulas using Σ with python
Get tweets containing keywords using Python Tweepy
Try using Dialogflow (formerly API.AI) Python SDK #dialogflow
Try using Python with Google Cloud Functions
Try using Junos On-box Python # 2 Commit Script
Try to operate Excel using Python (Xlwings)
Try scraping with Python.
Operate Redmine using Python Redmine
Fibonacci sequence using Python
python try ~ except ~ else
Data analysis using Python 0
Try using the BitFlyer Ligntning API in Python
Try using virtualenv (virtualenvwrapper)
Python: Try using the UI on Pythonista 3 on iPad
Data cleaning using Python
Try using the Python web framework Tornado Part 1
[Azure] Try using Azure Functions
Using Python #external packages
WiringPi-SPI communication using Python
Try gRPC in Python
Try using W & B
Try using Django templates.html
[Kaggle] Try using LGBM
Try using Python's feedparser.
Try using Python's Tkinter
Search Twitter using Python
Try using tensorflow ① Build python environment and introduce tensorflow
Try 9 slices in Python
Try using the Python web framework Tornado Part 2
Name identification using python
Notes using Python subprocesses
Python try / except notes
Try using ChatWork API and Qiita API in Python
Try using the DropBox Core API in Python
Try using Pytorch's collate_fn
[Unity (C #), Python] Try running Python code in Unity using IronPython
Try creating a compressed file using Python and zlib