What you can learn
--Part of the basic usage of tweepy. --Example of a program that utilizes tweepy.
What you can't learn
--Beautiful Python design. --Creative usage of tweepy.
twiiter.py
import tweepy
from collections import deque
from threading import Thread
#Minimum preparation before using tweepy
CONSUMER_KEY='XXXXXXX'
CONSUMER_SECRET='XXXXXXXX'
ACCESS_TOKEN='XXXXXXXX'
ACCESS_SECRET='XXXXXXXX'
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_TOKEN, ACCESS_SECRET)
#So far
class Format(object):
"""Class to provide with a variety of formats."""
def __init__(self, tweet):
"""Store some data of tweets in a hash-table. """
self.status = {
'text': tweet.text,
'author': tweet.author.screen_name,
'reply_name': tweet.in_reply_to_screen_name,
'created_at': tweet.created_at,
}
class StreamingFormat(Format):
def format(self):
"""Format tweet text readable with some information."""
author = self.status['author']
created_at = self.status['created_at']
string = '@\033[33m{}\033[0m:{}\n'.format(author, created_at)
string += self.status['text']
string += '\n'
return string
class Memory(deque):
"""Class to have hashed tweet contents."""
def __init__(self, capacity):
"""Define max capacity of self."""
super().__init__()
self.capacity = capacity
def append(self, item):
"""Append unless over capacity."""
if len(self) > self.capacity:
self.popleft
super().append(item)
class Producer(Thread):
"""Class to get tweets and avoid duplication. """
def __init__(self, queue, api, query):
""" Initialize memory to avoid duplication. """
super().__init__(target=self.main)
self.api = api
self.queue = queue
self.query = query
self.memory = Memory(1000)
def main(self):
""" Thread to add tweets to queue. """
while True:
for tweet in self.twigen():
if tweet is not None:
self.queue.put(tweet)
"""
https://syncer.jp/what-is-twitter-api-limit
According to the above site, Twitter takes Tweet
There is a profit limit, and if you access more than that,
You need to be careful because it will be limited.
15 minutes to search from all Tweets
There is a limit of 180 times, so once every 5 seconds
The pace is maximum. You don't have to attack the last minute, so
Set it about every 10 seconds.
"""
time.sleep(10)
def twigen(self):
"""Yield a tweet after hashing the tweet and memorizing it"""
import hashlib
tweets = self.api.search(q=self.query, count=100)
for tweet in tweets:
hashing = hashlib.md5(tweet.text.encode('utf-8'))
hashed_tweet = hashing.hexdigest()
if hashed_tweet in self.memory:
yield None
else:
self.memory.append(hashed_tweet)
yield tweet
class Consumer(Thread):
def __init__(self, queue):
super().__init__(target=self.main)
self.queue = queue
def main(self):
"""Take tweet from queue and print it."""
while True:
q = self.queue.get()
f = StreamingFormat(q)
print(f.format())
time.sleep(1)
class TwitterStreamer(object):
"""Class to print formatted tweet data on console."""
def __init__(self, api, query=None):
from queue import Queue
queue = Queue()
Producer(queue, api, query).start()
Consumer(queue).start()
if __name__ == '__main__':
opts = {
"api":tweepy.API(auth),
"query":'Honey badger'
}
TwitterStreamer(**opts)
The acquisition method of CONSUMER_KEY, CONSUMER_SECRET, ACCESS_TOKEN, ACCESS_SECRET
is omitted. Prepare it because it is absolutely necessary
@decimal1010:2016-04-02 10:36:36
Is Mitsuanaguma a honey badger?
@anaguma86:2016-04-02 09:53:06
A honey badger that gets excited about the notation of bottlenose dolphins instead of bottlenose dolphins.
@kyuurin525:2016-04-02 09:28:04
I opened something and became a honey badger on the 5th page.
@tsube_kii:2016-04-02 08:05:31
I didn't know what kind of animal the Lion Guard was with Kion at all, but the honey badger is an animal ...
When executed, it continues to be output to the console like this.
Recommended Posts