main.py 7.7 KB

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