gsc_tree2.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. import searchconsole
  2. import dataset
  3. import os
  4. import networkx as nx
  5. from pyvis.network import Network
  6. db = dataset.connect('sqlite:///:memory:')
  7. table=db['tmp']
  8. rid=0
  9. def checkig():
  10. global instl
  11. global table
  12. global pname
  13. global rid
  14. lst=[]
  15. cntdict={}
  16. codelist={}
  17. idx=0
  18. flag_break=False
  19. fname=os.path.abspath(__file__)
  20. elmts=fname.split(os.path.sep)
  21. path2=os.path.sep.join(elmts[0:-1])
  22. keysdir=path2+os.path.sep+'../keys'+os.path.sep
  23. account = searchconsole.authenticate(client_config='/Users/zooeytsai/Downloads/client_secret4.json')
  24. webproperty = account['https://bennis.com.tw/']
  25. print(webproperty)
  26. report = webproperty.query.range('today', days=-2).dimension('query').get()
  27. # report = webproperty.query.range('2022-05-20', '2022-05-23').dimension('page', 'query').get()
  28. # account = searchconsole.authenticate(client_config='C:\\gitlab\\kw_tools\\kw_tools\\hhh\\keys\\client_secret.json',credentials='C:\\gitlab\\kw_tools\\kw_tools\\hhh\\keys\\credentials.json')
  29. G = nx.Graph()
  30. # webproperty = account['https://ipromise.com.tw/']
  31. # webproperty = account['sc-domain:face8ook.org']
  32. # webproperty = account['sc-domain:hhh.com.tw']
  33. result=[]
  34. rdict={}
  35. total_idx=0
  36. for r in report.rows:
  37. if 'hhh.com.tw/designers/cases/' not in r[0]:
  38. continue
  39. if rdict.get(r[0]) is None:
  40. total_idx+=1
  41. rid=total_idx
  42. rdict[r[0]]=rid
  43. else:
  44. rid=rdict[r[0]]
  45. entry={'page':r[0],'query':r[1],'rid':rid}
  46. result.append(entry)
  47. print('list done')
  48. for r in result:
  49. table.insert(r)
  50. db.commit()
  51. print('db done')
  52. # cursor=db.query('select query as q,page as url,rid from tmp where rid in (select rid from (select rid,count(*) from tmp group by rid having count(*) > 2 and count(*) < 6) as tbl1) order by rid ')
  53. cursor=db.query('select query as q,page as url,rid from tmp order by rid ')
  54. riddict={}
  55. prev=''
  56. curnode=''
  57. cururl=''
  58. total_idx=0
  59. for c in cursor:
  60. G.add_edge(c['q'],c['rid'],weight=3,width=3,borderwidth=3)
  61. remove=[]
  62. G.remove_edges_from(nx.selfloop_edges(G))
  63. G2=G
  64. pyG = Network(height="600px", width="100%",bgcolor="#444444",font_color="white")
  65. pyG.from_nx(G2)
  66. pyG.show('news.html')
  67. r=checkig()