~sporiff/tsukumogami

ref: dc9bce9c253f687411d7cae0e28b32cf4a5eb8af tsukumogami/tsukumogami.py -rw-r--r-- 1.2 KiB
dc9bce9cCiarán Ainsworth Corrected typo in main .py 2 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import urllib.request
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)