add alternatives for deprecated utc datetime functions

This commit is contained in:
Mike Fährmann
2024-09-19 20:09:03 +02:00
parent a051e1c955
commit 2c7a0c3ca8
7 changed files with 68 additions and 19 deletions

View File

@@ -9,9 +9,9 @@
"""Extractors for https://8chan.moe/"""
from .common import Extractor, Message
from .. import text
from .. import text, util
from ..cache import memcache
from datetime import datetime, timedelta
from datetime import timedelta
import itertools
BASE_PATTERN = r"(?:https?://)?8chan\.(moe|se|cc)"
@@ -34,14 +34,14 @@ class _8chanExtractor(Extractor):
def cookies_prepare(self):
# fetch captcha cookies
# (necessary to download without getting interrupted)
now = datetime.utcnow()
now = util.datetime_utcnow()
url = self.root + "/captcha.js"
params = {"d": now.strftime("%a %b %d %Y %H:%M:%S GMT+0000 (UTC)")}
self.request(url, params=params).content
# adjust cookies
# - remove 'expires' timestamp
# - move 'captchaexpiration' value forward by 1 month)
# - move 'captchaexpiration' value forward by 1 month
domain = self.root.rpartition("/")[2]
for cookie in self.cookies:
if cookie.domain.endswith(domain):

View File

@@ -448,7 +448,8 @@ class PixivRankingExtractor(PixivExtractor):
self.log.warning("invalid date '%s'", date)
date = None
if not date:
date = (datetime.utcnow() - timedelta(days=1)).strftime("%Y-%m-%d")
now = util.datetime_utcnow()
date = (now - timedelta(days=1)).strftime("%Y-%m-%d")
self.date = date
return {"ranking": {
@@ -887,7 +888,7 @@ class PixivAppAPI():
"get_secure_url": "1",
}
time = datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%S+00:00")
time = util.datetime_utcnow().strftime("%Y-%m-%dT%H:%M:%S+00:00")
headers = {
"X-Client-Time": time,
"X-Client-Hash": hashlib.md5(

View File

@@ -9,7 +9,9 @@
"""Collection of functions that work on strings/text"""
import re
import sys
import html
import time
import datetime
import urllib.parse
@@ -247,12 +249,23 @@ def parse_query(qs):
return result
def parse_timestamp(ts, default=None):
"""Create a datetime object from a unix timestamp"""
try:
return datetime.datetime.utcfromtimestamp(int(ts))
except Exception:
return default
if sys.hexversion < 0x30c0000:
# Python <= 3.11
def parse_timestamp(ts, default=None):
"""Create a datetime object from a Unix timestamp"""
try:
return datetime.datetime.utcfromtimestamp(int(ts))
except Exception:
return default
else:
# Python >= 3.12
def parse_timestamp(ts, default=None):
"""Create a datetime object from a Unix timestamp"""
try:
Y, m, d, H, M, S, _, _, _ = time.gmtime(int(ts))
return datetime.datetime(Y, m, d, H, M, S)
except Exception:
return default
def parse_datetime(date_string, format="%Y-%m-%dT%H:%M:%S%z", utcoffset=0):

View File

@@ -218,18 +218,34 @@ def to_string(value):
def datetime_to_timestamp(dt):
"""Convert naive UTC datetime to timestamp"""
"""Convert naive UTC datetime to Unix timestamp"""
return (dt - EPOCH) / SECOND
def datetime_to_timestamp_string(dt):
"""Convert naive UTC datetime to timestamp string"""
"""Convert naive UTC datetime to Unix timestamp string"""
try:
return str((dt - EPOCH) // SECOND)
except Exception:
return ""
if sys.hexversion < 0x30c0000:
# Python <= 3.11
datetime_utcfromtimestamp = datetime.datetime.utcfromtimestamp
datetime_utcnow = datetime.datetime.utcnow
datetime_from_timestamp = datetime_utcfromtimestamp
else:
# Python >= 3.12
def datetime_from_timestamp(ts=None):
"""Convert Unix timestamp to naive UTC datetime"""
Y, m, d, H, M, S, _, _, _ = time.gmtime(ts)
return datetime.datetime(Y, m, d, H, M, S)
datetime_utcfromtimestamp = datetime_from_timestamp
datetime_utcnow = datetime_from_timestamp
def json_default(obj):
if isinstance(obj, CustomNone):
return None