main.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. import kw_planner
  14. db = dataset.connect('sqlite:///similar.db')
  15. app = FastAPI()
  16. origins = [
  17. "http://www.googo.org",
  18. "http://www.googo.org:8080",
  19. "http://www.googo.org:8001",
  20. "http://0.0.0.0:8080",
  21. "http://googo.org:8080",
  22. "http://googo.org",
  23. "http://139.162.121.30"
  24. ]
  25. app.add_middleware(
  26. CORSMiddleware,
  27. allow_origins=origins,
  28. allow_credentials=True,
  29. allow_methods=["*"],
  30. allow_headers=["*"],
  31. )
  32. @app.get("/index")
  33. async def index():
  34. return FileResponse('index.html')
  35. @app.post("/dm/{domain_name}")
  36. async def get_domain(domain_name):
  37. url = "https://similarweb2.p.rapidapi.com/pageoverview"
  38. if 'http' not in domain_name:
  39. domain_name='http://'+domain_name
  40. domain_name = domain_name.replace('%3A',':')
  41. domain_name = domain_name.replace('%2F','/')
  42. print(domain_name)
  43. querystring = {"website":domain_name}
  44. headers = {
  45. "x-rapidapi-key": "6dd30886e0msh7aefc9a0a794398p1896f2jsn275c45475609",
  46. "x-rapidapi-host": "similarweb2.p.rapidapi.com"
  47. }
  48. response = requests.request("GET", url, headers=headers, params=querystring)
  49. print(response.text)
  50. js=json.loads(response.text)
  51. print(response.text)
  52. jsdict={'SiteName':js['name'],'Description':js['siteDescription'],'GlobalRank':js['globalRank'],'Title':js['name'],'Category':js['categoryRank']['taxonomy'],'CountryRank':js['countryRank']['rank']}
  53. url = "https://similarweb2.p.rapidapi.com/trafficoverview"
  54. querystring = {"website":domain_name}
  55. response = requests.request("GET", url, headers=headers, params=querystring)
  56. js2=json.loads(response.text)
  57. print(response.text)
  58. jsdict['EstimatedMonthlyVisits']=js2['monthlyVisitsEstimate']
  59. # desc=js['siteDescription']
  60. # ustr=desc.encode('utf-8').decode('utf-8')
  61. # print(ustr)
  62. return JSONResponse(content=jsdict)
  63. @app.get("/keyword/{keyword}}")
  64. async def keyword(keyword):
  65. print(keyword)
  66. return "OK"
  67. @app.get("/domain/{domain_name}")
  68. async def read_item(domain_name):
  69. cursor=db.query('select domain_name,json,dt from similar where domain_name="'+domain_name+'"')
  70. for c in cursor:
  71. print(c['json'])
  72. print(c['dt'])
  73. # jsdict=ast.literal_eval(c['json'])
  74. jsdict=json.loads(c['json'])
  75. return JSONResponse(content=jsdict)
  76. print(domain_name)
  77. # conn = rpyc.connect("localhost",12345)
  78. conn = rpyc.connect("192.168.192.119",12345)
  79. #domain_name
  80. jsdata=conn.root.get_url('https://data.similarweb.com/api/v1/data?domain='+domain_name)
  81. print(jsdata)
  82. # jsdict=str(jsdata)[1:-1]
  83. # jsdict=json.loads(str(jsdata))
  84. jsdict=ast.literal_eval(str(jsdata))
  85. # jsdict=str(jsdata)
  86. table=db['similar']
  87. jstr=json.dumps(jsdict)
  88. table.insert({'domain_name':domain_name,'json':jstr,'dt':datetime.datetime.now()})
  89. db.commit()
  90. return JSONResponse(content=jsdict)
  91. # print(jsdata)
  92. # return {"item_id": domain_name}