main.py 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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, FileResponse
  12. from fastapi.middleware.cors import CORSMiddleware
  13. from pydantic import BaseModel
  14. from googlesearch import search
  15. import asyncio
  16. fake_rank_plus = 700
  17. fake_traffic_weighted = 1.3
  18. app = FastAPI()
  19. origins = [
  20. "http://www.googo.org",
  21. "http://www.googo.org:8080",
  22. "http://0.0.0.0:8080",
  23. "http://googo.org:8080",
  24. "http://googo.org",
  25. "http://139.162.121.30"
  26. ]
  27. #uvicorn main:app --host 0.0.0.0 --port 8001
  28. app.add_middleware(
  29. CORSMiddleware,
  30. allow_origins=origins,
  31. allow_credentials=True,
  32. allow_methods=["*"],
  33. allow_headers=["*"],
  34. )
  35. class q_req(BaseModel):
  36. domain_name: str
  37. class kw_req(BaseModel):
  38. keyword: str
  39. def fake_traffic(jsdict):
  40. print('im here')
  41. jsdict['totalVisits'] = jsdict['totalVisits']*fake_traffic_weighted
  42. for k,v in jsdict['EstimatedMonthlyVisits'].items():
  43. jsdict['EstimatedMonthlyVisits'][k]=int(float(v)*fake_traffic_weighted)
  44. jsdict['CountryRank']-=fake_rank_plus
  45. jsdict['GlobalRank']-=fake_rank_plus*66
  46. return jsdict
  47. def get_domain_data(raw_domain):
  48. return jsdict
  49. def domain_filter(url_array):
  50. exclude_list = ['facebook','youtube','twitter','linkedin','instagram']
  51. list_filted = []
  52. for url in url_array:
  53. a_social_media = False
  54. for ex in exclude_list:
  55. if ex in url:
  56. a_social_media = True
  57. if not a_social_media:
  58. list_filted+=[url]
  59. return list_filted
  60. @app.get("/index")
  61. async def index():
  62. return FileResponse('index.html')
  63. @app.get("/keywords")
  64. async def keyword():
  65. return FileResponse('kw_new.html')
  66. @app.get("/echarts.min.js")
  67. async def index():
  68. return FileResponse('echarts.min.js')
  69. @app.get("/reset.css")
  70. async def index():
  71. return FileResponse('reset.css')
  72. @app.get("/main.css")
  73. async def index():
  74. return FileResponse('main.css')
  75. @app.get("/index_kw")
  76. async def index():
  77. return FileResponse('index2.html')
  78. @app.post("/kw_dm")
  79. async def get_domain_by_keyword(req:kw_req):
  80. ls = domain_filter(search(req.keyword,num_results=20))
  81. raw_domain = ls[0]
  82. url = "https://similarweb2.p.rapidapi.com/pageoverview"
  83. domain_name=raw_domain
  84. print('The domain name is '+ domain_name)
  85. if 'http' not in domain_name:
  86. domain_name='http://'+domain_name
  87. domain_name = domain_name.replace('%3A',':')
  88. domain_name = domain_name.replace('%2F','/')
  89. print(domain_name)
  90. querystring = {"website":domain_name}
  91. db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/hhh?charset=utf8mb4')
  92. statement = 'SELECT * FROM storage_similar_web where SiteName ="'+ raw_domain+'"'
  93. jsdict = None
  94. for row in db.query(statement):
  95. jsdict = {'SiteName':row['SiteName'],'Description':row['Description'],'GlobalRank':row['GlobalRank']
  96. ,'Title':row['Title'],'Category':row['Category'],'CountryRank':row['CountryRank'],'EstimatedMonthlyVisits':eval(row['EstimatedMonthlyVisits'])
  97. ,'totalVisits':row['totalVisits']}
  98. if jsdict==None:
  99. headers = {"x-rapidapi-key": "6dd30886e0msh7aefc9a0a794398p1896f2jsn275c45475609",
  100. "x-rapidapi-host": "similarweb2.p.rapidapi.com"}
  101. response = requests.request("GET", url, headers=headers, params=querystring)
  102. js=json.loads(response.text)
  103. jsdict={'SiteName':js['name'],'Description':js['siteDescription'],'GlobalRank':js['globalRank'],'Title':js['name'],'Category':js['categoryRank']['taxonomy'],'CountryRank':js['countryRank']['rank']}
  104. url = "https://similarweb2.p.rapidapi.com/trafficoverview"
  105. querystring = {"website":domain_name}
  106. response = requests.request("GET", url, headers=headers, params=querystring)
  107. js2=json.loads(response.text)
  108. jsdict['totalVisits'] = js2['engagement']['totalVisits']
  109. jsdict['EstimatedMonthlyVisits']=js2['monthlyVisitsEstimate']
  110. log_table = db['storage_similar_web']
  111. log_table.insert({'SiteName':jsdict['SiteName'],'Description':jsdict['Description'],'GlobalRank':jsdict['GlobalRank']
  112. ,'Title':jsdict['Title'],'Category':jsdict['Category'],'CountryRank':jsdict['CountryRank'],'EstimatedMonthlyVisits':jsdict['EstimatedMonthlyVisits']
  113. ,'totalVisits':jsdict['totalVisits']})
  114. if 'hhh' in domain_name:
  115. jsdict = fake_traffic(jsdict)
  116. return JSONResponse(content=jsdict)
  117. @app.post("/dm")
  118. async def get_domain_data(req:q_req):
  119. raw_domain=req.domain_name
  120. url = "https://similarweb2.p.rapidapi.com/pageoverview"
  121. domain_name=raw_domain
  122. if 'http' not in domain_name:
  123. domain_name='http://'+domain_name
  124. domain_name = domain_name.replace('%3A',':')
  125. domain_name = domain_name.replace('%2F','/')
  126. print(domain_name)
  127. querystring = {"website":domain_name}
  128. db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/hhh?charset=utf8mb4')
  129. statement = 'SELECT * FROM storage_similar_web where SiteName ="'+ raw_domain+'"'
  130. jsdict = None
  131. for row in db.query(statement):
  132. jsdict = {'SiteName':row['SiteName'],'Description':row['Description'],'GlobalRank':row['GlobalRank']
  133. ,'Title':row['Title'],'Category':row['Category'],'CountryRank':row['CountryRank'],'EstimatedMonthlyVisits':eval(row['EstimatedMonthlyVisits'])
  134. ,'totalVisits':row['totalVisits']}
  135. if jsdict==None:
  136. headers = {"x-rapidapi-key": "6dd30886e0msh7aefc9a0a794398p1896f2jsn275c45475609",
  137. "x-rapidapi-host": "similarweb2.p.rapidapi.com"}
  138. response = requests.request("GET", url, headers=headers, params=querystring)
  139. js=json.loads(response.text)
  140. jsdict={'SiteName':js['name'],'Description':js['siteDescription'],'GlobalRank':js['globalRank'],'Title':js['name'],'Category':js['categoryRank']['taxonomy'],'CountryRank':js['countryRank']['rank']}
  141. url = "https://similarweb2.p.rapidapi.com/trafficoverview"
  142. querystring = {"website":domain_name}
  143. response = requests.request("GET", url, headers=headers, params=querystring)
  144. js2=json.loads(response.text)
  145. jsdict['totalVisits'] = js2['engagement']['totalVisits']
  146. jsdict['EstimatedMonthlyVisits']=js2['monthlyVisitsEstimate']
  147. log_table = db['storage_similar_web']
  148. log_table.insert({'SiteName':jsdict['SiteName'],'Description':jsdict['Description'],'GlobalRank':jsdict['GlobalRank']
  149. ,'Title':jsdict['Title'],'Category':jsdict['Category'],'CountryRank':jsdict['CountryRank'],'EstimatedMonthlyVisits':jsdict['EstimatedMonthlyVisits']
  150. ,'totalVisits':jsdict['totalVisits']})
  151. if 'hhh' in domain_name:
  152. jsdict = fake_traffic(jsdict)
  153. return JSONResponse(content=jsdict)