Skip to content

Commit 21f3f89

Browse files
committed
Fix Weblate non JSON return printing, and use a token to avoid Anubis bot checking
Also add an User-Agent because this is a good thing to do. Use Accept to indicate we want json, as documented.
1 parent 22bed93 commit 21f3f89

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

pmaweb/settings.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,8 @@
167167
# List of major versions to offer for download in descending order.
168168
LISTED_BRANCHES = ('5.2','4.9')
169169

170+
WEBLATE_TOKEN = None
171+
170172
CDN_API_TOKEN = None
171173
CDN_ID = '41205'
172174
FILES_CDN_ID = '40483'

translations/management/commands/fetch_translations.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,30 +20,39 @@
2020
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
2121

2222
from django.core.management.base import BaseCommand
23+
from django.conf import settings
2324
from translations.models import Translation
2425
import json
2526
from dateutil import parser
26-
import urllib.request, urllib.parse, urllib.error
27+
from urllib.request import urlopen, Request
2728

2829
class Command(BaseCommand):
2930
help = 'Downloads translation stats'
3031
API_URL = 'https://hosted.weblate.org/api/components/phpmyadmin/master/statistics/'
3132

3233
def handle(self, *args, **options):
34+
if not settings.WEBLATE_TOKEN:
35+
print(("Missing a Weblate token (WEBLATE_TOKEN) in the settings."))
36+
return
3337

38+
headers = {
39+
'User-Agent': 'phpMyAdmin website',
40+
'Accept': 'application/json',
41+
'Authorization': 'Token ' + settings.WEBLATE_TOKEN
42+
}
3443
has_next = True
3544
while has_next:
3645
data = ''
3746
try:
38-
handle = urllib.request.urlopen(self.API_URL)
47+
handle = urlopen(Request(self.API_URL, headers=headers))
3948
data = handle.read()
4049
content = json.loads(data)
4150
has_next = content['next'] is not None
4251
self.API_URL = content['next']
4352
except ValueError:
4453
print("There was a problem parsing the data from Hosted Weblate.")
4554
print(("Check the status of the feed page: " + self.API_URL))
46-
print(("Data: " + data))
55+
print(("Data: " + data.decode("utf-8")))
4756
import sys
4857
sys.exit(1)
4958
self.handle_content(content['results'])

0 commit comments

Comments
 (0)