Moved tests
This commit is contained in:
parent
f5731ffa35
commit
8fef2e9ef5
|
@ -37,7 +37,7 @@ class LodestoneAchievementScraper(Scraper):
|
||||||
response = self.session.get(url)
|
response = self.session.get(url)
|
||||||
response.raise_for_status()
|
response.raise_for_status()
|
||||||
|
|
||||||
NOW = personal_data.parse_util.response_datetime(response)
|
NOW = personal_data.parse_util.parse_response_datetime(response)
|
||||||
|
|
||||||
# Parse data
|
# Parse data
|
||||||
soup = bs4.BeautifulSoup(response.content, 'lxml')
|
soup = bs4.BeautifulSoup(response.content, 'lxml')
|
||||||
|
|
|
@ -16,34 +16,12 @@ logger = logging.getLogger(__name__)
|
||||||
URL_PROFILE = 'https://psnprofiles.com/{psn_id}'
|
URL_PROFILE = 'https://psnprofiles.com/{psn_id}'
|
||||||
URL_USER_GAME_TROPHIES = 'https://psnprofiles.com/trophies/{game_id}/{psn_id}'
|
URL_USER_GAME_TROPHIES = 'https://psnprofiles.com/trophies/{game_id}/{psn_id}'
|
||||||
|
|
||||||
FORMAT_DAY_MONTH_YEAR = '%d %B %Y'
|
|
||||||
|
|
||||||
|
|
||||||
def game_psnprofiles_id_from_url(relative_url: str) -> int:
|
def game_psnprofiles_id_from_url(relative_url: str) -> int:
|
||||||
m = re.match(r'/(?:trophy|trophies)/(\d+)\-(?:[\w-]+)(/[\w-]*)?', relative_url)
|
m = re.match(r'/(?:trophy|trophies)/(\d+)\-(?:[\w-]+)(/[\w-]*)?', relative_url)
|
||||||
result = m.group(1)
|
result = m.group(1)
|
||||||
return int(result)
|
return int(result)
|
||||||
|
|
||||||
|
|
||||||
assert game_psnprofiles_id_from_url('/trophies/21045-theatrhythm-final-bar-line')
|
|
||||||
assert game_psnprofiles_id_from_url('/trophies/21045-theatrhythm-final-bar-line/')
|
|
||||||
assert game_psnprofiles_id_from_url(
|
|
||||||
'/trophies/21045-theatrhythm-final-bar-line/Jmaanes',
|
|
||||||
)
|
|
||||||
assert game_psnprofiles_id_from_url(
|
|
||||||
'/trophy/21045-theatrhythm-final-bar-line/19-seasoned-hunter',
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def parse_time(text: str) -> datetime.datetime:
|
|
||||||
text = text.replace('\n', ' ')
|
|
||||||
text = text.strip()
|
|
||||||
return datetime.datetime.strptime(text, '%d %b %Y %I:%M:%S %p')
|
|
||||||
|
|
||||||
|
|
||||||
assert parse_time('06 Apr 2024 06:11:42 PM')
|
|
||||||
assert parse_time('26 Mar 2024 7:07:01 PM')
|
|
||||||
|
|
||||||
MAX_GAME_ITERATIONS = 10
|
MAX_GAME_ITERATIONS = 10
|
||||||
|
|
||||||
|
|
||||||
|
@ -76,7 +54,7 @@ class PsnProfilesScraper(Scraper):
|
||||||
response = self.session.get(url)
|
response = self.session.get(url)
|
||||||
response.raise_for_status()
|
response.raise_for_status()
|
||||||
|
|
||||||
NOW = personal_data.parse_util.response_datetime(response)
|
NOW = personal_data.parse_util.parse_response_datetime(response)
|
||||||
|
|
||||||
# Parse data
|
# Parse data
|
||||||
soup = bs4.BeautifulSoup(response.content, 'lxml')
|
soup = bs4.BeautifulSoup(response.content, 'lxml')
|
||||||
|
@ -139,10 +117,7 @@ class PsnProfilesScraper(Scraper):
|
||||||
if len(small_infos) > 2:
|
if len(small_infos) > 2:
|
||||||
time_played_div = small_infos[2]
|
time_played_div = small_infos[2]
|
||||||
time_played_div.sup.extract()
|
time_played_div.sup.extract()
|
||||||
time_played = datetime.datetime.strptime(
|
time_played = personal_data.parse_util.parse_date(time_played_div.get_text())
|
||||||
time_played_div.get_text().strip(),
|
|
||||||
FORMAT_DAY_MONTH_YEAR,
|
|
||||||
).date()
|
|
||||||
else:
|
else:
|
||||||
time_played = None
|
time_played = None
|
||||||
|
|
||||||
|
|
|
@ -19,6 +19,8 @@ DATETIME_UNITS = {
|
||||||
|
|
||||||
FORMAT_DATE_HEADER = '%a, %d %b %Y %H:%M:%S GMT'
|
FORMAT_DATE_HEADER = '%a, %d %b %Y %H:%M:%S GMT'
|
||||||
|
|
||||||
|
FORMAT_DAY_MONTH_YEAR = '%d %B %Y'
|
||||||
|
|
||||||
|
|
||||||
def parse_duration(text: str) -> datetime.timedelta:
|
def parse_duration(text: str) -> datetime.timedelta:
|
||||||
(num, unit) = text.split(' ')
|
(num, unit) = text.split(' ')
|
||||||
|
@ -26,6 +28,16 @@ def parse_duration(text: str) -> datetime.timedelta:
|
||||||
unit = DATETIME_UNITS[unit]
|
unit = DATETIME_UNITS[unit]
|
||||||
return unit * num
|
return unit * num
|
||||||
|
|
||||||
|
def parse_response_datetime(response) -> datetime.datetime:
|
||||||
def response_datetime(response) -> datetime.datetime:
|
|
||||||
return datetime.datetime.strptime(response.headers['Date'], FORMAT_DATE_HEADER)
|
return datetime.datetime.strptime(response.headers['Date'], FORMAT_DATE_HEADER)
|
||||||
|
|
||||||
|
def parse_time(text: str) -> datetime.datetime:
|
||||||
|
text = text.replace('\n', ' ')
|
||||||
|
text = text.strip()
|
||||||
|
return datetime.datetime.strptime(text, '%d %b %Y %I:%M:%S %p')
|
||||||
|
|
||||||
|
def parse_date(text: str) -> datetime.date:
|
||||||
|
return datetime.datetime.strptime(
|
||||||
|
text.strip(),
|
||||||
|
FORMAT_DAY_MONTH_YEAR,
|
||||||
|
).date()
|
||||||
|
|
6
test/test_parse_util.py
Normal file
6
test/test_parse_util.py
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
from personal_data.parse_util import parse_time
|
||||||
|
|
||||||
|
|
||||||
|
def test_parse_tme():
|
||||||
|
assert parse_time('06 Apr 2024 06:11:42 PM')
|
||||||
|
assert parse_time('26 Mar 2024 7:07:01 PM')
|
15
test/test_psnprofiles.py
Normal file
15
test/test_psnprofiles.py
Normal file
|
@ -0,0 +1,15 @@
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
from personal_data.fetchers import psnprofiles
|
||||||
|
|
||||||
|
URLS_AND_IDS = [
|
||||||
|
(21045, '/trophies/21045-theatrhythm-final-bar-line'),
|
||||||
|
(21045, '/trophies/21045-theatrhythm-final-bar-line/'),
|
||||||
|
(21045, '/trophies/21045-theatrhythm-final-bar-line/HelloWorld'),
|
||||||
|
(21045, '/trophy/21045-theatrhythm-final-bar-line/19-seasoned-hunter'),
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize('id, url', URLS_AND_IDS)
|
||||||
|
def test_game_psnprofiles_id_from_url(id, url):
|
||||||
|
assert psnprofiles.game_psnprofiles_id_from_url(url) == id
|
Loading…
Reference in New Issue
Block a user