14 socials integration (#34)

* Create XWrapper.py & ChanWrapper.py
* Tests for in XWrapper & ChanWrapper
* MAX_COMMENTS in social.py
* Soddisfatto Giacomo
* unified_timestamp
This commit was merged in pull request #34.
This commit is contained in:
Nunzi99
2025-10-20 16:56:11 +02:00
committed by GitHub
parent 3adf7ed250
commit 06c660b659
13 changed files with 242 additions and 27 deletions

View File

@@ -0,0 +1,22 @@
from datetime import datetime
def unified_timestamp(timestamp_ms: int | None = None, timestamp_s: int | None = None) -> str:
"""
Transform the timestamp from milliseconds or seconds to a unified string format.
The resulting string is a formatted string 'YYYY-MM-DD HH:MM'.
Args:
timestamp_ms: Timestamp in milliseconds.
timestamp_s: Timestamp in seconds.
Raises:
ValueError: If neither timestamp_ms nor timestamp_s is provided.
"""
if timestamp_ms is not None:
timestamp = timestamp_ms // 1000
elif timestamp_s is not None:
timestamp = timestamp_s
else:
raise ValueError("Either timestamp_ms or timestamp_s must be provided")
assert timestamp > 0, "Invalid timestamp data received"
return datetime.fromtimestamp(timestamp).strftime('%Y-%m-%d %H:%M')

View File

@@ -1,6 +1,6 @@
import statistics
from datetime import datetime
from pydantic import BaseModel
from app.api.core import unified_timestamp
class ProductInfo(BaseModel):
@@ -64,24 +64,8 @@ class Price(BaseModel):
"""Timestamp in format YYYY-MM-DD HH:MM"""
def set_timestamp(self, timestamp_ms: int | None = None, timestamp_s: int | None = None) -> None:
"""
Sets the timestamp from milliseconds or seconds.
The timestamp is saved as a formatted string 'YYYY-MM-DD HH:MM'.
Args:
timestamp_ms: Timestamp in milliseconds.
timestamp_s: Timestamp in seconds.
Raises:
ValueError: If neither timestamp_ms nor timestamp_s is provided.
"""
if timestamp_ms is not None:
timestamp = timestamp_ms // 1000
elif timestamp_s is not None:
timestamp = timestamp_s
else:
raise ValueError("Either timestamp_ms or timestamp_s must be provided")
assert timestamp > 0, "Invalid timestamp data received"
self.timestamp = datetime.fromtimestamp(timestamp).strftime('%Y-%m-%d %H:%M')
""" Use the unified_timestamp function to set the timestamp."""
self.timestamp = unified_timestamp(timestamp_ms, timestamp_s)
@staticmethod
def aggregate(prices: dict[str, list['Price']]) -> list['Price']:

View File

@@ -1,6 +1,10 @@
from pydantic import BaseModel
from app.api.core import unified_timestamp
MAX_COMMENTS = 5
class SocialPost(BaseModel):
"""
Represents a social media post with time, title, description, and comments.
@@ -10,6 +14,10 @@ class SocialPost(BaseModel):
description: str = ""
comments: list["SocialComment"] = []
def set_timestamp(self, timestamp_ms: int | None = None, timestamp_s: int | None = None) -> None:
""" Use the unified_timestamp function to set the time."""
self.time = unified_timestamp(timestamp_ms, timestamp_s)
class SocialComment(BaseModel):
"""
Represents a comment on a social media post.
@@ -17,6 +25,10 @@ class SocialComment(BaseModel):
time: str = ""
description: str = ""
def set_timestamp(self, timestamp_ms: int | None = None, timestamp_s: int | None = None) -> None:
""" Use the unified_timestamp function to set the time."""
self.time = unified_timestamp(timestamp_ms, timestamp_s)
class SocialWrapper:
"""