import codecs
import sys
import codecs
import traceback
import requests
import re
import pandas as pd
import random
import urllib
import json
import gspread
import datetime
from gspread_pandas import Spread, Client
from oauth2client.service_account import ServiceAccountCredentials
import os

def 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\\service\\gspread.json', scope)
    credentials = ServiceAccountCredentials.from_json_keyfile_name('c:\\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)



fr=codecs.open('c:/tmp/out.txt','r','utf-8')
lines=fr.readlines()
fr.close()
cur_kw=None
fulldict={}
for l in lines:
    l=l.replace('\n','').replace('\u202a','').replace('\u202c','')
    if '(' in l:
        cur_kw=l
    else:
        elmts=l.split(',')
        lst=[]
        for elmt in elmts:
            lst.append(elmt)
        fulldict[cur_kw]=lst
#        fulldict

#    print(l)
print(fulldict)


def get_num_n(fulldict,n):
    result=[]
    for k,v in fulldict.items():
        if len(fulldict[k])<n+1:
            result.append(' ')
        else:
            result.append(fulldict[k][n])
    return result

cols=[]
maxlen=0
for k,v in fulldict.items():
    elmts=k.split('(')
    k=elmts[0]
    cols.append(k)
    if len(v)>maxlen:
        maxlen=len(v)

df = pd.DataFrame(columns=tuple(cols))
llen=len(fulldict.items())


for i in range(maxlen):
    df.loc[i]=get_num_n(fulldict,i)
print(df)
save_sheet(df,'keyword-details','風水')




