import  googlesearch
import codecs
import sys
import time
import dataset
import os


db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/hhh?charset=utf8mb4')
table=db['hhh_contentgap_serp']

#print(os.path.realpath('.'))
curdir=os.path.realpath('.')

fr=codecs.open(curdir+os.sep+'contentgap.txt','r','utf-8')
lines=fr.readlines()
lst=[]
for l in lines:
    lst.append(l.replace('\n',''))


for l in lst[25:]:
    time.sleep(5)
    results=googlesearch.search(l, num_results=150)
    idx=0
    for r in results:
        print(idx)
        print(r)
        if 'hhh.com.tw' in r:
            print('found')
            print(idx)
            table.insert({'kw':l,'ranking':idx})
            db.commit()
            break
        idx+=1
        


sys.exit()
