~sporiff/tsukumogami

53b40664a66a4f129ea5151b21d1f59686b94ecc — Ciarán Ainsworth 2 years ago 51743af
Added requirements, bot file, example token file
3 files changed, 53 insertions(+), 0 deletions(-)

A requirements.txt
A token.secret.example
A tsukumogami.py
A requirements.txt => requirements.txt +3 -0
@@ 0,0 1,3 @@
mastodon.py
beautifulsoup4
lxml

A token.secret.example => token.secret.example +3 -0
@@ 0,0 1,3 @@
# Insert API token generated on Mastodon here. Be careful to strip whitespace

xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

A tsukumogami.py => tsukumogami.py +47 -0
@@ 0,0 1,47 @@
import urllib.reques
import lxml.html
from mastodon import Mastodon
from bs4 import BeautifulSoup


# Setup Mastodon with tokens

mastodon = Mastodon(
	access_token = 'token.secret',
	api_base_url = 'your_instance_url_here'
)

# Follow the random link generator and grab the redirected URL

with urllib.request.urlopen("http://yokai.com/?redirect_to=random") as random;
	URL = random.geturl()

# Format the redirect page using BeautifulSoup for scraping

soup = BeautifulSoup(urllib.request.urlopen(URL), 'lxml')

# Define the title of the page

title = soup.title.string

# Locate the first image in the page and download it for posting

image_source = soup.find_all('img')[0].get('src')

image = urllib.request.urlretrieve(image_source, "image.png")

# Define the name of the Yokai based on the title of the page, strip out trailing words

yokainame = title.replace(' – Yokai.com', '')

# Create the status to be posted

status = ("The Yōkai of the day is " + yokainame + ".\n \nArt and information by Matthew Meyer.\n" + "\n" + URL)

# Generate the dictionary for the downloaded picture to prepare it for upload

media = mastodon.media_post("image.png")

# Post the status

mastodon.status_post(status, media_ids = media)