from gooey import Gooey, GooeyParser import httplib2 from googleapiclient.discovery import build from oauth2client.service_account import ServiceAccountCredentials from oauth2client.client import AccessTokenRefreshError scope = ["https://www.googleapis.com/auth/androidpublisher"] track_choices = ["alpha", "beta", "production", "rollout"] # Установим параметры Gooey parser = GooeyParser() parser.add_argument( "package_name", metavar="Package name", help="The package name. (e.g. com.android.sample)" ) parser.add_argument( "apk_file", metavar="Apk file", help="The path to the APK file.", widget="FileChooser" ) parser.add_argument( "key_file", metavar="Key file", help="The path to the json key file.", default="./key.json", widget="FileChooser" ) parser.add_argument( "track", metavar="Key file", choices=track_choices, help="Publish branch. May be \"alpha\", \"beta\", \"production\" or \"rollout\".", default="alpha", ) def build_androidpublisher_service(key_filename): creds = ServiceAccountCredentials.from_json_keyfile_name(key_filename, scope) http_auth = creds.authorize(http=httplib2.Http()) return build('androidpublisher', 'v3', http=http_auth) def generate_edit(service, package_name): edit_request = service.edits().insert(body={}, packageName=package_name) result = edit_request.execute() return result['id'] @Gooey( program_name="GooglePlay Publisher", program_description="Publish updates for your apps in easy way!", required_cols=1, optional_cols=1 ) def main(): args = parser.parse_args() package_name, apk_file, key_file = args.package_name, args.apk_file, args.key_file # type: str TRACK = args.track # type: str good_key, good_apk = key_file.endswith(".json"), apk_file.endswith(".apk") if not good_key or not good_apk: _wt = "key" if not good_key else "apk" _ft = "json" if not good_key else "apk" raise Exception(f"Your {_wt} is invalid. It should be .{_ft} file") if TRACK not in track_choices: raise Exception(f"Wrong branch chosen") try: service = build_androidpublisher_service(key_file) edit_id = generate_edit(service, package_name) # Загружаем apk на сервера google apk_response = service.edits().apks().upload( editId=edit_id, packageName=package_name, media_body=apk_file ).execute() # Выводим код версии, как подтверждение успешной загрузки print("Version code {} has been uploaded".format(apk_response['versionCode'])) # Теперь надо установить Track, чтобы apk задеплоились в правильную ветку track_response = service.edits().tracks().update( editId=edit_id, packageName=package_name, track=TRACK, body={"versionCodes": [apk_response['versionCode']]} ).execute() # Выводим сообщение об успешной установке Track'а для VC print("Track {} is set for version code(s) {}".format( track_response['track'], str(track_response['versionCodes']) )) # В конце надо закрепить edit commit_request = service.edits().commit(editId=edit_id, packageName=package_name).execute() # И вывести сообщение об этом print("Edit \"{}\" has been committed".format(commit_request['id'])) except AccessTokenRefreshError: raise Exception("The credentials have been revoked or expired, re-run the app to re-authorize") except Exception as e: raise Exception(str(e.args)) if __name__ == '__main__': try: main() except Exception as e: print(e) raise Exception(f"Error occured! E: {e.args}")