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:
parent
8f304893e3
commit
af9ed570a2
1 changed files with 43 additions and 16 deletions
|
@ -1,24 +1,38 @@
|
|||
import asyncio
|
||||
import json
|
||||
import pickle as pkl
|
||||
|
||||
import aiohttp as aio
|
||||
|
||||
|
||||
def setdefault(filename, default=None):
|
||||
try:
|
||||
with open('background.json') as infile:
|
||||
background = json.load(infile)
|
||||
print('\"background.json\" loaded.')
|
||||
with open(filename, 'rb') as infile:
|
||||
print('\"{}\" loaded.'.format(filename))
|
||||
return pkl.load(infile)
|
||||
except FileNotFoundError:
|
||||
with open('background.json', 'w+') as iofile:
|
||||
print('Background file not found: \"background.json\" created and loaded.')
|
||||
json.dump({}, iofile, indent=4, sort_keys=True)
|
||||
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)
|
||||
background = json.load(iofile)
|
||||
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:
|
||||
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):
|
||||
if replace is None:
|
||||
|
@ -34,3 +48,16 @@ async def clear(obj, interval=10*60, replace=None):
|
|||
while True:
|
||||
obj = replace
|
||||
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
|
||||
|
|
Loading…
Reference in a new issue