I tried to make a bot that randomly acquires Wikipedia articles and tweets once a day

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.

Developed Twitter bot

Personally, it can be a little stimulating to my brain in the morning.

Wiki Hitohira

environment

point

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

About getting Wikipedia articles

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)

About tweets on Twitter

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)

About AWS lambda_function

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.

Wiki Hitohira

About AWS Lambda Layer

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

I tried to make a bot that randomly acquires Wikipedia articles and tweets once a day
I tried to make a system that fetches only deleted tweets
I tried to make a translation BOT that works on Discord using googletrans
I tried to make a script that traces the tweets of a specific user on Twitter and saves the posted image at once
I tried to make a periodical process with Selenium and Python
I tried to make a Web API
I tried to make a skill that Alexa will return as cold
I tried to make a ○ ✕ game using TensorFlow
I tried to make a periodical process with CentOS7, Selenium, Python and Chrome
I tried to make a dictionary function that does not distinguish between cases
I tried to make a simple image recognition API with Fast API and Tensorflow
I refactored "I tried to make a script that saves posted images at once by going back to the tweets of a specific user on Twitter".
I tried to make a "fucking big literary converter"
[LPIC 101] I tried to summarize the command options that are easy to make a mistake
[Python + Bottle] I tried to release a web service that visualizes Twitter's positioned tweets.
I tried to make a memo app that can be pomodoro, but a reflection record
A Python beginner made a chat bot, so I tried to summarize how to make it.
It's Cat Day, so I tried to make something that translates into cat-like words.
[1 hour challenge] I tried to make a fortune-telling site that is too suitable with Python
I tried to combine Discord Bot and face recognition-for LT-
I tried to make a generator that generates a C # container class from CSV with Python
I tried to create a bot for PES event notification
I tried to make a stopwatch using tkinter in python
I tried to make GUI tic-tac-toe with Python and Tkinter
I tried to make a simple text editor using PyQt
I tried to make a site that makes it easy to see the update information of Azure
[Graduation from article scattering] I tried to develop a service that can list articles by purpose
I tried to find out the difference between A + = B and A = A + B in Python, so make a note
[Python] I tried to make a simple program that works on the command line using argparse.
[Google Photo & Slack Photo Bot] A story about making a bot that acquires photos in Google Photos and sends them to Slack.
[5th] I tried to make a certain authenticator-like tool with python
I tried to create a server environment that runs on Windows 10
[Python] I made a Line bot that randomly asks English words.
I tried to make an activity that collectively sets location information
[2nd] I tried to make a certain authenticator-like tool with python
I tried to make a regular expression of "amount" using Python
[Python] I tried to implement stable sorting, so make a note
I tried to make a regular expression of "time" using Python
[3rd] I tried to make a certain authenticator-like tool with python
[Python] A memo that I tried to get started with asyncio
I tried to make a 2channel post notification application with Python
[Introduction] I want to make a Mastodon Bot with Python! 【Beginners】
I tried to create Bulls and Cows with a shell program
I tried to make a todo application using bottle with python
[1st] I tried to make a certain authenticator-like tool with python
I tried to extract players and skill names from sports articles
I tried to make a strange quote for Jojo with LSTM
I tried to make a mechanism of exclusive control with Go
I made a wikipedia gacha bot
How to make a slack bot
Python: I tried to make a flat / flat_map just right with a generator
I tried to make a face diagnosis AI for a female professional golfer ①
How to make a rock-paper-scissors bot that can be easily moved (commentary)
Make a Discord Bot that you can search for and paste images
I tried to make a face diagnosis AI for a female professional golfer ②
I tried to make a calculator with Tkinter so I will write it
I tried to make a traffic light-like with Raspberry Pi 4 (Python edition)
I tried to make Kana's handwriting recognition Part 2/3 Data creation and learning
I tried to create a sample to access Salesforce using Python and Bottle
I tried to make a url shortening service serverless with AWS CDK
I want to make a web application using React and Python flask