| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960 | #!/usr/bin/python3import sysimport codecsimport tracebackimport requestsimport reimport pandas as pdimport randomimport urllibimport datasetimport jsonimport gspreadimport datetimefrom gspread_pandas import Spread, Clientfrom oauth2client.service_account import ServiceAccountCredentialsimport osimport threadingdef save_sheet(df,filename,tabname,startpos='A1'):    scope = ['https://spreadsheets.google.com/feeds',            'https://www.googleapis.com/auth/drive']    credentials = ServiceAccountCredentials.from_json_keyfile_name('c:\\keys\\spread2.json', scope)#    credentials = ServiceAccountCredentials.from_json_keyfile_name('/var/keys/spread2.json', scope)    gc = gspread.authorize(credentials)    spread = Spread(filename,creds=credentials)    spread.df_to_sheet(df, index=False, sheet=tabname, start=startpos, replace=False)def do_jobs():    db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/hhh?charset=utf8mb4')#    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;')#    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;')#    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;')#    cursor=db.query('SELECT distinct kw,if(ranking IS NULL ,101,ranking ) as r   FROM hhh.hhh_contentgap_serp where id >=494 order by r asc;')#    cursor=db.query('SELECT distinct kw,if(ranking IS NULL ,101,ranking ) as r   FROM hhh.hhh_contentgap_serp where id >=544 order by r asc;')#    cursor=db.query('SELECT distinct kw,if(ranking IS NULL ,101,ranking ) as r   FROM hhh.hhh_contentgap_serp where id >=719 order by r asc;')    cursor=db.query('SELECT distinct kw, if( min(ranking) IS NULL ,101,min(ranking) )   as r FROM hhh.hhh_contentgap_serp where datediff(now(),dt) =0  group by kw order by id desc;')#SELECT * FROM hhh.hhh_contentgap_serp where id >=444 order by ranking desc;    df = pd.DataFrame(columns=('kw','r'))    idx=0    for c in cursor:        df.loc[idx]=[c['kw'],c['r']]        idx+=1    save_sheet(df,'ContentGap_Ranking','ranking_20220311')t = threading.Thread(target = do_jobs)t.start()t.join()
 |