top_keywords_ranking.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. #!/usr/bin/python3
  2. import sys
  3. import codecs
  4. import traceback
  5. import requests
  6. import re
  7. import pandas as pd
  8. import random
  9. import urllib
  10. import dataset
  11. import json
  12. import gspread
  13. import datetime
  14. from gspread_pandas import Spread, Client
  15. from oauth2client.service_account import ServiceAccountCredentials
  16. import os
  17. import threading
  18. def save_sheet(df,filename,tabname,startpos='A1'):
  19. scope = ['https://spreadsheets.google.com/feeds',
  20. 'https://www.googleapis.com/auth/drive']
  21. credentials = ServiceAccountCredentials.from_json_keyfile_name('c:\\keys\\spread2.json', scope)
  22. # credentials = ServiceAccountCredentials.from_json_keyfile_name('/var/keys/spread2.json', scope)
  23. gc = gspread.authorize(credentials)
  24. spread = Spread(filename,creds=credentials)
  25. spread.df_to_sheet(df, index=False, sheet=tabname, start=startpos, replace=False)
  26. def do_jobs():
  27. db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/hhh?charset=utf8mb4')
  28. # cursor=db.query('SELECT distinct kw,if(ranking IS NULL ,"未上榜",ranking ) as r FROM hhh.hhh_contentgap_serp where id >=155 order by r asc;')
  29. # cursor=db.query('SELECT distinct kw,if(ranking IS NULL ,"未上榜",ranking ) as r FROM hhh.hhh_contentgap_serp where id >=394 order by r asc;')
  30. # cursor=db.query('SELECT distinct kw,if(ranking IS NULL ,"未上榜",ranking ) as r FROM hhh.hhh_contentgap_serp where id >=444 order by r asc;')
  31. # cursor=db.query('SELECT distinct kw,if(ranking IS NULL ,101,ranking ) as r FROM hhh.hhh_top_serp where id >=274 order by r asc;')
  32. cursor=db.query('SELECT distinct kw, if( min(ranking) IS NULL ,101,min(ranking) ) as r FROM hhh.hhh_top_serp where datediff(now(),dt) =0 group by kw order by id desc;')
  33. #SELECT * FROM hhh.hhh_contentgap_serp where id >=444 order by ranking desc;
  34. df = pd.DataFrame(columns=('kw','r'))
  35. idx=0
  36. for c in cursor:
  37. df.loc[idx]=[c['kw'],c['r']]
  38. idx+=1
  39. save_sheet(df,'WeeklyReport','top')
  40. t = threading.Thread(target = do_jobs)
  41. t.start()
  42. t.join()