main.py 7.5 KB

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