165 lines
3.8 KiB
Python
165 lines
3.8 KiB
Python
import json
|
|
import os
|
|
import ast
|
|
from math import floor
|
|
|
|
import discord
|
|
|
|
|
|
lastCommandFile = "command.last"
|
|
|
|
### reads token files
|
|
def readToken(filePath):
|
|
with open(filePath, 'r') as f:
|
|
return f.readline()
|
|
|
|
|
|
def getStationName():
|
|
#there's a small chance the files don't exist. If they don't we create an empty file.
|
|
try:
|
|
with open("station.name", 'r') as f:
|
|
return f.readline()
|
|
except FileNotFoundError:
|
|
open("station.name", 'w').close()
|
|
return "REPLACEME"
|
|
|
|
def getStationUrl():
|
|
try:
|
|
with open("station.url",'r') as f:
|
|
return f.readline()
|
|
except FileNotFoundError:
|
|
open("station.url", 'w').close()
|
|
return "https://www.youtube.com/watch?v=dQw4w9WgXcQ"
|
|
|
|
|
|
### sends a get request to the sheet
|
|
async def getFromSheet(session, gsheetToken, data):
|
|
|
|
params = {"type":data}
|
|
|
|
async with session.get(gsheetToken, params=params) as response:
|
|
#print("Status:", response.status)
|
|
text = await response.text()
|
|
#print(text)
|
|
return text
|
|
|
|
|
|
### takes a json, returns a http code
|
|
async def postToSheet(session, data, gsheetToken):
|
|
async with session.post(gsheetToken, json=data) as response:
|
|
text = await response.text()
|
|
#print(text)
|
|
#print("Status:", response.status)
|
|
return response.status
|
|
|
|
|
|
### carrier state parser, takes the values from the sheet and turn it into something we can use
|
|
|
|
async def parseCarrierState(state):
|
|
splitState = state.split(",")
|
|
|
|
carrierDict = {}
|
|
|
|
for i in range(0,len(splitState),2):
|
|
carrier = splitState[i]
|
|
state = splitState[i+1]
|
|
if carrier != "" and state != "":
|
|
carrierDict[carrier] = state
|
|
|
|
return carrierDict
|
|
|
|
### converts a dict to the sheet syntax (CSV)
|
|
async def toRawState(carrierDict :dict[str, str]):
|
|
|
|
rawState = []
|
|
|
|
for key,value in carrierDict.items():
|
|
rawState.append([key])
|
|
rawState.append([value])
|
|
|
|
rawLen = len(rawState)
|
|
|
|
for i in range(rawLen, 41):
|
|
rawState.append([""])
|
|
|
|
return rawState
|
|
|
|
|
|
async def getListOfCarriers(state):
|
|
splitState = state.split(",")
|
|
carrierList = []
|
|
for i in range(0,len(splitState),2):
|
|
carrier = splitState[i]
|
|
if carrier != "":
|
|
carrierList.append(carrier)
|
|
|
|
return carrierList
|
|
|
|
### used to query the carrier state internally. Why did I make this????
|
|
async def getInternalCarrierState(session, gsheetToken):
|
|
response = await getFromSheet(session, gsheetToken, "carrier")
|
|
return response
|
|
|
|
|
|
|
|
|
|
###creates a nice progress bar from two values
|
|
async def createProgressBar(a,b):
|
|
#print(a, b)
|
|
ratio = floor(((b-a)/b) * 10)
|
|
|
|
#print(ratio)
|
|
|
|
progressBar = f"{'◼'* ratio}{'◻'*(10-ratio)} {ratio*10}%"
|
|
|
|
return progressBar
|
|
|
|
|
|
|
|
#creates the file if it doesn't exist. stores last command used by... users on this delivery.
|
|
async def writeDeliveryToCommandFile(author, command):
|
|
|
|
#shitty hack to see if the file is empty
|
|
path = os.getcwd() + "/" + lastCommandFile
|
|
empty = os.stat(path).st_size < 1
|
|
|
|
with open(lastCommandFile, 'r') as f:
|
|
if empty:
|
|
jsonContent = json.loads("{}")
|
|
|
|
else:
|
|
jsonContent = json.load(f)
|
|
|
|
|
|
|
|
with open(lastCommandFile, 'w') as f:
|
|
jsonContent[author] = command
|
|
json.dump(jsonContent, f)
|
|
|
|
async def getDataFromDeliveryFile(author):
|
|
path = os.getcwd() + "/" + lastCommandFile
|
|
empty = os.stat(path).st_size < 1
|
|
|
|
with open(lastCommandFile, 'r') as f:
|
|
if empty:
|
|
return None
|
|
|
|
else:
|
|
jsonContent = json.load(f)
|
|
if author in jsonContent.keys():
|
|
return jsonContent[author]
|
|
|
|
else:
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
|
async def delLastCommandFile():
|
|
open(lastCommandFile, 'w').close()
|
|
|
|
|
|
|
|
|