SHARE
TWEET

Untitled

a guest Oct 9th, 2019 107 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from aiohttp import web
  2. import asyncio
  3. import json
  4. import requests
  5.  
  6. period = 5
  7.  
  8. curr = [[],[]]
  9.  
  10. async def api_wallet():    
  11.     global curr
  12.     while True:      
  13.        
  14.         r = requests.get('https://www.cbr-xml-daily.ru/daily_json.js')
  15.         data = r.json
  16.         data = json.loads(r.content)
  17.         usd_r = data['Valute']['USD']['Value']
  18.         eur_r = data['Valute']['EUR']['Value']
  19.  
  20.         if not usd_r in curr[0]:
  21.             curr[0] = [usd_r]
  22.             print(usd_r)
  23.  
  24.         if not eur_r in curr[1]:
  25.             curr[1] = [eur_r]    
  26.             print(eur_r)
  27.        
  28.         await asyncio.sleep(period)
  29.  
  30. def usd_get(request):
  31.     return web.Response(text=' usd = ' + str(curr[0][-1]),
  32.                         content_type='text/plain')
  33. def eur_get(request):
  34.     return web.Response(text=' eur = ' + str(curr[1][-1]),
  35.                         content_type='text/plain')
  36.  
  37. def create_runner():
  38.     app = web.Application()
  39.     app.add_routes([        
  40.         web.get('/usd/get', usd_get),
  41.         web.get('/eur/get', eur_get)])
  42.     return web.AppRunner(app)
  43.  
  44. async def start_server(host='127.0.0.1', port=8080):
  45.     runner = create_runner()
  46.     await runner.setup()
  47.     site = web.TCPSite(runner, host, port)
  48.     await site.start()
  49.  
  50.  
  51. if __name__ == '__main__':
  52.     loop = asyncio.get_event_loop()    
  53.     loop.run_until_complete(start_server())
  54.     tasks = [asyncio.ensure_future(api_wallet())]    
  55.     loop.run_until_complete(asyncio.wait(tasks))
  56.     loop.run_forever()
  57.     loop.close()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top