We create and operate Twitter BOT on the platform of Google App Engine (hereinafter referred to as GAE) that can be used for free for studying Python. BOT uses Google Alerts to regularly mutter love live news. Account: @ll_news_bot Source: GitHub
I will keep a memorandum of the procedure to operate BOT with GAE SDK for Python.
The final specifications of the BOT created this time are as follows.
Mutter a new Google Alert entry every 30m (* 1) (9: 00 ~ 23: 59)
Mutter Follow Me (18:30)
Follow back every 30m (* 1) (6: 00 ~ 23: 59)
If you only follow every 1h Remove (0: 00 ~ 5: 59)
1 GAE has an instance time, and it does not consume time only during the request processing period. It will be idle so that it can respond immediately after the request is completed, but it also consumes time during that time. Therefore, we try to avoid wasting idle time by muttering and refollowing at the same time.
Create new news from Google Alerts. At this time, set the delivery destination to ** feed **.
I am using the following environment for local development.
GoogleAppEngine Create a new application for BOT from appengine.google.com. The * Application Identifier * entered at this time will be used as the * Application ID * when creating a new application with GAEL launcher.
We used the following third-party modules for BOT development. There are two types of modules, a Python wrapper for the Twitter API and an RSS feed parser.
The above module must be deployed on GAE along with the web application, so After installing or building the module, I had to copy it into my project.
The functions of the created BOT are largely "Create a mutter from an RSS feed" and "Operation of Twitter API", so we will create a module for each of these processes.
This module uses the * feedparser * module to parse RSS feeds and create muttering content. We regularly check for new entries, so we use * DataStore * to avoid duplicate muttering.
class FeedFetcher(object):
'''Class for parsing RSS feeds for Google Alerts'''
def fetch_new_entries(self):
'''Get new entries from the Google Alerts RSS feed.'''
#abridgement
return new_entory
class Entry(object):
'''Class for storing parsed entry information'''
def make_tweets(self):
'''Create muttering content'''
return #Muttering
#How to use
fetcher = FeedFetcher()
new_entries = fetcher. fetch_new_entries()
for entry in new_entries:
print entry.make_tweet()
In this module, I wrap the python-twitter module so that I can easily hit the required API. In addition, the Twitter authentication information obtained at the time of preparation is passed to the python-twitter API. This is reference, and when using python-twitter with GAE, it is not cached. ..
class TwitterBot(object):
'''Classes for muttering to Twitter, refollowing and unfollowing users'''
def tweet(self, message):
'''Mumble the target content to Twitter'''
#abridgement
def get_not_follow_users(self):
'''Get the ID list of unfollowed users'''
return #Unfollowed user ID list
def refollow(self, not_follow_id):
'''Follow the target user'''
#abridgement
# ...abridgement
#How to use
bot = TwitterBot()
fetcher = FeedFetcher()
bot.tweet(fetcher. fetch_new_entries[0].make_tweet)
bot.refollow(bot. get_not_follow_users()[0])
First, create a * app.yaml * file for URL mapping. If login is admin, even if you hit the URL on the browser, the request will not be sent unless you authenticate your account. In script, write the * WSGIApplication * object name that maps the module name and URL.
application: ll-news-bot
version: 8
runtime: python27
api_version: 1
threadsafe: true
handlers:
- url: /
script: tweet.app
login: admin
- url: /tweet
script: tweet.app
On the python side, create a class that inherits * RequestHandler * and write the handle processing of the get or post method. I don't call the Twitter API in large numbers at once, but I use the task queue for studying. When the task is executed, a POST method request is sent to the specified URL.
tweet.py
class MainHandler(webapp2.RequestHandler):
def get(self):
taskqueue.add(queue_name='tweet', url='/tweet', params={'message': u'test'})
class TweetHandler(webapp2.RequestHandler):
def post(self):
#Tweet processing
bot = TwitterBot()
bot.tweet(self.request.get('message')
app = webapp2.WSGIApplication([('/', MainHandler), ('/tweet', TweetHandler)], true)
To use the task queue, create * queue.yaml *. The following site was very helpful for the explanation about the task queue.
app.yaml
queue:
- name: tweet
rate: 1/s
bucket_size: 1
retry_parameters:
task_retry_limit: 0
Finally, create a * cron.yaml * file to run this process on a regular basis. Use * schedule * to set how often to execute.
cron.yaml
cron:
- description: tweet task
url: /
schedule: every 30 minutes from 9:00 to 23:59
timezone: Asia/Tokyo
Use the GAELaucher app and check the operation of the app in your local environment. You can create it from the Launcher app. At that time, use the * Application Identifier * of the application created in advance for the Application ID.
You need to run * Make Symlink * from the menu before running. Also, if the port number used by GAE is used by another app, you need to change either port number.
Finally, use GAE Launcher to deploy the application to Google's servers. At that time, you will be asked to enter an account, so enter the Google account that corresponds to the application created in advance.
I sometimes see * Checking if deployment succeeded. * Endlessly during deployment, which seems to be a server-side issue. Finally, you will be asked to upgrade the version and redeploy or roll back, so this time I upgraded the version and redeployed and it worked. The above is the flow from the development of Twitter BOT to its operation.
Recommended Posts