Skip to content

Commit

Permalink
Use Transifex API v3 for generating .tx/config
Browse files Browse the repository at this point in the history
Base on per string calculations for stats in README for backwards compatibility -- this gives 55.39 today vs 48.01 on per word calculations

Old Transifex API stopped to work on Feb 13 at 10AM CET
  • Loading branch information
m-aciek committed Feb 15, 2023
1 parent 2c904bc commit dc2f733
Showing 1 changed file with 31 additions and 9 deletions.
40 changes: 31 additions & 9 deletions manage_translation.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def recreate_tx_config():
)
)
for resource in resources:
slug = resource['slug']
slug = resource.slug
name = RESOURCE_NAME_MAP.get(slug, slug)
if slug == '0':
continue
Expand Down Expand Up @@ -92,6 +92,15 @@ def recreate_tx_config():
)


@dataclass
class Resource:
slug: str

@classmethod
def from_api_v3_entry(cls, data: dict) -> Self:
return cls(slug=data['attributes']['slug'])


@dataclass
class ResourceLanguageStatistics:
name: str
Expand All @@ -111,7 +120,7 @@ def from_api_v3_entry(cls, data: dict) -> Self:
)


def _get_resources():
def _get_from_api_v3_with_cursor(url: str, params: dict):
from requests import get

resources = []
Expand All @@ -123,19 +132,32 @@ def _get_resources():
transifex_api_key = os.getenv('TX_TOKEN')
while True:
response = get(
'https://rest.api.transifex.com/resource_language_stats',
params={
'filter[project]': f'o:python-doc:p:{PROJECT_SLUG}', 'filter[language]': f'l:{LANGUAGE}'
} | ({'page[cursor]': cursor} if cursor else {}),
url,
params=params | ({'page[cursor]': cursor} if cursor else {}),
headers={'Authorization': f'Bearer {transifex_api_key}'}
)
response.raise_for_status()
response_json = response.json()
response_list = response_json['data']
resources.extend(response_list)
if 'next' not in response_json['links']:
if not response_json['links'].get('next'): # for stats no key, for list resources null
break
cursor = unquote(search('page\[cursor]=([^&]*)', response_json['links']['next']).group(1))
return resources


def _get_resources():
resources = _get_from_api_v3_with_cursor(
'https://rest.api.transifex.com/resources', {'filter[project]': f'o:python-doc:p:{PROJECT_SLUG}'}
)
return [Resource.from_api_v3_entry(entry) for entry in resources]


def _get_resource_language_stats():
resources = _get_from_api_v3_with_cursor(
'https://rest.api.transifex.com/resource_language_stats',
{'filter[project]': f'o:python-doc:p:{PROJECT_SLUG}', 'filter[language]': f'l:{LANGUAGE}'}
)
return [ResourceLanguageStatistics.from_api_v3_entry(entry) for entry in resources]


Expand All @@ -161,9 +183,9 @@ def language_switcher(entry):
def average(averages, weights):
return sum([a * w for a, w in zip(averages, weights)]) / sum(weights)

resources = _get_resources()
resources = _get_resource_language_stats()
filtered = list(filter(language_switcher, resources))
average_list = [e.translated_words / e.total_words for e in filtered]
average_list = [e.translated_strings / e.total_strings for e in filtered]
weights_list = [e.total_words for e in filtered]

language_switcher_status = average(average_list, weights=weights_list) * 100
Expand Down

0 comments on commit dc2f733

Please sign in to comment.