diff --git a/config.json b/config.json new file mode 100644 index 0000000..e871e2a --- /dev/null +++ b/config.json @@ -0,0 +1,23 @@ +{ + "defaultProfile": "oloturia", + + "oloturia": { + "saveLocation": "/var/tmp", + "filename": "/image.png", + "imagesLocation": "./oloturia", + "csvLocation": "./oloturia", + "font": "./ubuntu.ttf", + "twitter": { + "token": "./twitter_token", + "filename": "/twitter.png" + }, + "mastodon": { + "token": "./mastodon_token", + "filename": "/mastodon.png" + }, + "telegram": { + "token": "./telegram_token", + "filename": "/telegram.png" + } + } +} diff --git a/mastodon_main.py b/mastodon_main.py index 6b2dafa..2cabc73 100755 --- a/mastodon_main.py +++ b/mastodon_main.py @@ -1,5 +1,5 @@ #!/usr/bin/env python3 -from randstrip import createStrip +from randstrip import createStrip,readConfig from mastodon import Mastodon import os @@ -8,17 +8,19 @@ fileDir = fileDir +"/" API_URL = "https://botsin.space" if __name__ == "__main__": - with open(fileDir+"mastodon_token") as f: + config = readConfig(platform="mastodon") + + with open(config["token"]) as f: createapp = f.readlines() createapp = [x.strip() for x in createapp] TOKEN = createapp[0] mastodon = Mastodon(access_token = TOKEN,api_base_url = API_URL) - status = createStrip("mastodon.png") + status = createStrip(config) if status == 0: published = False for i in range(1,100): try: - new_strip = mastodon.media_post(fileDir+"mastodon.png","image/png") + new_strip = mastodon.media_post(config["saveLocation"]+config["filename"],"image/png") mastodon.status_post("Nuova striscia",media_ids=new_strip) published = True except: diff --git a/A00.png b/oloturia/A00.png similarity index 100% rename from A00.png rename to oloturia/A00.png diff --git a/A01.png b/oloturia/A01.png similarity index 100% rename from A01.png rename to oloturia/A01.png diff --git a/A02.png b/oloturia/A02.png similarity index 100% rename from A02.png rename to oloturia/A02.png diff --git a/A03.png b/oloturia/A03.png similarity index 100% rename from A03.png rename to oloturia/A03.png diff --git a/A04.png b/oloturia/A04.png similarity index 100% rename from A04.png rename to oloturia/A04.png diff --git a/A05.png b/oloturia/A05.png similarity index 100% rename from A05.png rename to oloturia/A05.png diff --git a/A06.png b/oloturia/A06.png similarity index 100% rename from A06.png rename to oloturia/A06.png diff --git a/A07.png b/oloturia/A07.png similarity index 100% rename from A07.png rename to oloturia/A07.png diff --git a/A08.png b/oloturia/A08.png similarity index 100% rename from A08.png rename to oloturia/A08.png diff --git a/A09.png b/oloturia/A09.png similarity index 100% rename from A09.png rename to oloturia/A09.png diff --git a/A10.png b/oloturia/A10.png similarity index 100% rename from A10.png rename to oloturia/A10.png diff --git a/A11.png b/oloturia/A11.png similarity index 100% rename from A11.png rename to oloturia/A11.png diff --git a/A12.png b/oloturia/A12.png similarity index 100% rename from A12.png rename to oloturia/A12.png diff --git a/A13.png b/oloturia/A13.png similarity index 100% rename from A13.png rename to oloturia/A13.png diff --git a/A14.png b/oloturia/A14.png similarity index 100% rename from A14.png rename to oloturia/A14.png diff --git a/A15.png b/oloturia/A15.png similarity index 100% rename from A15.png rename to oloturia/A15.png diff --git a/A16.png b/oloturia/A16.png similarity index 100% rename from A16.png rename to oloturia/A16.png diff --git a/A17.png b/oloturia/A17.png similarity index 100% rename from A17.png rename to oloturia/A17.png diff --git a/A18.png b/oloturia/A18.png similarity index 100% rename from A18.png rename to oloturia/A18.png diff --git a/A19.png b/oloturia/A19.png similarity index 100% rename from A19.png rename to oloturia/A19.png diff --git a/A20.png b/oloturia/A20.png similarity index 100% rename from A20.png rename to oloturia/A20.png diff --git a/A21.png b/oloturia/A21.png similarity index 100% rename from A21.png rename to oloturia/A21.png diff --git a/A22.png b/oloturia/A22.png similarity index 100% rename from A22.png rename to oloturia/A22.png diff --git a/A23.png b/oloturia/A23.png similarity index 100% rename from A23.png rename to oloturia/A23.png diff --git a/A24.png b/oloturia/A24.png similarity index 100% rename from A24.png rename to oloturia/A24.png diff --git a/A25.png b/oloturia/A25.png similarity index 100% rename from A25.png rename to oloturia/A25.png diff --git a/A26.png b/oloturia/A26.png similarity index 100% rename from A26.png rename to oloturia/A26.png diff --git a/A27.png b/oloturia/A27.png similarity index 100% rename from A27.png rename to oloturia/A27.png diff --git a/A28.png b/oloturia/A28.png similarity index 100% rename from A28.png rename to oloturia/A28.png diff --git a/A29.png b/oloturia/A29.png similarity index 100% rename from A29.png rename to oloturia/A29.png diff --git a/A30.png b/oloturia/A30.png similarity index 100% rename from A30.png rename to oloturia/A30.png diff --git a/A31.png b/oloturia/A31.png similarity index 100% rename from A31.png rename to oloturia/A31.png diff --git a/A32.png b/oloturia/A32.png similarity index 100% rename from A32.png rename to oloturia/A32.png diff --git a/ART00.png b/oloturia/ART00.png similarity index 100% rename from ART00.png rename to oloturia/ART00.png diff --git a/ART01.png b/oloturia/ART01.png similarity index 100% rename from ART01.png rename to oloturia/ART01.png diff --git a/ART02.png b/oloturia/ART02.png similarity index 100% rename from ART02.png rename to oloturia/ART02.png diff --git a/ART03.png b/oloturia/ART03.png similarity index 100% rename from ART03.png rename to oloturia/ART03.png diff --git a/ARTO01.png b/oloturia/ARTO01.png similarity index 100% rename from ARTO01.png rename to oloturia/ARTO01.png diff --git a/ARTO02.png b/oloturia/ARTO02.png similarity index 100% rename from ARTO02.png rename to oloturia/ARTO02.png diff --git a/ARTO03.png b/oloturia/ARTO03.png similarity index 100% rename from ARTO03.png rename to oloturia/ARTO03.png diff --git a/ARTO04.png b/oloturia/ARTO04.png similarity index 100% rename from ARTO04.png rename to oloturia/ARTO04.png diff --git a/ARTO05.png b/oloturia/ARTO05.png similarity index 100% rename from ARTO05.png rename to oloturia/ARTO05.png diff --git a/ARTO06.png b/oloturia/ARTO06.png similarity index 100% rename from ARTO06.png rename to oloturia/ARTO06.png diff --git a/ARTO07.png b/oloturia/ARTO07.png similarity index 100% rename from ARTO07.png rename to oloturia/ARTO07.png diff --git a/ARTO08.png b/oloturia/ARTO08.png similarity index 100% rename from ARTO08.png rename to oloturia/ARTO08.png diff --git a/ARTO09.png b/oloturia/ARTO09.png similarity index 100% rename from ARTO09.png rename to oloturia/ARTO09.png diff --git a/ARTO10.png b/oloturia/ARTO10.png similarity index 100% rename from ARTO10.png rename to oloturia/ARTO10.png diff --git a/B00.0.png b/oloturia/B00.0.png similarity index 100% rename from B00.0.png rename to oloturia/B00.0.png diff --git a/B00.1.png b/oloturia/B00.1.png similarity index 100% rename from B00.1.png rename to oloturia/B00.1.png diff --git a/B00.2.png b/oloturia/B00.2.png similarity index 100% rename from B00.2.png rename to oloturia/B00.2.png diff --git a/B00.3.png b/oloturia/B00.3.png similarity index 100% rename from B00.3.png rename to oloturia/B00.3.png diff --git a/B01.png b/oloturia/B01.png similarity index 100% rename from B01.png rename to oloturia/B01.png diff --git a/B02.png b/oloturia/B02.png similarity index 100% rename from B02.png rename to oloturia/B02.png diff --git a/B03.png b/oloturia/B03.png similarity index 100% rename from B03.png rename to oloturia/B03.png diff --git a/B04.png b/oloturia/B04.png similarity index 100% rename from B04.png rename to oloturia/B04.png diff --git a/B05.png b/oloturia/B05.png similarity index 100% rename from B05.png rename to oloturia/B05.png diff --git a/B06.png b/oloturia/B06.png similarity index 100% rename from B06.png rename to oloturia/B06.png diff --git a/B07.png b/oloturia/B07.png similarity index 100% rename from B07.png rename to oloturia/B07.png diff --git a/B08.png b/oloturia/B08.png similarity index 100% rename from B08.png rename to oloturia/B08.png diff --git a/B09.0.png b/oloturia/B09.0.png similarity index 100% rename from B09.0.png rename to oloturia/B09.0.png diff --git a/B09.1.png b/oloturia/B09.1.png similarity index 100% rename from B09.1.png rename to oloturia/B09.1.png diff --git a/B10.png b/oloturia/B10.png similarity index 100% rename from B10.png rename to oloturia/B10.png diff --git a/B11.png b/oloturia/B11.png similarity index 100% rename from B11.png rename to oloturia/B11.png diff --git a/B12.png b/oloturia/B12.png similarity index 100% rename from B12.png rename to oloturia/B12.png diff --git a/B13.png b/oloturia/B13.png similarity index 100% rename from B13.png rename to oloturia/B13.png diff --git a/B14.png b/oloturia/B14.png similarity index 100% rename from B14.png rename to oloturia/B14.png diff --git a/B15.png b/oloturia/B15.png similarity index 100% rename from B15.png rename to oloturia/B15.png diff --git a/B16.png b/oloturia/B16.png similarity index 100% rename from B16.png rename to oloturia/B16.png diff --git a/B20.png b/oloturia/B20.png similarity index 100% rename from B20.png rename to oloturia/B20.png diff --git a/B21.0.png b/oloturia/B21.0.png similarity index 100% rename from B21.0.png rename to oloturia/B21.0.png diff --git a/B21.1.png b/oloturia/B21.1.png similarity index 100% rename from B21.1.png rename to oloturia/B21.1.png diff --git a/B21.2.png b/oloturia/B21.2.png similarity index 100% rename from B21.2.png rename to oloturia/B21.2.png diff --git a/B22.png b/oloturia/B22.png similarity index 100% rename from B22.png rename to oloturia/B22.png diff --git a/B23.png b/oloturia/B23.png similarity index 100% rename from B23.png rename to oloturia/B23.png diff --git a/B24.png b/oloturia/B24.png similarity index 100% rename from B24.png rename to oloturia/B24.png diff --git a/B25.0.png b/oloturia/B25.0.png similarity index 100% rename from B25.0.png rename to oloturia/B25.0.png diff --git a/B25.1.png b/oloturia/B25.1.png similarity index 100% rename from B25.1.png rename to oloturia/B25.1.png diff --git a/B26.0.png b/oloturia/B26.0.png similarity index 100% rename from B26.0.png rename to oloturia/B26.0.png diff --git a/B26.1.png b/oloturia/B26.1.png similarity index 100% rename from B26.1.png rename to oloturia/B26.1.png diff --git a/B27.png b/oloturia/B27.png similarity index 100% rename from B27.png rename to oloturia/B27.png diff --git a/B28.png b/oloturia/B28.png similarity index 100% rename from B28.png rename to oloturia/B28.png diff --git a/B29.png b/oloturia/B29.png similarity index 100% rename from B29.png rename to oloturia/B29.png diff --git a/B30.png b/oloturia/B30.png similarity index 100% rename from B30.png rename to oloturia/B30.png diff --git a/B31.png b/oloturia/B31.png similarity index 100% rename from B31.png rename to oloturia/B31.png diff --git a/B32.png b/oloturia/B32.png similarity index 100% rename from B32.png rename to oloturia/B32.png diff --git a/C01.png b/oloturia/C01.png similarity index 100% rename from C01.png rename to oloturia/C01.png diff --git a/C02.png b/oloturia/C02.png similarity index 100% rename from C02.png rename to oloturia/C02.png diff --git a/C03.png b/oloturia/C03.png similarity index 100% rename from C03.png rename to oloturia/C03.png diff --git a/C04.png b/oloturia/C04.png similarity index 100% rename from C04.png rename to oloturia/C04.png diff --git a/C05.png b/oloturia/C05.png similarity index 100% rename from C05.png rename to oloturia/C05.png diff --git a/C06.png b/oloturia/C06.png similarity index 100% rename from C06.png rename to oloturia/C06.png diff --git a/O00.0.png b/oloturia/O00.0.png similarity index 100% rename from O00.0.png rename to oloturia/O00.0.png diff --git a/O00.1.png b/oloturia/O00.1.png similarity index 100% rename from O00.1.png rename to oloturia/O00.1.png diff --git a/O00.2.png b/oloturia/O00.2.png similarity index 100% rename from O00.2.png rename to oloturia/O00.2.png diff --git a/O00.3.png b/oloturia/O00.3.png similarity index 100% rename from O00.3.png rename to oloturia/O00.3.png diff --git a/O01.png b/oloturia/O01.png similarity index 100% rename from O01.png rename to oloturia/O01.png diff --git a/O02.png b/oloturia/O02.png similarity index 100% rename from O02.png rename to oloturia/O02.png diff --git a/O04.png b/oloturia/O04.png similarity index 100% rename from O04.png rename to oloturia/O04.png diff --git a/O05.png b/oloturia/O05.png similarity index 100% rename from O05.png rename to oloturia/O05.png diff --git a/OBJ00.png b/oloturia/OBJ00.png similarity index 100% rename from OBJ00.png rename to oloturia/OBJ00.png diff --git a/OBJ01.png b/oloturia/OBJ01.png similarity index 100% rename from OBJ01.png rename to oloturia/OBJ01.png diff --git a/OBJ02.png b/oloturia/OBJ02.png similarity index 100% rename from OBJ02.png rename to oloturia/OBJ02.png diff --git a/OBJ03.png b/oloturia/OBJ03.png similarity index 100% rename from OBJ03.png rename to oloturia/OBJ03.png diff --git a/OBJ04.png b/oloturia/OBJ04.png similarity index 100% rename from OBJ04.png rename to oloturia/OBJ04.png diff --git a/OBJ05.png b/oloturia/OBJ05.png similarity index 100% rename from OBJ05.png rename to oloturia/OBJ05.png diff --git a/OBJ06.png b/oloturia/OBJ06.png similarity index 100% rename from OBJ06.png rename to oloturia/OBJ06.png diff --git a/OBJ07.png b/oloturia/OBJ07.png similarity index 100% rename from OBJ07.png rename to oloturia/OBJ07.png diff --git a/OBJ08.png b/oloturia/OBJ08.png similarity index 100% rename from OBJ08.png rename to oloturia/OBJ08.png diff --git a/OBJ09.png b/oloturia/OBJ09.png similarity index 100% rename from OBJ09.png rename to oloturia/OBJ09.png diff --git a/OBJ10.png b/oloturia/OBJ10.png similarity index 100% rename from OBJ10.png rename to oloturia/OBJ10.png diff --git a/OBJ11.png b/oloturia/OBJ11.png similarity index 100% rename from OBJ11.png rename to oloturia/OBJ11.png diff --git a/OBJ12.png b/oloturia/OBJ12.png similarity index 100% rename from OBJ12.png rename to oloturia/OBJ12.png diff --git a/OBJ13.png b/oloturia/OBJ13.png similarity index 100% rename from OBJ13.png rename to oloturia/OBJ13.png diff --git a/OBJ14.png b/oloturia/OBJ14.png similarity index 100% rename from OBJ14.png rename to oloturia/OBJ14.png diff --git a/OBJ15.png b/oloturia/OBJ15.png similarity index 100% rename from OBJ15.png rename to oloturia/OBJ15.png diff --git a/OBJ16.png b/oloturia/OBJ16.png similarity index 100% rename from OBJ16.png rename to oloturia/OBJ16.png diff --git a/Q00.png b/oloturia/Q00.png similarity index 100% rename from Q00.png rename to oloturia/Q00.png diff --git a/Q01.png b/oloturia/Q01.png similarity index 100% rename from Q01.png rename to oloturia/Q01.png diff --git a/Q02.0.png b/oloturia/Q02.0.png similarity index 100% rename from Q02.0.png rename to oloturia/Q02.0.png diff --git a/Q02.1.png b/oloturia/Q02.1.png similarity index 100% rename from Q02.1.png rename to oloturia/Q02.1.png diff --git a/Q03.png b/oloturia/Q03.png similarity index 100% rename from Q03.png rename to oloturia/Q03.png diff --git a/Q04.png b/oloturia/Q04.png similarity index 100% rename from Q04.png rename to oloturia/Q04.png diff --git a/Q05.png b/oloturia/Q05.png similarity index 100% rename from Q05.png rename to oloturia/Q05.png diff --git a/Q06.png b/oloturia/Q06.png similarity index 100% rename from Q06.png rename to oloturia/Q06.png diff --git a/Q07.png b/oloturia/Q07.png similarity index 100% rename from Q07.png rename to oloturia/Q07.png diff --git a/Q08.png b/oloturia/Q08.png similarity index 100% rename from Q08.png rename to oloturia/Q08.png diff --git a/Q09.png b/oloturia/Q09.png similarity index 100% rename from Q09.png rename to oloturia/Q09.png diff --git a/Q10.png b/oloturia/Q10.png similarity index 100% rename from Q10.png rename to oloturia/Q10.png diff --git a/Q11.png b/oloturia/Q11.png similarity index 100% rename from Q11.png rename to oloturia/Q11.png diff --git a/Q12.png b/oloturia/Q12.png similarity index 100% rename from Q12.png rename to oloturia/Q12.png diff --git a/Q13.png b/oloturia/Q13.png similarity index 100% rename from Q13.png rename to oloturia/Q13.png diff --git a/Q14.png b/oloturia/Q14.png similarity index 100% rename from Q14.png rename to oloturia/Q14.png diff --git a/Q15.png b/oloturia/Q15.png similarity index 100% rename from Q15.png rename to oloturia/Q15.png diff --git a/Q16.png b/oloturia/Q16.png similarity index 100% rename from Q16.png rename to oloturia/Q16.png diff --git a/Q17.png b/oloturia/Q17.png similarity index 100% rename from Q17.png rename to oloturia/Q17.png diff --git a/Q18.png b/oloturia/Q18.png similarity index 100% rename from Q18.png rename to oloturia/Q18.png diff --git a/Q19.png b/oloturia/Q19.png similarity index 100% rename from Q19.png rename to oloturia/Q19.png diff --git a/Q20.png b/oloturia/Q20.png similarity index 100% rename from Q20.png rename to oloturia/Q20.png diff --git a/Q21.png b/oloturia/Q21.png similarity index 100% rename from Q21.png rename to oloturia/Q21.png diff --git a/Q22.png b/oloturia/Q22.png similarity index 100% rename from Q22.png rename to oloturia/Q22.png diff --git a/Q23.png b/oloturia/Q23.png similarity index 100% rename from Q23.png rename to oloturia/Q23.png diff --git a/U00.png b/oloturia/U00.png similarity index 100% rename from U00.png rename to oloturia/U00.png diff --git a/obj.csv b/oloturia/obj.csv similarity index 100% rename from obj.csv rename to oloturia/obj.csv diff --git a/ram.csv b/oloturia/ram.csv similarity index 100% rename from ram.csv rename to oloturia/ram.csv diff --git a/rtext.csv b/oloturia/rtext.csv similarity index 100% rename from rtext.csv rename to oloturia/rtext.csv diff --git a/subs.csv b/oloturia/subs.csv similarity index 100% rename from subs.csv rename to oloturia/subs.csv diff --git a/randstrip.py b/randstrip.py index 7967060..9ad530a 100755 --- a/randstrip.py +++ b/randstrip.py @@ -5,29 +5,29 @@ from PIL import ImageDraw import csv import random import os +import json fileDir = os.path.dirname(os.path.abspath(__file__)) -fileDir = fileDir +"/" -def replaceText(text): +def replaceText(text,config): """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 subs: + with open(config["csvLocation"]+"/"+"subs.csv") as subs: csvReader = csv.reader(subs,delimiter=";") for row in csvReader: if text.find(row[0]) != -1: text = text.replace(row[0],row[random.randint(1,len(row)-1)],1) return text -def fetchText(indText): +def fetchText(indText,config): """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 number of actors (at the moment they are limited to two; then a couple of columns or each actor with x and y coord of the strings; after the coords the outcomes, one column for each actor Delimiter is ; and line feeds @, if there aren't any options, it returns 0 (no text) It returns two arrays, coords is a tuple (x,y) and result is the outcome""" - with open(fileDir+"rtext.csv") as rtext: + with open(config["csvLocation"]+"/rtext.csv") as rtext: csvReader = csv.reader(rtext,delimiter=';') for row in csvReader: if row[0]==indText: @@ -46,7 +46,7 @@ def fetchText(indText): return coords,result -def fetchVign(): +def fetchVign(config): """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""" @@ -55,7 +55,7 @@ def fetchVign(): nvign = 0 currVign = "000" story = [] - with open(fileDir+"ram.csv") as ram: + with open(config["csvLocation"]+"/ram.csv") as ram: csvReader = csv.reader(ram) for row in csvReader: starts.append(row[0]) @@ -70,44 +70,44 @@ def fetchVign(): print("tree with no END") quit() -def addThing(indVign): +def addThing(indVign,config): """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: + with open(config["csvLocation"]+"/obj.csv") as obj: csvReader = csv.reader(obj) for row in csvReader: if row[0] == indVign: return row[random.randint(3,len(row)-1)],row[1],row[2] return 0 -def writeStrip(story,fontSize): +def writeStrip(story,fontSize,config): """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. If the object is an R, then repeats the last object.""" strip = [] for indVign in story: try: - vign = Image.open(fileDir+indVign).convert('RGBA') + vign = Image.open(config["imagesLocation"]+"/"+indVign).convert('RGBA') addtext = ImageDraw.Draw(vign) - fnt = ImageFont.truetype(fileDir+"ubuntu.ttf",fontSize) - textVign = fetchText(indVign) + fnt = ImageFont.truetype(config["font"],fontSize) + textVign = fetchText(indVign,config) if textVign!=0: for x in range(len(textVign[0])): text_vign = textVign[1][x] while text_vign.find('$') != -1: - text_vign = replaceText(text_vign) + text_vign = replaceText(text_vign,config) text_vign = text_vign.replace('@','\n') addtext.multiline_text((int(textVign[0][x][0]),int(textVign[0][x][1])),text_vign,fill="#000000",font=fnt,align="center") - obj = addThing(indVign) + obj = addThing(indVign,config) if obj!=0: if obj[0] == 'R': - objImg = Image.open(fileDir+prevObj[0]) + objImg = Image.open(config["imagesLocation"]+"/"+prevObj[0]) else: prevObj = obj - objImg = Image.open(fileDir+obj[0]) + objImg = Image.open(config["imagesLocation"]+"/"+obj[0]) vign.paste(objImg,(int(obj[1]),int(obj[2]))) strip.append(vign) except FileNotFoundError: @@ -119,38 +119,75 @@ def writeStrip(story,fontSize): xshift += 600 return image -def createStrip(name,fontSize=22): +def createStrip(config,specialPlatform="",fontSize=22): """Create strip and save it createStrip(str path/filename)""" try: - story = fetchVign() - finalStrip = writeStrip(story,fontSize) - if name == "android": + story = fetchVign(config) + finalStrip = writeStrip(story,fontSize,config) + if specialPlatform == "android": return finalStrip else: - finalStrip.save(fileDir+name) + finalStrip.save(config["saveLocation"]+config["filename"]) return 0 except Exception as err: return err +def readConfig(profile=False,platform=False): + """Read configuration file""" + try: + with open(fileDir+"/config.json") as f: + config = json.load(f) + except IOError: + print("config.json not found") + return False + if not(profile): + profile = config["defaultProfile"] + else: + profile = profile[0] + try: + checkProfile = config[profile] + except KeyError: + print("Profile not found") + quit() + saveLocation = checkLocal(config[profile]["saveLocation"]) + imagesLocation = checkLocal(config[profile]["imagesLocation"]) + csvLocation = checkLocal(config[profile]["csvLocation"]) + font = checkLocal(config[profile]["font"]) + if platform: + token = checkLocal(config[profile][platform]["token"]) + filename = checkLocal(config[profile][platform]["filename"]) + return {"saveLocation":saveLocation,"imagesLocation":imagesLocation,"csvLocation":csvLocation,"font":font,"token":token,"filename":filename} + filename = config[profile]["filename"] + return {"saveLocation":saveLocation,"imagesLocation":imagesLocation,"csvLocation":csvLocation,"font":font,"filename":filename} + +def checkLocal(directory): + """Checks if it's a relative or absolute path""" + if directory[0] == ".": + return fileDir + directory[1:] + else: + return directory + + if __name__ == "__main__": import argparse parser = argparse.ArgumentParser() parser.add_argument('-s','--story',metavar='story',default='',nargs=4,help='name of the images') parser.add_argument('-m','--multiple',metavar='multiple',default=[1],nargs=1,type=int,help='multiple output (int >0)') parser.add_argument('-x','--xsize',metavar='xsize',default=0,type=int,nargs=1,help='resize image x') + parser.add_argument('-p','--profile',metavar='profile',default="",type=str,nargs=1,help='profile') args = parser.parse_args() if args.multiple[0] <= 0: quit() + config = readConfig(profile=args.profile) for x in range(0,args.multiple[0]): if (args.story == ''): - story = fetchVign() + story = fetchVign(config) else: story = [] for x in args.story: story.append(x) - print(story) - finalStrip = writeStrip(story,22) + finalStrip = writeStrip(story,22,config) if args.multiple[0] == 1: if args.xsize == 0: finalStrip.show() diff --git a/telegram_main.py b/telegram_main.py index b5b76ad..941e8da 100755 --- a/telegram_main.py +++ b/telegram_main.py @@ -1,6 +1,6 @@ #!/usr/bin/env python3 from telegram.ext import Updater, CommandHandler -from randstrip import createStrip +from randstrip import createStrip,readConfig import requests import os @@ -8,10 +8,11 @@ fileDir = os.path.dirname(os.path.abspath(__file__)) fileDir = fileDir +"/" def newStrip(bot, update): - status = createStrip("telegram.png") + config = readConfig(platform="telegram") + status = createStrip(config) if status == 0: try: - bot.send_photo(chat_id=update.message.chat_id,photo=open(fileDir+"telegram.png","rb")) + bot.send_photo(chat_id=update.message.chat_id,photo=open(config["saveLocation"]+config["filename"],"rb")) except Exception as err: print(err) else: @@ -19,7 +20,8 @@ def newStrip(bot, update): print(status) if __name__ == "__main__": - with open(fileDir+"telegram_token") as token_file: + config = readConfig(platform="telegram") + with open(config["token"]) as token_file: content = token_file.readlines() token = content[0].strip() updater = Updater(token) diff --git a/twitter_main.py b/twitter_main.py index c3c5cfe..0277fd3 100755 --- a/twitter_main.py +++ b/twitter_main.py @@ -1,15 +1,16 @@ #!/usr/bin/env python3 import tweepy -from randstrip import createStrip +from randstrip import createStrip,readConfig import os fileDir = os.path.dirname(os.path.abspath(__file__)) fileDir = fileDir +"/" if __name__ == "__main__": - status = createStrip("twitter.png") + config = readConfig(platform="twitter") + status = createStrip(config) if status == 0: - with open(fileDir+"twitter_token") as f: + with open(config["token"]) as f: tokens = f.readlines() tokens = [x.strip() for x in tokens] auth = tweepy.OAuthHandler(tokens[0],tokens[1]) @@ -19,7 +20,7 @@ if __name__ == "__main__": for i in range(0,100): try: api.verify_credentials() - api.update_with_media(fileDir+"twitter.png","Generatore automatico di strip. Striscia di oggi.") + api.update_with_media(config["saveLocation"]+config["filename"],"Generatore automatico di strip. Striscia di oggi.") published = True except: continue