Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from aiohttp import web
- import asyncio
- import json
- import requests
- period = 5
- curr = [[],[]]
- async def api_wallet():
- global curr
- while True:
- r = requests.get('https://www.cbr-xml-daily.ru/daily_json.js')
- data = r.json
- data = json.loads(r.content)
- usd_r = data['Valute']['USD']['Value']
- eur_r = data['Valute']['EUR']['Value']
- if not usd_r in curr[0]:
- curr[0] = [usd_r]
- print(usd_r)
- if not eur_r in curr[1]:
- curr[1] = [eur_r]
- print(eur_r)
- await asyncio.sleep(period)
- def usd_get(request):
- return web.Response(text=' usd = ' + str(curr[0][-1]),
- content_type='text/plain')
- def eur_get(request):
- return web.Response(text=' eur = ' + str(curr[1][-1]),
- content_type='text/plain')
- def create_runner():
- app = web.Application()
- app.add_routes([
- web.get('/usd/get', usd_get),
- web.get('/eur/get', eur_get)])
- return web.AppRunner(app)
- async def start_server(host='127.0.0.1', port=8080):
- runner = create_runner()
- await runner.setup()
- site = web.TCPSite(runner, host, port)
- await site.start()
- if __name__ == '__main__':
- loop = asyncio.get_event_loop()
- loop.run_until_complete(start_server())
- tasks = [asyncio.ensure_future(api_wallet())]
- loop.run_until_complete(asyncio.wait(tasks))
- loop.run_forever()
- loop.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement