Compare commits
2 commits
ce3ddc2d0d
...
47635ddd18
Author | SHA1 | Date | |
---|---|---|---|
47635ddd18 | |||
30a0c2a273 |
2 changed files with 38 additions and 5 deletions
25
README.md
Normal file
25
README.md
Normal file
|
@ -0,0 +1,25 @@
|
||||||
|
# Fediverse Nose Ears bot
|
||||||
|
|
||||||
|
Posts the [Nose Ears](https://wuzzy.neocities.org/) comics to the
|
||||||
|
fediverse. It supports comic titles, images and alt texts.
|
||||||
|
|
||||||
|
## Configuration
|
||||||
|
|
||||||
|
To configure this bot, customize the first lines in the `noseears.py`
|
||||||
|
script to your needs, such as by setting the `instance` varible and
|
||||||
|
the `datadir` and `credfile` variables to indicate the position where
|
||||||
|
the bot should store its data and the credentials file
|
||||||
|
respectively. The credentials file can be generated using the
|
||||||
|
`mastodon.log_in` method.
|
||||||
|
|
||||||
|
## Usage
|
||||||
|
|
||||||
|
When supplied with no arguments, `noseears.py` tries to fetch the
|
||||||
|
latest nose ears comic. When supplied with a number as an argument, it
|
||||||
|
tries to fetch the comic with said number, and if "random" is passed
|
||||||
|
as an argument, it tries to fetch a random comic.
|
||||||
|
|
||||||
|
## Copyright
|
||||||
|
|
||||||
|
Copyright © 2022 Antonia <antonia@antonia.is>, Licensed under GPL
|
||||||
|
Version 3 or later.
|
18
noseears.py
18
noseears.py
|
@ -27,16 +27,24 @@ from bs4 import BeautifulSoup
|
||||||
import os
|
import os
|
||||||
from mastodon import Mastodon
|
from mastodon import Mastodon
|
||||||
import re
|
import re
|
||||||
|
import random
|
||||||
|
|
||||||
|
|
||||||
instance = "https://botsin.space"
|
instance = "https://botsin.space"
|
||||||
baseurl = "https://wuzzy.neocities.org"
|
baseurl = "https://wuzzy.neocities.org"
|
||||||
datadir = "data"
|
datadir = "data"
|
||||||
|
credfile= "cred"
|
||||||
cachedir = os.path.join(datadir, "cache")
|
cachedir = os.path.join(datadir, "cache")
|
||||||
seendir = os.path.join(datadir, "seen")
|
seendir = os.path.join(datadir, "seen")
|
||||||
|
|
||||||
|
|
||||||
if len(sys.argv) > 1 :
|
if len(sys.argv) > 1 :
|
||||||
page = request.urlopen(baseurl+"/comic/"+sys.argv[1])
|
n = sys.argv[1]
|
||||||
|
if sys.argv[1] == "random" :
|
||||||
|
# try posting a random comic
|
||||||
|
l = max([int(re.sub("[^0-9]", "", i)) for i in os.listdir(seendir)])
|
||||||
|
n = str(random.randint(1, l))
|
||||||
|
page = request.urlopen(baseurl+"/comic/"+n)
|
||||||
else:
|
else:
|
||||||
page = request.urlopen(baseurl)
|
page = request.urlopen(baseurl)
|
||||||
|
|
||||||
|
@ -60,8 +68,8 @@ if not img :
|
||||||
print("No image found!")
|
print("No image found!")
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
|
number = re.sub("[^0-9]", "", img) # number of comic
|
||||||
permalink = baseurl + "/comic/" + re.sub("[^0-9]", "", img)
|
permalink = baseurl + "/comic/" + number
|
||||||
## try to fetch comic from url
|
## try to fetch comic from url
|
||||||
|
|
||||||
if not os.path.exists(cachedir) :
|
if not os.path.exists(cachedir) :
|
||||||
|
@ -70,7 +78,7 @@ if not os.path.exists(seendir) :
|
||||||
os.makedirs(seendir)
|
os.makedirs(seendir)
|
||||||
|
|
||||||
fname = os.path.join(cachedir,img.split("/")[-1])
|
fname = os.path.join(cachedir,img.split("/")[-1])
|
||||||
seen = os.path.join(seendir,img.split("/")[-1])
|
seen = os.path.join(seendir, number)
|
||||||
if os.path.exists(seen) :
|
if os.path.exists(seen) :
|
||||||
sys.exit(0)
|
sys.exit(0)
|
||||||
|
|
||||||
|
@ -79,7 +87,7 @@ if os.path.exists(seen) :
|
||||||
request.urlretrieve(baseurl+img, fname)
|
request.urlretrieve(baseurl+img, fname)
|
||||||
|
|
||||||
mastodon = Mastodon(
|
mastodon = Mastodon(
|
||||||
access_token="cred",
|
access_token=credfile,
|
||||||
api_base_url=instance
|
api_base_url=instance
|
||||||
)
|
)
|
||||||
md = mastodon.media_post(fname, "image/png", description=alt)
|
md = mastodon.media_post(fname, "image/png", description=alt)
|
||||||
|
|
Loading…
Reference in a new issue