1
0
Fork 0
mirror of https://github.com/myned/modufur.git synced 2024-12-24 14:27:27 +00:00

autopep8, aiohttp session methods

This commit is contained in:
Myned 2017-10-12 22:30:07 -04:00
parent 8f304893e3
commit af9ed570a2

View file

@ -1,26 +1,40 @@
import asyncio
import json import json
import pickle as pkl
try: import aiohttp as aio
with open('background.json') as infile:
background = json.load(infile)
print('\"background.json\" loaded.') def setdefault(filename, default=None):
except FileNotFoundError: try:
with open('background.json', 'w+') as iofile: with open(filename, 'rb') as infile:
print('Background file not found: \"background.json\" created and loaded.') print('\"{}\" loaded.'.format(filename))
json.dump({}, iofile, indent=4, sort_keys=True) return pkl.load(infile)
iofile.seek(0) except FileNotFoundError:
background = json.load(iofile) with open(filename, 'wb+') as iofile:
print('File not found: \"{}\" created and loaded with default values.'.format(filename))
pkl.dump(default, iofile)
iofile.seek(0)
return pkl.load(iofile)
def load(filename):
with open(filename, 'rb') as infile:
return pkl.load(infile)
def dump(obj, filename):
with open(filename, 'wb') as outfile:
pkl.dump(obj, outfile)
background = setdefault('./cogs/background.pkl', {})
with open('config.json') as infile: with open('config.json') as infile:
config = json.load(infile) config = json.load(infile)
def update(out, file):
with open(file, 'w') as outfile:
json.dump(out, outfile, indent=4, sort_keys=True)
import asyncio async def clear(obj, interval=10 * 60, replace=None):
async def clear(obj, interval=10*60, replace=None):
if replace is None: if replace is None:
if type(obj) is list: if type(obj) is list:
replace = [] replace = []
@ -34,3 +48,16 @@ async def clear(obj, interval=10*60, replace=None):
while True: while True:
obj = replace obj = replace
asyncio.sleep(interval) asyncio.sleep(interval)
session = None
HEADERS = {'user-agent': 'Modumind/0.0.1 (Myned)'}
async def fetch(url, *, params={}, json=False):
global session, HEADERS
async with session.get(url, params=params, headers=HEADERS) as r:
if json is True:
return await r.json()
return r