Scraping & parsing for Steam

This commit is contained in:
stijndcl 2022-10-13 22:31:45 +02:00
parent deefeb1106
commit 855f60727b
10 changed files with 338 additions and 21 deletions

View file

@ -1,5 +1,7 @@
import asyncio
from typing import AsyncGenerator, Generator
import json
import pathlib
from typing import AsyncGenerator, Generator, Union
from unittest.mock import MagicMock
import pytest
@ -66,3 +68,22 @@ def mock_client() -> Didier:
mock_client.user = mock_user
return mock_client
"""Data providers"""
def _provide(name: str) -> Union[dict, str]:
location = pathlib.Path(__file__).parent / "test_data" / name
with open(location, "r") as fp:
if name.endswith(".json"):
return json.load(fp)
return fp.read()
@pytest.fixture
def free_games_response() -> str:
"""Fixture to get an example response from the free games RSS feed"""
return _provide("free_games.rss")

View file

@ -0,0 +1,40 @@
<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:media="http://search.yahoo.com/mrss/" xmlns:wp="http://wordpress.org/export/1.2/" xmlns:excerpt="http://wordpress.org/export/1.2/excerpt/" >
<channel>
<title>pepeizq&#039;s deals</title>
<description>Follow the latest deals for PC games from legit stores such as Steam, Humble, Fanatical, Gamesplanet, GOG and more</description>
<link>https://pepeizqdeals.com</link>
<lastBuildDate>Thu, 13 Oct 2022 17:11:24 +0000</lastBuildDate>
<item>
<title><![CDATA[Minion Masters &#8211; Torment • Free • Steam]]></title>
<link><![CDATA[https://pepeizqdeals.com/55623/minion-masters-torment-free-steam/]]></link>
<pubDate>Thu, 13 Oct 2022 18:08:41 +0100</pubDate>
<dc:creator>pepeizq</dc:creator>
<dc:identifier>55623</dc:identifier>
<dc:modified>2022-10-13 18:08:59</dc:modified>
<dc:created unix="1665684521">2022-10-13 18:08:41</dc:created>
<guid isPermaLink="true"><![CDATA[https://pepeizqdeals.com/55623/minion-masters-torment-free-steam/]]></guid><category>12</category>
<description><![CDATA[]]></description><content:encoded><![CDATA[]]></content:encoded><enclosure url="https://pepeizqdeals.com/wp-content/uploads/2022/10/imagenWeb286-19-8-510-en.webp"/><media:content url="https://pepeizqdeals.com/wp-content/uploads/2022/10/imagenWeb286-19-8-510-en.webp" height="150" width="150" type="image/jpeg"/>
</item>
<item>
<title><![CDATA[Darkwood + ToeJam &#038; Earl: Back in the Groove! • Free • Epic Games Store]]></title>
<link><![CDATA[https://pepeizqdeals.com/55616/darkwood-toejam-earl-back-in-the-groove-free-epic-games-store/]]></link>
<pubDate>Thu, 13 Oct 2022 17:03:59 +0100</pubDate>
<dc:creator>pepeizq</dc:creator>
<dc:identifier>55616</dc:identifier>
<dc:modified>2022-10-13 17:04:17</dc:modified>
<dc:created unix="1665680639">2022-10-13 17:03:59</dc:created>
<guid isPermaLink="true"><![CDATA[https://pepeizqdeals.com/55616/darkwood-toejam-earl-back-in-the-groove-free-epic-games-store/]]></guid><category>12</category>
<description><![CDATA[]]></description><content:encoded><![CDATA[]]></content:encoded><enclosure url="https://pepeizqdeals.com/wp-content/uploads/2022/10/imagenWeb286-18-3-139-en.webp"/><media:content url="https://pepeizqdeals.com/wp-content/uploads/2022/10/imagenWeb286-18-3-139-en.webp" height="150" width="150" type="image/jpeg"/>
</item>
<item>
<title><![CDATA[Rebel Inc: Escalation &#8211; Sand &#038; Secrets • Free • Steam]]></title>
<link><![CDATA[https://pepeizqdeals.com/54874/rebel-inc-escalation-sand-secrets-free-steam/]]></link>
<pubDate>Tue, 20 Sep 2022 18:08:52 +0100</pubDate>
<dc:creator>pepeizq</dc:creator>
<dc:identifier>54874</dc:identifier>
<dc:modified>2022-09-20 18:09:03</dc:modified>
<dc:created unix="1663697332">2022-09-20 18:08:52</dc:created>
<guid isPermaLink="true"><![CDATA[https://pepeizqdeals.com/54874/rebel-inc-escalation-sand-secrets-free-steam/]]></guid><category>12</category>
<description><![CDATA[]]></description><content:encoded><![CDATA[]]></content:encoded><enclosure url=""/><media:content url="" height="" width="" type=""/>
</item></channel></rss><!-- end of xml string -->

View file

@ -0,0 +1,17 @@
from sqlalchemy import select
from sqlalchemy.ext.asyncio import AsyncSession
from database.crud import free_games as crud
from database.schemas import FreeGame
async def test_add_games(postgres: AsyncSession):
"""Test adding new games"""
statement = select(FreeGame)
games = (await postgres.execute(statement)).scalars().all()
assert not games
await crud.add_free_games(postgres, [1, 2, 3, 4])
games = (await postgres.execute(statement)).scalars().all()
assert len(games) == 4