Skip to content
Snippets Groups Projects
Commit 38209cc3 authored by Tomáš Valenta's avatar Tomáš Valenta
Browse files

use django cache instead of requests-cache

parent e2f1dfcd
No related branches found
No related tags found
2 merge requests!787Release,!749Add personal calendars, move from requests-cache to Django cache
......@@ -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(
......
......@@ -12,7 +12,6 @@ pirates<=0.7
whitenoise
opencv-python
requests
requests-cache
icalevnt
ics
arrow
......
#
# 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
......
#
# 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
#
......
#
# 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
#
......
......@@ -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"]
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment