mirror of
https://gitlab.com/oloturia/fumcaso.git
synced 2025-01-09 15:07:19 +01:00
Relative paths (it's possible to run the script from another directory) added comments
This commit is contained in:
parent
88812846ac
commit
6b4dc1b54c
3 changed files with 63 additions and 30 deletions
|
@ -1,19 +1,21 @@
|
||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
from randstrip import createStrip
|
from randstrip import createStrip
|
||||||
from mastodon import Mastodon
|
from mastodon import Mastodon
|
||||||
import os.path
|
impor os
|
||||||
|
|
||||||
|
fileDir = os.path.dirname(os.path.abspath(__file__))
|
||||||
|
fileDir = fileDir +"/"
|
||||||
API_URL = "https://botsin.space"
|
API_URL = "https://botsin.space"
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
with open("createapp") as f:
|
with open(fileDir+"createapp") as f:
|
||||||
createapp = f.readlines()
|
createapp = f.readlines()
|
||||||
createapp = [x.strip() for x in createapp]
|
createapp = [x.strip() for x in createapp]
|
||||||
TOKEN = createapp[0]
|
TOKEN = createapp[0]
|
||||||
mastodon = Mastodon(access_token = TOKEN,api_base_url = API_URL)
|
mastodon = Mastodon(access_token = TOKEN,api_base_url = API_URL)
|
||||||
status = createStrip("mastodon.png")
|
status = createStrip(fileDir+"mastodon.png")
|
||||||
if status == 0:
|
if status == 0:
|
||||||
new_strip = mastodon.media_post("mastodon.png","image/png")
|
new_strip = mastodon.media_post(fileDir+"mastodon.png","image/png")
|
||||||
mastodon.status_post("Nuova striscia",media_ids=new_strip)
|
mastodon.status_post("Nuova striscia",media_ids=new_strip)
|
||||||
else:
|
else:
|
||||||
print("error creating image\n")
|
print("error creating image\n")
|
||||||
|
|
43
randstrip.py
43
randstrip.py
|
@ -4,9 +4,16 @@ from PIL import ImageFont
|
||||||
from PIL import ImageDraw
|
from PIL import ImageDraw
|
||||||
import csv
|
import csv
|
||||||
import random
|
import random
|
||||||
|
import os
|
||||||
|
|
||||||
|
fileDir = os.path.dirname(os.path.abspath(__file__))
|
||||||
|
fileDir = fileDir +"/"
|
||||||
|
|
||||||
|
|
||||||
def replaceText(text):
|
def replaceText(text):
|
||||||
with open("subs.csv") as rtext:
|
"""This function replace $WILDCARD with a word found in subs.csv
|
||||||
|
subs.csv definition is 1st colum $WILDCARD, subsequent columns, possible values (chosen at random), delimiter is ;"""
|
||||||
|
with open(fileDir+"subs.csv") as rtext:
|
||||||
csvReader = csv.reader(rtext,delimiter=";")
|
csvReader = csv.reader(rtext,delimiter=";")
|
||||||
for row in csvReader:
|
for row in csvReader:
|
||||||
if text.find(row[0]) != -1:
|
if text.find(row[0]) != -1:
|
||||||
|
@ -14,7 +21,11 @@ def replaceText(text):
|
||||||
return text
|
return text
|
||||||
|
|
||||||
def fetchText(indText):
|
def fetchText(indText):
|
||||||
with open("rtext.csv") as rtext:
|
"""This function fetch the text for the image with just only one character
|
||||||
|
rtext.csv definition is: 1st column the name of the file (i.e. A001.png), 2nd x-coord, 3rd y-coord, 4th and subsequent, the possible outcomes
|
||||||
|
Delimiter is ; and line feeds @, if there aren't any options, it returns 0 (no text)
|
||||||
|
It returns a tuple (x,y,text)"""
|
||||||
|
with open(fileDir+"rtext.csv") as rtext:
|
||||||
csvReader = csv.reader(rtext,delimiter=';')
|
csvReader = csv.reader(rtext,delimiter=';')
|
||||||
for row in csvReader:
|
for row in csvReader:
|
||||||
if row[0]==indText:
|
if row[0]==indText:
|
||||||
|
@ -24,7 +35,13 @@ def fetchText(indText):
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
def fetch2Text(indText):
|
def fetch2Text(indText):
|
||||||
with open("r2text.csv") as rtext:
|
"""This function fetch the text for the image with two characters
|
||||||
|
rtext.csv definition is: 1st column the name of the file (i.e. B001.png), 2nd x-coord, 3rd y-coord of the first string
|
||||||
|
4th x-coord, 5th y-coord of the second string, 6th and subsequent are the outcomes, alternated as the odd one is an
|
||||||
|
answer to the even one
|
||||||
|
Delimiter is ; and line feeds @, if there aren't any options, it returns 0 (no text)
|
||||||
|
It returns a tuple(x1,y1,x2,y2,text1,text2)"""
|
||||||
|
with open(fileDir+"r2text.csv") as rtext:
|
||||||
csvReader = csv.reader(rtext,delimiter=';')
|
csvReader = csv.reader(rtext,delimiter=';')
|
||||||
for row in csvReader:
|
for row in csvReader:
|
||||||
if row[0]==indText:
|
if row[0]==indText:
|
||||||
|
@ -38,12 +55,15 @@ def fetch2Text(indText):
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
def fetchVign():
|
def fetchVign():
|
||||||
|
"""This functions fetch an image, randomly, chosen from a markov tree defined in ram.csv
|
||||||
|
ram.csv definition is: 1st column the name of the image (without extension), subsequent columns, possible outcomes chosen randomly
|
||||||
|
It returns an array with the file names"""
|
||||||
starts = []
|
starts = []
|
||||||
startdest = []
|
startdest = []
|
||||||
nvign = 0
|
nvign = 0
|
||||||
currVign = "000"
|
currVign = "000"
|
||||||
story = []
|
story = []
|
||||||
with open("ram.csv") as ram:
|
with open(fileDir+"ram.csv") as ram:
|
||||||
csvReader = csv.reader(ram)
|
csvReader = csv.reader(ram)
|
||||||
for row in csvReader:
|
for row in csvReader:
|
||||||
starts.append(row[0])
|
starts.append(row[0])
|
||||||
|
@ -59,7 +79,10 @@ def fetchVign():
|
||||||
return story
|
return story
|
||||||
|
|
||||||
def addThing(indVign):
|
def addThing(indVign):
|
||||||
with open("obj.csv") as obj:
|
"""This function adds a small image (object) to a larger image
|
||||||
|
obj.csv definition is: name of the image (i.e. A001.png), x-coord, y-coord, subsequent columns possible outcomes
|
||||||
|
It returns a tuple (object file name, x, y)"""
|
||||||
|
with open(fileDir+"obj.csv") as obj:
|
||||||
csvReader = csv.reader(obj)
|
csvReader = csv.reader(obj)
|
||||||
for row in csvReader:
|
for row in csvReader:
|
||||||
if row[0] == indVign:
|
if row[0] == indVign:
|
||||||
|
@ -67,12 +90,14 @@ def addThing(indVign):
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
def writeStrip(story):
|
def writeStrip(story):
|
||||||
|
"""This function creates the strip returning an image object that could be saved or viewed. It takes an array with filenames as parameter
|
||||||
|
The first image is always 000, then appends to strip the files, then decorates it fetching text and adding objects"""
|
||||||
strip = []
|
strip = []
|
||||||
for indVign in story:
|
for indVign in story:
|
||||||
if indVign!="000":
|
if indVign!="000":
|
||||||
vign = Image.open(indVign).convert('RGBA')
|
vign = Image.open(fileDir+indVign).convert('RGBA')
|
||||||
addtext = ImageDraw.Draw(vign)
|
addtext = ImageDraw.Draw(vign)
|
||||||
fnt = ImageFont.truetype("ubuntu.ttf",16)
|
fnt = ImageFont.truetype(fileDir+"ubuntu.ttf",16)
|
||||||
if indVign[0] == 'A':
|
if indVign[0] == 'A':
|
||||||
textVign = fetchText(indVign)
|
textVign = fetchText(indVign)
|
||||||
if textVign !=0:
|
if textVign !=0:
|
||||||
|
@ -93,7 +118,7 @@ def writeStrip(story):
|
||||||
addtext.multiline_text((int(textVign[2]),int(textVign[3])),text2,fill="#000000",font=fnt,align="center")
|
addtext.multiline_text((int(textVign[2]),int(textVign[3])),text2,fill="#000000",font=fnt,align="center")
|
||||||
obj = addThing(indVign)
|
obj = addThing(indVign)
|
||||||
if obj!=0:
|
if obj!=0:
|
||||||
objImg = Image.open(obj[0])
|
objImg = Image.open(fileDir+obj[0])
|
||||||
vign.paste(objImg,(int(obj[1]),int(obj[2])))
|
vign.paste(objImg,(int(obj[1]),int(obj[2])))
|
||||||
strip.append(vign)
|
strip.append(vign)
|
||||||
image = Image.new('RGBA',(2400,500))
|
image = Image.new('RGBA',(2400,500))
|
||||||
|
@ -104,6 +129,8 @@ def writeStrip(story):
|
||||||
return image
|
return image
|
||||||
|
|
||||||
def createStrip(name):
|
def createStrip(name):
|
||||||
|
"""Create strip and save it
|
||||||
|
createStrip(str path/filename)"""
|
||||||
try:
|
try:
|
||||||
story = fetchVign()
|
story = fetchVign()
|
||||||
finalStrip = writeStrip(story)
|
finalStrip = writeStrip(story)
|
||||||
|
|
|
@ -2,12 +2,16 @@
|
||||||
from telegram.ext import Updater, CommandHandler
|
from telegram.ext import Updater, CommandHandler
|
||||||
from randstrip import createStrip
|
from randstrip import createStrip
|
||||||
import requests
|
import requests
|
||||||
|
import os
|
||||||
|
|
||||||
|
fileDir = os.path.dirname(os.path.abspath(__file__))
|
||||||
|
fileDir = fileDir +"/"
|
||||||
|
|
||||||
def newStrip(bot, update):
|
def newStrip(bot, update):
|
||||||
status = createStrip("telegram.png")
|
status = createStrip(fileDir+"telegram.png")
|
||||||
if status == 0:
|
if status == 0:
|
||||||
try:
|
try:
|
||||||
bot.send_photo(chat_id=update.message.chat_id,photo=open("telegram.png","rb"))
|
bot.send_photo(chat_id=update.message.chat_id,photo=open(fileDir+"telegram.png","rb"))
|
||||||
except Exception as err:
|
except Exception as err:
|
||||||
print(err)
|
print(err)
|
||||||
else:
|
else:
|
||||||
|
@ -15,7 +19,7 @@ def newStrip(bot, update):
|
||||||
print(status)
|
print(status)
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
with open("token") as token_file:
|
with open(fileDir+"token") as token_file:
|
||||||
content = token_file.readlines()
|
content = token_file.readlines()
|
||||||
token = content[0].strip()
|
token = content[0].strip()
|
||||||
updater = Updater(token)
|
updater = Updater(token)
|
||||||
|
|
Loading…
Reference in a new issue