main.py 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. import rpyc
  2. import time
  3. from fastapi import FastAPI
  4. import sys
  5. import os
  6. import dataset
  7. import datetime
  8. import json
  9. import ast
  10. from fastapi.middleware.cors import CORSMiddleware
  11. db = dataset.connect('sqlite:///similar.db')
  12. app = FastAPI()
  13. origins = [
  14. "http://www.googo.org",
  15. "http://www.googo.org:8080",
  16. "http://googo.org:8080",
  17. "http://googo.org",
  18. "http://139.162.121.30"
  19. ]
  20. app.add_middleware(
  21. CORSMiddleware,
  22. allow_origins=origins,
  23. allow_credentials=True,
  24. allow_methods=["*"],
  25. allow_headers=["*"],
  26. )
  27. @app.get("/domain/{domain_name}")
  28. async def read_item(domain_name):
  29. cursor=db.query('select domain_name,json,dt from similar where domain_name="'+domain_name+'"')
  30. for c in cursor:
  31. print(c['json'])
  32. print(c['dt'])
  33. jsdict=ast.literal_eval(c['json'])
  34. return jsdict
  35. print(domain_name)
  36. # conn = rpyc.connect("localhost",12345)
  37. conn = rpyc.connect("192.168.192.119",12345)
  38. #domain_name
  39. jsdata=conn.root.get_url('https://data.similarweb.com/api/v1/data?domain='+domain_name)
  40. print(jsdata)
  41. # jsdict=str(jsdata)[1:-1]
  42. # jsdict=json.loads(str(jsdata))
  43. jsdict=ast.literal_eval(str(jsdata))
  44. # jsdict=str(jsdata)
  45. table=db['similar']
  46. jstr=json.dumps(jsdict)
  47. table.insert({'domain_name':domain_name,'json':jstr,'dt':datetime.datetime.now()})
  48. db.commit()
  49. return jsdata
  50. # print(jsdata)
  51. # return {"item_id": domain_name}