diff --git a/instagram_utils/services.py b/instagram_utils/services.py index 10b1754c836f11a7a44c13a627448811ea61cf48..bc167bf5fefd2f02ec0a05abb59853a4d099b8e0 100644 --- a/instagram_utils/services.py +++ b/instagram_utils/services.py @@ -2,7 +2,7 @@ import datetime import logging import io import os -import requests_cache +import requests from django.core.files import File from main.models import MainHomePage, MainPersonPage @@ -19,8 +19,6 @@ class InstagramDownloadService: """ def __init__(self, app_id: int, app_secret: str): - self.session = requests_cache.CachedSession("instagram_cache") - self.app_id = app_id self.app_secret = app_secret @@ -51,7 +49,7 @@ class InstagramDownloadService: def download_remote_image(self, image_url) -> (str, File): try: - response = self.session.get(image_url) + response = requests.get(image_url) response.raise_for_status() except Exception as exc: logger.warning( @@ -63,7 +61,7 @@ class InstagramDownloadService: return os.path.basename(image_url), File(io.BytesIO(response.content)) def get_user_data(self, access_token: str) -> dict: - user_data = self.session.get( + user_data = requests.get( f"https://graph.instagram.com/v16.0/me?access_token={access_token}" "&fields=id,username" ) @@ -72,12 +70,11 @@ class InstagramDownloadService: return user_data.json() def get_recent_media(self, user_data: dict, access_token: str) -> list[dict]: - with self.session.cache_disabled(): - recent_media = self.session.get( - f"https://graph.instagram.com/v16.0/{user_data['id']}/media?access_token=" - f"{access_token}&fields=id,timestamp,caption,media_type,permalink," - "media_url,thumbnail_url" - ) + recent_media = requests.get( + f"https://graph.instagram.com/v16.0/{user_data['id']}/media?access_token=" + f"{access_token}&fields=id,timestamp,caption,media_type,permalink," + "media_url,thumbnail_url" + ) if not recent_media.ok: logger.warning( diff --git a/requirements/base.in b/requirements/base.in index d7f49b0fd4baac6d2af16bc6fd0189e83deafee2..d42e722b5768c98aedf409f64cf75e416786aefa 100644 --- a/requirements/base.in +++ b/requirements/base.in @@ -12,7 +12,6 @@ pirates<=0.7 whitenoise opencv-python requests -requests-cache icalevnt ics arrow diff --git a/requirements/base.txt b/requirements/base.txt index c31ed644a1767c846441ea0bdce54d3dc8e2900f..9c49218f591d4a40c643d5eb9cef52b4f0475cac 100644 --- a/requirements/base.txt +++ b/requirements/base.txt @@ -1,6 +1,6 @@ # -# This file is autogenerated by pip-compile with python 3.10 -# To update, run: +# This file is autogenerated by pip-compile with Python 3.10 +# by the following command: # # pip-compile base.in # @@ -8,8 +8,6 @@ amqp==5.1.1 # via kombu anyascii==0.3.2 # via wagtail -appnope==0.1.3 - # via ipython arrow==1.2.3 # via # -r base.in diff --git a/requirements/dev.txt b/requirements/dev.txt index e3a023da69a1c8d99ccd3a800807e4be76c269a6..827deebf62a58817800d6f2cd00eb4d2c7e0a599 100644 --- a/requirements/dev.txt +++ b/requirements/dev.txt @@ -1,6 +1,6 @@ # -# This file is autogenerated by pip-compile with python 3.10 -# To update, run: +# This file is autogenerated by pip-compile with Python 3.10 +# by the following command: # # pip-compile dev.in # diff --git a/requirements/production.txt b/requirements/production.txt index 3333386f4e89a4345670321d51b646a4098aa598..4f1ff713e8bdd33fadf6ca9bece8acbfc4575046 100644 --- a/requirements/production.txt +++ b/requirements/production.txt @@ -1,6 +1,6 @@ # -# This file is autogenerated by pip-compile with python 3.10 -# To update, run: +# This file is autogenerated by pip-compile with Python 3.10 +# by the following command: # # pip-compile production.in # diff --git a/shared/blocks.py b/shared/blocks.py index 869a45551714a4a4f23442f72b47135a7d125f94..6b741f2baf48294ec7b8f23bca85d3d0296e0cda 100644 --- a/shared/blocks.py +++ b/shared/blocks.py @@ -5,7 +5,8 @@ import re import typing import urllib -import requests_cache +import requests +from django.core.cache import cache from django.core.exceptions import ValidationError from django.core.files.images import ImageFile from django.forms.utils import ErrorList @@ -679,12 +680,7 @@ class ChartDataset(blocks.StructBlock): def get_redmine_projects(): - session = requests_cache.CachedSession( - "redmine_cache", - expire_after=datetime.timedelta(hours=1), - ) - - projects = session.get("https://redmine.pirati.cz/projects.json?limit=10000") + projects = requests.get("https://redmine.pirati.cz/projects.json?limit=10000") projects.raise_for_status() projects = projects.json()["projects"] @@ -774,14 +770,14 @@ class ChartRedmineIssueDataset(blocks.StructBlock): return url def _get_parsed_issues(self, value, labels, issues_url) -> tuple: - session = requests_cache.CachedSession( - "redmine_cache", - expire_after=datetime.timedelta(days=14), - ) + issues_response = cache.get(f"redmine_{issues_url}") + + if issues_response is None: + issues_response = requests.get(issues_url) + issues_response.raise_for_status() + issues_response = issues_response.json() - issues_response = session.get(issues_url) - issues_response.raise_for_status() - issues_response = issues_response.json() + cache.set(f"redmine_{issues_url}", issues_response) only_grow = value.get("only_grow", False) @@ -790,10 +786,16 @@ class ChartRedmineIssueDataset(blocks.StructBlock): while issues_response["total_count"] - offset > len(issues_response["issues"]): offset += 100 + url_with_offset = f"{issues_url}&offset={offset}" - issues_response = session.get(f"{issues_url}&offset={offset}") - issues_response.raise_for_status() - issues_response = issues_response.json() + issues_response = cache.get(f"redmine_{url_with_offset}") + + if issues_response is None: + issues_response = requests.get(url_with_offset) + issues_response.raise_for_status() + issues_response = issues_response.json() + + cache.set(f"redmine_{url_with_offset}", issues_response) collected_issues += issues_response["issues"]