Skip to content

Instantly share code, notes, and snippets.

@kzar
Created September 16, 2015 16:11
Show Gist options
  • Select an option

  • Save kzar/5a31685c96fdd3cdea58 to your computer and use it in GitHub Desktop.

Select an option

Save kzar/5a31685c96fdd3cdea58 to your computer and use it in GitHub Desktop.
A quick script to clear all files and disable most languages for a Crowdin project. (Code modified from adblockplus/cms)
#!/usr/bin/python
# coding: utf-8
# This file is part of the Adblock Plus web scripts,
# Copyright (C) 2006-2015 Eyeo GmbH
#
# Adblock Plus is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 3 as
# published by the Free Software Foundation.
#
# Adblock Plus is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
import json
import logging
import sys
import urllib
import urllib3
class CrowdinAPI:
FILES_PER_REQUEST = 20
def __init__(self, project_name, api_key):
self.api_key = api_key
self.project_name = project_name
self.connection = urllib3.connection_from_url("https://api.crowdin.com/")
def raw_request(self, request_method, api_endpoint, query_params=(), **kwargs):
url = "/api/project/%s/%s?%s" % (
urllib.quote(self.project_name),
urllib.quote(api_endpoint),
urllib.urlencode((("key", self.api_key),) + query_params)
)
try:
response = self.connection.request(
request_method, str(url), **kwargs
)
except urllib3.exceptions.HTTPError:
logging.error("Connection to API endpoint %s failed", url)
raise
if response.status < 200 or response.status >= 300:
logging.error("API call to %s failed:\n%s", url, response.data)
raise urllib3.exceptions.HTTPError(response.status)
return response
def request(self, request_method, api_endpoint, data=None, files=None):
fields = []
if data:
for name, value in data.iteritems():
if isinstance(value, basestring):
fields.append((name, value))
else:
fields.extend((name + "[]", v) for v in value)
if files:
fields.extend(("files[%s]" % f[0], f) for f in files)
response = self.raw_request(
request_method, api_endpoint, (("json", "1"),),
fields=fields, preload_content=False
)
try:
return json.load(response)
except ValueError:
logging.error("Invalid response returned by API endpoint %s", url)
raise
def list_files(project_info):
def parse_file_node(node, path=""):
if node["node_type"] == "file":
remote_files.add(path + node["name"])
elif node["node_type"] == "directory":
dir_name = path + node["name"]
remote_directories.add(dir_name)
for file in node.get("files", []):
parse_file_node(file, dir_name + "/")
remote_files = set()
remote_directories = set()
for node in project_info["files"]:
parse_file_node(node)
return remote_files, remote_directories
def remove_files(crowdin_api, files):
for file_name in files:
logging.info("Removing file %s", file_name)
crowdin_api.request("POST", "delete-file", data={"file": file_name})
def remove_directories(crowdin_api, directories):
for directory in reversed(sorted(directories, key=len)):
logging.info("Removing directory %s", directory)
crowdin_api.request("POST", "delete-directory", data={"name": directory})
if __name__ == "__main__":
if len(sys.argv) != 3:
print >>sys.stderr, "Usage: ./crowdin-reset.py crowdin_project_name project_api_key"
sys.exit(1)
crowdin_api = CrowdinAPI(*sys.argv[1:])
project_info = crowdin_api.request("GET", "info")
files, directories = list_files(project_info)
# Clear files and directories
remove_files(crowdin_api, files)
remove_directories(crowdin_api, directories)
# Clear all enabled languages except German (There must be at least one enabled)
crowdin_api.request(
"POST", "edit-project",
data={"languages": ["de"]}
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment