mirror of
https://github.com/narrrl/holy-flavor-rater.git
synced 2026-11-03 16:41:27 +01:00
fixed worker internet access and switched to requests library for more reliable syncing
This commit is contained in:
1 parent
89a4e51101
commit
ea5d85f31f
4 files changed
+13
-19
No files matched your search
@@ -1,6 +1,6 @@
|
|||||||
import json
|
import json
|
||||||
import os
|
import os
|
||||||
import urllib.request
|
import requests
|
||||||
from django.core.management.base import BaseCommand
|
from django.core.management.base import BaseCommand
|
||||||
from django.core.files import File
|
from django.core.files import File
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
@@ -24,18 +24,14 @@ class Command(BaseCommand):
|
|||||||
|
|
||||||
os.makedirs(os.path.dirname(filepath), exist_ok=True)
|
os.makedirs(os.path.dirname(filepath), exist_ok=True)
|
||||||
|
|
||||||
req = urllib.request.Request(url, headers={'User-Agent': 'Mozilla/5.0'})
|
response = requests.get(url, headers={'User-Agent': 'Mozilla/5.0'}, timeout=10)
|
||||||
with urllib.request.urlopen(req, timeout=10) as response:
|
response.raise_for_status()
|
||||||
content = response.read()
|
|
||||||
|
|
||||||
if not content:
|
|
||||||
return None
|
|
||||||
|
|
||||||
if os.path.exists(filepath):
|
if os.path.exists(filepath):
|
||||||
os.remove(filepath)
|
os.remove(filepath)
|
||||||
|
|
||||||
with open(filepath, 'wb') as f:
|
with open(filepath, 'wb') as f:
|
||||||
f.write(content)
|
f.write(response.content)
|
||||||
|
|
||||||
return f"flavors/{filename}"
|
return f"flavors/{filename}"
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import json
|
import json
|
||||||
import urllib.request
|
import requests
|
||||||
import re
|
import re
|
||||||
import os
|
import os
|
||||||
from django.core.management.base import BaseCommand
|
from django.core.management.base import BaseCommand
|
||||||
@@ -32,18 +32,14 @@ class Command(BaseCommand):
|
|||||||
|
|
||||||
os.makedirs(os.path.dirname(filepath), exist_ok=True)
|
os.makedirs(os.path.dirname(filepath), exist_ok=True)
|
||||||
|
|
||||||
req = urllib.request.Request(url, headers={'User-Agent': 'Mozilla/5.0'})
|
response = requests.get(url, headers={'User-Agent': 'Mozilla/5.0'}, timeout=10)
|
||||||
with urllib.request.urlopen(req, timeout=10) as response:
|
response.raise_for_status()
|
||||||
content = response.read()
|
|
||||||
|
|
||||||
if not content:
|
|
||||||
return None
|
|
||||||
|
|
||||||
if os.path.exists(filepath):
|
if os.path.exists(filepath):
|
||||||
os.remove(filepath)
|
os.remove(filepath)
|
||||||
|
|
||||||
with open(filepath, 'wb') as f:
|
with open(filepath, 'wb') as f:
|
||||||
f.write(content)
|
f.write(response.content)
|
||||||
|
|
||||||
return f"flavors/{filename}"
|
return f"flavors/{filename}"
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
@@ -55,9 +51,9 @@ class Command(BaseCommand):
|
|||||||
self.stdout.write(f"Fetching from {url}...")
|
self.stdout.write(f"Fetching from {url}...")
|
||||||
|
|
||||||
try:
|
try:
|
||||||
req = urllib.request.Request(url, headers={'User-Agent': 'Mozilla/5.0'})
|
response = requests.get(url, headers={'User-Agent': 'Mozilla/5.0'}, timeout=15)
|
||||||
with urllib.request.urlopen(req) as response:
|
response.raise_for_status()
|
||||||
data = json.loads(response.read().decode())
|
data = response.json()
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
self.stdout.write(self.style.ERROR(f"Failed to fetch data: {e}"))
|
self.stdout.write(self.style.ERROR(f"Failed to fetch data: {e}"))
|
||||||
return
|
return
|
||||||
|
|||||||
@@ -9,3 +9,4 @@ whitenoise>=6.6.0
|
|||||||
gunicorn>=21.2.0
|
gunicorn>=21.2.0
|
||||||
django-ratelimit>=4.1.0
|
django-ratelimit>=4.1.0
|
||||||
django-compression-middleware>=0.5.0
|
django-compression-middleware>=0.5.0
|
||||||
|
requests>=2.31.0
|
||||||
@@ -41,6 +41,7 @@ services:
|
|||||||
command: python manage.py worker
|
command: python manage.py worker
|
||||||
restart: unless-stopped
|
restart: unless-stopped
|
||||||
networks:
|
networks:
|
||||||
|
- narl
|
||||||
- internal
|
- internal
|
||||||
dns:
|
dns:
|
||||||
- 8.8.8.8
|
- 8.8.8.8
|
||||||
|
|||||||
Reference in new issue
Block a user