-
Notifications
You must be signed in to change notification settings - Fork 4
/
localizator.py
executable file
·161 lines (134 loc) · 4.97 KB
/
localizator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
from __future__ import print_function
try:
import httplib2
import urllib3
from apiclient import errors
except ImportError:
print("run pip3 install httplib2")
import os
try:
from apiclient import discovery
except ImportError:
print("run `pip3 install google-api-python-client`\n "
"or manually on https://developers.google.com/api-client-library/python/start/installation")
import oauth2client
from oauth2client import client
from oauth2client import tools
import translations
try:
import argparse
parser = argparse.ArgumentParser(parents=[tools.argparser], description='Create localizable files')
parser.add_argument('--id', help='provide file id to avoid prompt')
parser.add_argument('--path', help='Path destination for *.lproj folders', default='./')
parser.add_argument('--platform', choices=['ios', 'android'], help='Should be either ios or android', default='ios')
parser.add_argument('--gid', help='Use the Google sheet ID from the end of the url link')
parser.add_argument('--keep_csv', type=bool, help='Should keep the CSV file on the disk', default=False)
args = parser.parse_args()
flags = args
except ImportError:
flags = None
print("Cannot parse")
SCOPES = 'https://www.googleapis.com/auth/drive'
CLIENT_SECRET_FILE = 'client_secret.json'
APPLICATION_NAME = 'Drive API Python Quickstart'
def get_credentials():
"""Gets valid user credentials from storage.
If nothing has been stored, or if the stored credentials are invalid,
the OAuth2 flow is completed to obtain the new credentials.
Returns:
Credentials, the obtained credential.
"""
home_dir = os.path.expanduser('~')
credential_dir = os.path.join(home_dir, '.credentials')
if not os.path.exists(credential_dir):
os.makedirs(credential_dir)
credential_path = os.path.join(credential_dir, 'drive-python-quickstart.json')
store = oauth2client.file.Storage(credential_path)
credentials = store.get()
if not credentials or credentials.invalid:
flow = client.flow_from_clientsecrets(CLIENT_SECRET_FILE, SCOPES)
flow.user_agent = APPLICATION_NAME
if flags:
credentials = tools.run_flow(flow, store, flags)
else: # Needed only for compatibility with Python 2.6
credentials = tools.run(flow, store)
print('Storing credentials to ' + credential_path)
return credentials
def getFiles(service):
"""
Retrieve a list of File resources.
Args:
service: Drive API service instance.
Returns:
List of File resources.
"""
result = []
page_token = None
while True:
try:
param = {}
if page_token:
param['pageToken'] = page_token
param['maxResults'] = '1000'
files = service.files().list(**param).execute()
result.extend(files['items'])
page_token = files.get('nextPageToken')
if not page_token:
break
except errors.HttpError as error:
print('An error occurred: %s' % error)
break
return service, result
def download__file_metadata(file_id, token, gid=0):
file_id = file_id
url = "https://docs.google.com/spreadsheets/d/"+file_id+"/export?gid="+str(gid)+"&format=csv"
headers = {"Authorization": "Bearer "+str(token)}
r = urllib3.PoolManager().request('GET', url=url, headers=headers)
return r.data
def main():
"""Shows basic usage of the Google Drive API.
Creates a Google Drive API service object and outputs the names and IDs
for up to 10 files.
"""
credentials = get_credentials()
credentials.authorize(httplib2.Http())
token = str(credentials.access_token)
if args.id:
file = download__file_metadata(args.id, token, args.gid)
else:
i = 0
service, files = getFiles(service)
for item in files:
print(str(item['title']) + " - " + str(item['id']))
i += 1
exit(1)
content = file
filename = "tmp" + '.csv'
csvf = open(filename, 'w')
csvf.write(content.decode("utf-8"))
csvf.close()
if args.platform == 'ios':
translations.translate(filename, args.path)
elif args.platform == 'android':
translations.translate_android(filename, args.path)
else:
print("Invalid platform. type --help for help")
if not args.keep_csv:
os.remove(filename)
print("Your files have been generated under '"+args.path+"'")
def download_file(service, drive_file):
download_url = drive_file['exportLinks']['text/csv']
if args.gid:
download_url += "&gid=" + args.gid
if download_url:
resp, content = service._http.request(download_url)
if resp.status == 200:
return content
else:
print('An error occurred: %s' % resp)
return None
else:
# The file doesn't have any content stored on Drive.
return None
if __name__ == '__main__':
main()