hhh_tree.py 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. from instaloader import Instaloader, Profile
  2. import traceback
  3. import copy
  4. import operator
  5. import dataset
  6. import pandas as pd
  7. import networkx as nx
  8. #import pysftp
  9. import codecs
  10. import pyvis
  11. import sys
  12. import pickle
  13. import os
  14. import searchconsole
  15. from pyvis.network import Network
  16. import jieba
  17. db = dataset.connect('sqlite:///:memory:')
  18. table=db['tmp']
  19. #pname='cont'
  20. #pname='damanwoo'
  21. #pname='drama'
  22. #pname='news'
  23. #pname='www'
  24. #pname='ipromise'
  25. #pname='sports'
  26. #pname='rumor'
  27. #pname='korea'
  28. pname='hhh.rb'
  29. rid=0
  30. def get_css():
  31. fr=codecs.open('jared/data/css.txt','r','utf-8')
  32. lines=fr.readlines()
  33. content=' '.join(lines)
  34. fr.close()
  35. return content
  36. def modify_file(fname):
  37. fr=codecs.open(fname,'r','utf-8')
  38. lines=fr.readlines()
  39. fr.close()
  40. # css=get_css()
  41. css=''
  42. content_output=''
  43. for l in lines:
  44. if '<body>' in l[0:10]:
  45. content_output+=l
  46. content_output+='\n<div id="google">\n'
  47. continue
  48. if '<style type="text' in l[0:22]:
  49. content_output+=l
  50. content_output+="\n"+css+"\n"
  51. continue
  52. if '<div id = "mynetwork"' in l[0:30]:
  53. content_output+=l
  54. content_output+='\n</div>\n'
  55. continue
  56. content_output+=l
  57. fw=codecs.open("mod_"+fname,'w','utf-8')
  58. fw.write(content_output)
  59. fw.close()
  60. def checkig(pgnum):
  61. global instl
  62. global table
  63. global pname
  64. global rid
  65. lst=[]
  66. cntdict={}
  67. codelist={}
  68. idx=0
  69. flag_break=False
  70. fname=os.path.abspath(__file__)
  71. elmts=fname.split(os.path.sep)
  72. path2=os.path.sep.join(elmts[0:-1])
  73. keysdir=path2+os.path.sep+'keys'+os.path.sep
  74. # account = searchconsole.authenticate(client_config='c:/keys/client_secret.json',credentials='c:/keys/credentials.json')
  75. account = searchconsole.authenticate(client_config=keysdir+'client_secret.json',credentials=keysdir+'credentials.json')
  76. # webproperty = account['https://ipromise.com.tw/']
  77. # webproperty = account['https://'+pname+'.face8ook.org/']
  78. # webproperty = account['https://www.damanwoo.com/']
  79. # webproperty = account['https://hhh.com.tw/']
  80. webproperty = account['https://innews.com.tw/']
  81. # report=webproperty.query.range('2021-03-01', '2021-06-17').dimension('page','query').get()
  82. # report=webproperty.query.range('2021-06-01', '2021-06-17').dimension('page','query').get()
  83. # report=webproperty.query.range('2020-06-01', '2021-06-22').dimension('page','query').filter('page', '/designers/cases/(491|31|293|278|31|24|594|356|307|491|33|385)', 'equals').get()
  84. # report=webproperty.query.range('2020-03-01', '2021-06-22').dimension('page','query').filter('page', '/designers/cases/'+pgnum, 'contains').get()
  85. # report=webproperty.query.range('2021-12-01', '2022-01-18').dimension('page','query').get()
  86. report=webproperty.query.range('2022-01-01', '2022-04-16').dimension('page','query').get()
  87. urlq={}
  88. for r in report.rows:
  89. if urlq.get(r[0]) is None:
  90. urlq[r[0]]=[r[1]]
  91. else:
  92. urlq[r[0]].append(r[1])
  93. print(urlq)
  94. allrows=[]
  95. for k,v in urlq.items():
  96. # if len(v)<40:
  97. if len(v)<0:
  98. continue
  99. # print(k)
  100. for q in v:
  101. # elmts=jieba.cut(q)
  102. elmts=q.split(' ')
  103. for elmt in elmts:
  104. # print(elmt)
  105. table.insert({'q':elmt,'rid':rid,'url':k})
  106. rid+=1
  107. allrows.append([r[0],r[1] ])
  108. db.commit()
  109. # cursor=db.query('(select q from (select q,count(url) from tmp where length(q)> 2 group by q having count(url) <= 3) as tbl1 )')
  110. def gen_pic():
  111. G=None
  112. # if os.path.exists(pname):
  113. # G = pickle.load( open( pname, "rb" ) )
  114. # else:
  115. # G = nx.Graph()
  116. G = nx.Graph()
  117. finallist=[]
  118. # cursor=db.query('select q,rid,url from tmp where q in (select distinct q from (select q,count(url) from tmp where length(q)> 2 group by q having count(url) <= 3) as tbl1 ) order by q')
  119. # cursor=db.query('select q,rid,url from tmp where q in (select distinct q from (select q,count(url) from tmp where length(q)> 2 group by q having count(url) <= 3) as tbl1 ) order by q')
  120. # cursor=db.query('select q,rid,url from tmp where q in (select distinct q from (select q,count(url) from tmp where length(q)> 2 group by q having ) as tbl1 ) order by q')
  121. # cursor=db.query('select q,rid,url from tmp where q in (select distinct q from (select q,count(url) from tmp where length(q)> 2 group by q ) as tbl1 ) order by q')
  122. cursor=db.query('select q,rid,url from tmp where q in (select distinct q from (select q,count(url) from tmp where length(q)> 2 group by q having count(url) <= 3) as tbl1 ) order by q')
  123. riddict={}
  124. prev=''
  125. curnode=''
  126. cururl=''
  127. total_idx=0
  128. for c in cursor:
  129. if c['q']!=prev:
  130. cururl=c['url']
  131. prev=c['q']
  132. total_idx+=1
  133. # if total_idx >= 200:
  134. # break
  135. else:
  136. # G.add_edge(cururl,c['url'],weight=3,width=3,borderwidth=3)
  137. # G.add_edge(cururl[40:51],c['url'][40:51],weight=3,width=3,borderwidth=3)
  138. # G.add_edge(c['q'],cururl[40:51],weight=3,width=3,borderwidth=3)
  139. # G.add_edge(c['q'],c['url'][40:51],weight=3,width=3,borderwidth=3)
  140. query=c['q']
  141. rid=c['rid']
  142. if riddict.get(rid) is None:
  143. riddict[rid]={}
  144. riddict[rid][query]=1
  145. else:
  146. if riddict[rid].get(query) is not None:
  147. riddict[rid][query]=1
  148. else:
  149. riddict[rid][query]={}
  150. G.add_edge(c['q'],c['rid'],weight=3,width=3,borderwidth=3)
  151. # G.add_edge(c['q'],c['rid'],weight=3,width=3,borderwidth=3)
  152. pickle.dump( G, open( pname, "wb" ) )
  153. # G2 = [G.subgraph(c).copy() for c in nx.connected_components(G)]
  154. # remove = [node for node,degree in dict(G.degree()).items() if degree <2]
  155. # G.remove_nodes_from(remove)
  156. remove=[]
  157. # for n in G.nodes:
  158. # if '承諾' in n:
  159. # remove.append(n)
  160. # if 'promise' in n:
  161. # remove.append(n)
  162. # G.remove_nodes_from(remove)
  163. G.remove_edges_from(nx.selfloop_edges(G))
  164. G.remove_nodes_from(list(nx.isolates(G)))
  165. # lst= [G.subgraph(c).copy() for c in nx.connected_components(G)]
  166. # lst=[]
  167. # for c in nx.connected_components(G):
  168. # cc=G.subgraph(c).copy()
  169. # if cc.number_of_nodes()>7:
  170. # lst.append(cc)
  171. # if nx.diameter(cc, e=None, usebounds=False)>1:
  172. # lst.append(cc)
  173. # G2=nx.compose_all(lst)
  174. G2=G
  175. # pyG = Network(height="750px", width="100%",bgcolor="#333333",font_color="white")
  176. pyG = Network(height="600px", width="100%",bgcolor="#444444",font_color="white")
  177. pyG.from_nx(G2)
  178. pyG.show(pname+'.html')
  179. modify_file(pname+'.html')
  180. # cnopts = pysftp.CnOpts()
  181. # cnopts.hostkeys = None
  182. # s = pysftp.Connection(host='www.choozmo.com', username='jared', password='sstc5202',cnopts=cnopts)
  183. # local_path = "mod_"+pname+".html"
  184. # remote_path = "/home/nginx/farmoutput/tags/"+"mod_"+pname+".html"
  185. # s.put(local_path, remote_path)
  186. return finallist
  187. #r=checkig('投資')
  188. #r=checkig('保險')
  189. #r=checkig('嘖嘖')
  190. #r=checkig('募資')
  191. #r=checkig('遠赤外線')
  192. #lst=['491','31','293','278','24','594','356','307','491','33','385']
  193. #lst=['491','31','293','278','24','594','356','307','491','33','385']
  194. #for l in lst:
  195. # r=checkig(l)
  196. checkig('12')
  197. gen_pic()
  198. #r=checkig('信用卡')
  199. #print(r)
  200. # network.on( 'click', function(properties) {
  201. # var ids = properties.nodes;
  202. # var clickedNodes = nodes.get(ids);
  203. # var copyText = clickedNodes[0].label;
  204. # var promise = navigator.clipboard.writeText(copyText);
  205. #// console.log('clicked nodes:', clickedNodes);
  206. #});