Skip to content

Instantly share code, notes, and snippets.

@pyeongho
Created December 24, 2019 05:24
Show Gist options
  • Save pyeongho/fd6975c221e1ac00f0fa8728bf649daa to your computer and use it in GitHub Desktop.
Save pyeongho/fd6975c221e1ac00f0fa8728bf649daa to your computer and use it in GitHub Desktop.
import os
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
import csv;
import gdata.docs.service
import gdata.spreadsheet.service
from xml.etree.ElementTree import Element, ElementTree, SubElement, dump
'''
get google doc information from the command line argument
download method
Create by : YoungChan Lee
'''
def get_gdoc_information_android():
#email = raw_input('Email address:')
#password = getpass('Password:')
gdoc_id = raw_input('Google Doc Id:') # xxxxxxxxxxx#gid=1234 형태
downloadpath = sys.argv[2]
try:
file_path = download(gdoc_id)
readCSV(downloadpath,file_path)
except Exception, e:
print ":::::::::::::ERROR:::::::::::::"
print(e)
#raise e
def download(gdoc_id, download_path=None, ):
print "Downloading the CVS file with id %s" % gdoc_id
gd_client = gdata.docs.service.DocsService()
#auth using ClientLogin
gs_client = gdata.spreadsheet.service.SpreadsheetsService()
#gs_client.ClientLogin(email, password)
#getting the key(resource id and tab id from the ID)
resource = gdoc_id.split('#')[0]
tab = gdoc_id.split('#')[1].split('=')[1]
resource_id = 'spreadsheet:'+resource
if download_path is None:
download_path = os.path.abspath(os.path.dirname(__file__))
file_name = os.path.join(download_path, '%s.csv' % (gdoc_id))
print 'download_path : %s' % download_path;
print 'Downloading spreadsheet to %s' % file_name
docs_token = gd_client.GetClientLoginToken()
gd_client.SetClientLoginToken(gs_client.GetClientLoginToken())
gd_client.Export(resource_id, file_name, gid=tab)
gd_client.SetClientLoginToken(docs_token)
print "Download Completed!"
return file_name
def readCSV(savepath, file_name):
print "read CSV file : %s" % file_name
SourceCSV= open(file_name,"r")
csvReader = csv.reader(SourceCSV)
header = csvReader.next()
androidkey_idx = header.index("_name")
formatted_idx = header.index("_formatted")
en_idx = header.index("values-en")
# de_idx = header.index("values-de")
# es_idx = header.index("values-es")
# fr_idx = header.index("values-fr")
# hi_idx = header.index("values-hi")
# ja_idx = header.index("values-ja")
# ko_idx = header.index("values-ko")
# ru_idx = header.index("values-ru")
#ar_idx = header.index("values-ar")
#it_idx = header.index("values-it")
#pt_idx = header.index("values-pt")
#zh_tw_idx = header.index("values-zh-rTW")
# Make an empty Element
resources_en = Element("resources")
# resources_de = Element("resources")
# resources_es = Element("resources")
# resources_fr = Element("resources")
# resources_hi = Element("resources")
# resources_ja = Element("resources")
# resources_ko = Element("resources")
# resources_ru = Element("resources")
#resources_ar = Element("resources")
#resources_it = Element("resources")
#resources_pt = Element("resources")
#resources_zh_tw = Element("resources")
# Loop through the lines in the file and get each coordinate
for row in csvReader:
androidkey = row[androidkey_idx]
formatted = row[formatted_idx]
print(formatted +" "+androidkey)
en = row[en_idx]
# de = row[de_idx]
# if not row[de_idx]:
# de = row[en_idx]
#
# es = row[es_idx]
# if not row[es_idx]:
# es = row[en_idx]
#
# fr = row[fr_idx]
# if not row[fr_idx]:
# fr = row[en_idx]
#
# hi = row[hi_idx]
# if not row[hi_idx]:
# hi = row[en_idx]
#
# ja = row[ja_idx]
# if not row[ja_idx]:
# ja = row[en_idx]
#
# ko = row[ko_idx]
# if not row[ko_idx]:
# ko = row[en_idx]
#
# ru = row[ru_idx]
# if not row[ru_idx]:
# ru = row[en_idx]
'''
ar = row[ar_idx]
if not row[ar_idx]:
ar = row[en_idx]
it = row[it_idx]
if not row[it_idx]:
it = row[en_idx]
pt = row[pt_idx]
if not row[pt_idx]:
pt = row[en_idx]
zh_tw = row[zh_tw_idx]
if not row[zh_tw_idx]:
zh_tw = row[en_idx]
'''
#append en resource
if row[en_idx]:
string_en = Element("string")
string_en.attrib["name"] = androidkey.decode('utf-8')
string_en.text = en.decode('utf-8')
resources_en.append(string_en)
# #append de resource
# string_de = Element("string")
# string_de.attrib["name"] = androidkey.decode('utf-8')
# string_de.text = de.decode('utf-8')
# resources_de.append(string_de)
#
# #append es resource
# string_es = Element("string")
# string_es.attrib["name"] = androidkey.decode('utf-8')
# string_es.text = es.decode('utf-8')
# resources_es.append(string_es)
#
# #append fr resource
# string_fr = Element("string")
# string_fr.attrib["name"] = androidkey.decode('utf-8')
# string_fr.text = fr.decode('utf-8')
# resources_fr.append(string_fr)
#
# #append hi resource
# string_hi = Element("string")
# string_hi.attrib["name"] = androidkey.decode('utf-8')
# string_hi.text = hi.decode('utf-8')
# resources_hi.append(string_hi)
#
# #append ja resource
# string_ja = Element("string")
# string_ja.attrib["name"] = androidkey.decode('utf-8')
# string_ja.text = ja.decode('utf-8')
# resources_ja.append(string_ja)
#
# #append ko resource
# string_ko = Element("string")
# string_ko.attrib["name"] = androidkey.decode('utf-8')
# string_ko.text = ko.decode('utf-8')
# resources_ko.append(string_ko)
#
# #append ru resource
# string_ru = Element("string")
# string_ru.attrib["name"] = androidkey.decode('utf-8')
# string_ru.text = ru.decode('utf-8')
# resources_ru.append(string_ru)
'''
#append ar resource
string_ar = Element("string")
string_ar.attrib["name"] = androidkey.decode('utf-8')
string_ar.text = ar.decode('utf-8')
resources_ar.append(string_ar)
#append it resource
string_it = Element("string")
string_it.attrib["name"] = androidkey.decode('utf-8')
string_it.text = it.decode('utf-8')
resources_it.append(string_it)
#append pt resource
string_pt = Element("string")
string_pt.attrib["name"] = androidkey.decode('utf-8')
string_pt.text = pt.decode('utf-8')
resources_pt.append(string_pt)
#append zh_tw resource
string_zh_tw = Element("string")
string_zh_tw.attrib["name"] = androidkey.decode('utf-8')
string_zh_tw.text = zh_tw.decode('utf-8')
resources_zh_tw.append(string_zh_tw)
'''
def indent(elem, level=0):
i = "\n" + level*" "
if len(elem):
if not elem.text or not elem.text.strip():
elem.text = i + " "
if not elem.tail or not elem.tail.strip():
elem.tail = i
for elem in elem:
indent(elem, level+1)
if not elem.tail or not elem.tail.strip():
elem.tail = i
else:
if level and (not elem.tail or not elem.tail.strip()):
elem.tail = i
indent(resources_en)
#dump(resources_en)
# indent(resources_de)
# indent(resources_es)
# indent(resources_fr)
# indent(resources_hi)
# indent(resources_ja)
# indent(resources_ko)
# indent(resources_ru)
'''
indent(resources_ar)
indent(resources_it)
indent(resources_pt)
indent(resources_zh_tw)
'''
#Make Resource Folder
newpath_en = savepath+r'\values-en'
if not os.path.exists(newpath_en):
os.makedirs(newpath_en)
# newpath_de = savepath+r'\values-de'
# if not os.path.exists(newpath_de):
# os.makedirs(newpath_de)
#
# newpath_es = savepath+r'\values-es'
# if not os.path.exists(newpath_es):
# os.makedirs(newpath_es)
#
# newpath_fr = savepath+r'\values-fr'
# if not os.path.exists(newpath_fr):
# os.makedirs(newpath_fr)
#
# newpath_hi = savepath+r'\values-hi'
# if not os.path.exists(newpath_hi):
# os.makedirs(newpath_hi)
#
# newpath_ja = savepath+r'\values-ja'
# if not os.path.exists(newpath_ja):
# os.makedirs(newpath_ja)
#
# newpath_ko = savepath+r'\values-ko'
# if not os.path.exists(newpath_ko):
# os.makedirs(newpath_ko)
#
# newpath_ru = savepath+r'\values-ru'
# if not os.path.exists(newpath_ru):
# os.makedirs(newpath_ru)
'''
newpath_ar = savepath+r'\values-ar'
if not os.path.exists(newpath_ar):
os.makedirs(newpath_ar)
newpath_it = savepath+r'\values-it'
if not os.path.exists(newpath_it):
os.makedirs(newpath_it)
newpath_pt = savepath+r'\values-pt'
if not os.path.exists(newpath_pt):
os.makedirs(newpath_pt)
newpath_zh_tw = savepath+r'\values-zh-rTW'
if not os.path.exists(newpath_zh_tw):
os.makedirs(newpath_zh_tw)
'''
#make Xml file
ElementTree(resources_en).write(newpath_en+"\\strings.xml", "utf-8")
# ElementTree(resources_de).write(newpath_de+"\\strings.xml", "utf-8")
# ElementTree(resources_es).write(newpath_es+"\\strings.xml", "utf-8")
# ElementTree(resources_fr).write(newpath_fr+"\\strings.xml", "utf-8")
# ElementTree(resources_hi).write(newpath_hi+"\\strings.xml", "utf-8")
# ElementTree(resources_ja).write(newpath_ja+"\\strings.xml", "utf-8")
# ElementTree(resources_ko).write(newpath_ko+"\\strings.xml", "utf-8")
# ElementTree(resources_ru).write(newpath_ru+"\\strings.xml", "utf-8")
'''
ElementTree(resources_ar).write(newpath_ar+"\\strings.xml", "utf-8")
ElementTree(resources_it).write(newpath_it+"\\strings.xml", "utf-8")
ElementTree(resources_pt).write(newpath_pt+"\\strings.xml", "utf-8")
ElementTree(resources_zh_tw).write(newpath_zh_tw+"\\strings.xml", "utf-8")
'''
SourceCSV.close()
os.remove(file_name)
if __name__=='__main__':
get_gdoc_information_android()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment