If you live mainly on the Internet, your information browsing will be biased toward what you are interested in, and you will have less chance to come into contact with things that you are not interested in. So, once a day, I made a Twitter bot that randomly acquires one Wikipedia article and tweets it in order to forcibly input unknown words to myself.
Personally, it can be a little stimulating to my brain in the morning.
--I used the MediaWiki API to randomly get one Wikipedia article. --The once-daily launch was achieved by periodically executing AWS Lambda functions with the Amazon CloudWatch Events cron expression. (Since there are many explanations on how to create functions in AWS Lambda, I will omit the explanation here.) --In order to develop a Twitter bot, you need to register for Twitter App in addition to acquiring an account. (Since there are many explanations about Twitter App, I will omit the explanation here.)
I used the MediaWiki API to get one Wikipedia article at random. It's not good just to get the title of the article, so I first get one article at random and then use the article ID (pageid) to retrieve the content. Twitter Tweet is limited to 140 characters, so the message to Tweet is Article content (opening part) + Link to Wikipedia article It is made to fit in 140 characters in the format. There is a little ingenuity in "making it fit", so I would like to take some time to explain it again.
wiki_random.py
import json
import sys
import urllib.parse
import urllib.request
import os
# Wikipedia API
WIKI_URL = "https://ja.wikipedia.org/w/api.php?"
#Generate parameters for a query that randomly retrieves one article
def set_url_random():
params = {
'action': 'query',
'format': 'json',
'list': 'random', #Get randomly
'rnnamespace': 0, #Specify a standard namespace
'rnlimit': 1 #Set the upper limit of the number of results to 1(Default: 1)
}
return params
#Generate parameters for the query to get the content of the specified article
def set_url_extract(pageid):
params = {
'action': 'query',
'format': 'json',
'prop': 'extracts', #Get the text of the article
'exsentences': 5, #Take out 5 lines
'explaintext': '',
'pageids': pageid #Article ID
}
return params
#Get a random article ID
def get_random_wordid():
try:
request_url = WIKI_URL + urllib.parse.urlencode(set_url_random())
html = urllib.request.urlopen(request_url)
html_json = json.loads(html.read().decode('utf-8'))
pageid = (html_json['query']['random'][0])['id']
except Exception as e:
print ("get_random_word: Exception Error: ", e)
sys.exit(1)
return pageid
#Get the content of the article ID and create a Tweet sentence of 140 characters or less
def get_word_content(pageid):
request_url = WIKI_URL + urllib.parse.urlencode(set_url_extract(pageid))
html = urllib.request.urlopen(request_url)
html_json = json.loads(html.read().decode('utf-8'))
explaintext = html_json['query']['pages'][str(pageid)]['extract']
explaintext = explaintext.splitlines()[0] #Get only the first element if a line break is included
if len(explaintext) > 128:
explaintext = explaintext[0:124] + "..."
explaintext += "\nhttps://ja.wikipedia.org/?curid=" + str(pageid) #twitter has url 11.Specifications that count as 5 characters
return explaintext
if __name__ == '__main__':
pageid = get_random_wordid()
extract = get_word_content(pageid)
print(extract)
I'm using the twitter library.
tweet.py
import os
from twitter import Twitter, OAuth
#Get the key information obtained by Twitter App set in the environment variable
API_KEY = os.environ.get("TWITTER_API_KEY")
API_SECRET_KEY = os.environ.get("TWITTER_API_SECRET_KEY")
ACCESS_TOKEN = os.environ.get("TWITTER_ACCESS_TOKEN")
ACCESS_TOKEN_SECRET = os.environ.get("TWITTER_ACCESS_TOKEN_SECRET")
#Generate Twitter Tweet
def TweetMessage(msg):
t = Twitter(auth = OAuth(ACCESS_TOKEN, ACCESS_TOKEN_SECRET, API_KEY, API_SECRET_KEY))
statusUpdate = t.statuses.update(status=msg)
I will write it for the time being.
lambda_function.py
import wiki_random
import tweet
def lambda_handler(event, context):
pageid = wiki_random.get_random_wordid()
msg = wiki_random.get_word_content(pageid)
tweet.TweetMessage(msg)
Please see here to see what the execution result will look like.
The three Python libraries, requests, requests-oauthlib, and twitter, are not included as standard in AWS Lambda, so I included them using the Layer function. (Since there are many explanations about the Layer function, I will omit the explanation here.)
Recommended Posts