main.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. import rpyc
  2. import time
  3. from fastapi import FastAPI
  4. import sys
  5. import os
  6. import dataset
  7. import requests
  8. import datetime
  9. import json
  10. import ast
  11. from fastapi.responses import JSONResponse
  12. from fastapi.middleware.cors import CORSMiddleware
  13. db = dataset.connect('sqlite:///similar.db')
  14. app = FastAPI()
  15. origins = [
  16. "http://cmm.ai",
  17. "https://cmm.ai",
  18. ]
  19. app.add_middleware(
  20. CORSMiddleware,
  21. allow_origins=origins,
  22. allow_credentials=True,
  23. allow_methods=["*"],
  24. allow_headers=["*"],
  25. )
  26. @app.post("/seturl/{full_url}")
  27. async def get_domain(full_url):
  28. url = "https://similarweb2.p.rapidapi.com/pageoverview"
  29. if 'http' not in domain_name:
  30. domain_name='http://'+domain_name
  31. domain_name = domain_name.replace('%3A',':')
  32. domain_name = domain_name.replace('%2F','/')
  33. print(domain_name)
  34. querystring = {"website":domain_name}
  35. headers = {
  36. "x-rapidapi-key": "6dd30886e0msh7aefc9a0a794398p1896f2jsn275c45475609",
  37. "x-rapidapi-host": "similarweb2.p.rapidapi.com"
  38. }
  39. response = requests.request("GET", url, headers=headers, params=querystring)
  40. print(response.text)
  41. js=json.loads(response.text)
  42. print(response.text)
  43. jsdict={'SiteName':js['name'],'Description':js['siteDescription'],'GlobalRank':js['globalRank'],'Title':js['name'],'Category':js['categoryRank']['taxonomy'],'CountryRank':js['countryRank']['rank']}
  44. url = "https://similarweb2.p.rapidapi.com/trafficoverview"
  45. querystring = {"website":domain_name}
  46. response = requests.request("GET", url, headers=headers, params=querystring)
  47. js2=json.loads(response.text)
  48. print(response.text)
  49. jsdict['EstimatedMonthlyVisits']=js2['monthlyVisitsEstimate']
  50. # desc=js['siteDescription']
  51. # ustr=desc.encode('utf-8').decode('utf-8')
  52. # print(ustr)
  53. return JSONResponse(content=jsdict)
  54. @app.get("/domain/{domain_name}")
  55. async def read_item(domain_name):
  56. cursor=db.query('select domain_name,json,dt from similar where domain_name="'+domain_name+'"')
  57. for c in cursor:
  58. print(c['json'])
  59. print(c['dt'])
  60. # jsdict=ast.literal_eval(c['json'])
  61. jsdict=json.loads(c['json'])
  62. return JSONResponse(content=jsdict)
  63. print(domain_name)
  64. # conn = rpyc.connect("localhost",12345)
  65. conn = rpyc.connect("192.168.192.119",12345)
  66. #domain_name
  67. jsdata=conn.root.get_url('https://data.similarweb.com/api/v1/data?domain='+domain_name)
  68. print(jsdata)
  69. # jsdict=str(jsdata)[1:-1]
  70. # jsdict=json.loads(str(jsdata))
  71. jsdict=ast.literal_eval(str(jsdata))
  72. # jsdict=str(jsdata)
  73. table=db['similar']
  74. jstr=json.dumps(jsdict)
  75. table.insert({'domain_name':domain_name,'json':jstr,'dt':datetime.datetime.now()})
  76. db.commit()
  77. return JSONResponse(content=jsdict)
  78. # print(jsdata)
  79. # return {"item_id": domain_name}