init commit, should have done this a while ago
This commit is contained in:
commit
ff0b6969a6
33
api.py
Normal file
33
api.py
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
import aiohttp
|
||||||
|
|
||||||
|
secure=True
|
||||||
|
def url(endpoint,version=1):
|
||||||
|
s='s' if secure else ''
|
||||||
|
return f'http{s}://{instance}/api/v{version}/{endpoint}'
|
||||||
|
|
||||||
|
#cs=aiohttp.ClientSession() # Fuck your warnings
|
||||||
|
#Fuck your errors too
|
||||||
|
async def call(method,endpoint,version=1,headers={},**kwargs):
|
||||||
|
async with aiohttp.ClientSession() as sess:
|
||||||
|
async with sess.request(method, url(endpoint,version), headers={'Authorization':f'Bearer {token}'}|headers, **kwargs) as r:
|
||||||
|
try: return await r.json()
|
||||||
|
except aiohttp.client_exceptions.ContentTypeError: raise Exception(await r.read())
|
||||||
|
|
||||||
|
async def post(text,images=[],json={},**kwargs):
|
||||||
|
j={'status':text}
|
||||||
|
pics=[]
|
||||||
|
for image in images:
|
||||||
|
f=aiohttp.FormData()
|
||||||
|
f.add_field('file',image[1],filename=image[0])
|
||||||
|
pics.append(await call('POST','media',2,data=f))
|
||||||
|
if pics: j['media_ids']=list(map(lambda x:x['id'],pics))
|
||||||
|
await call('POST','statuses',json=j|json,**kwargs)
|
||||||
|
|
||||||
|
async def stream(method,endpoint,params,needle,*args,**kwargs):
|
||||||
|
p={'limit':40}|params
|
||||||
|
while True:
|
||||||
|
page=await call(method,endpoint,params=p,*args,**kwargs)
|
||||||
|
if not page: break # Handles a particular edge case
|
||||||
|
for n in page: yield n
|
||||||
|
p|=needle(page)
|
||||||
|
if len(page)<40: break
|
32
main.py
Normal file
32
main.py
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
import asyncio
|
||||||
|
import functools
|
||||||
|
import datetime, traceback
|
||||||
|
#from os import path
|
||||||
|
|
||||||
|
#conf=j.load(open(path.join(path.dirname(__file__),'config.json')))[path.splitext(path.basename(__file__))[0]]
|
||||||
|
|
||||||
|
def a():
|
||||||
|
yield 1
|
||||||
|
gentype=type(a())
|
||||||
|
funkies=[]
|
||||||
|
|
||||||
|
def loop(timer=0,start=None):
|
||||||
|
def _loop(funky):
|
||||||
|
@functools.wraps(funky)
|
||||||
|
async def _wrapper(*args,**kwargs):
|
||||||
|
if start is not None:
|
||||||
|
ima=datetime.datetime.utcnow()
|
||||||
|
await asyncio.sleep((start-ima).total_seconds()) # It'll be very slightly wrong but whatever
|
||||||
|
while True:
|
||||||
|
try: await funky(*args,**kwargs)
|
||||||
|
except Exception: open('error '+str(datetime.datetime.now()),'w').write(traceback.format_exc())
|
||||||
|
await asyncio.sleep(timer)
|
||||||
|
funkies.append(_wrapper)
|
||||||
|
return _wrapper
|
||||||
|
return _loop
|
||||||
|
|
||||||
|
async def _main():
|
||||||
|
await asyncio.gather(*map(lambda x:x(),funkies))
|
||||||
|
|
||||||
|
def run():
|
||||||
|
asyncio.run(_main())
|
Loading…
Reference in New Issue
Block a user