From 6ce50b72ad4dfa75a68e382fef1f24219ba64033 Mon Sep 17 00:00:00 2001 From: Hossein Date: Thu, 7 Aug 2025 03:10:33 +0330 Subject: [PATCH] dodododdo --- schemas.py | 7 +------ scraper.py | 22 ++++++++-------------- 2 files changed, 9 insertions(+), 20 deletions(-) diff --git a/schemas.py b/schemas.py index cda4f93..779e75c 100644 --- a/schemas.py +++ b/schemas.py @@ -10,15 +10,10 @@ class ScrapeRequest(BaseModel): max_count: int = Field(default=5, ge=1, le=50) -class MediaItem(BaseModel): - media_type: str # "image" or "video" - url: str # ✅ only S3/MinIO URL (renamed from local_media) - - class ScrapedPost(BaseModel): ig_post_id: str media_type: str # "image", "video", or "carousel" caption: Optional[str] thumbnail_url: Optional[str] - local_media: List[MediaItem] # ✅ now only contains {"media_type", "url"} + local_media: List[str] # ✅ list of S3 URLs only created_at: datetime diff --git a/scraper.py b/scraper.py index be1cc45..3254f07 100644 --- a/scraper.py +++ b/scraper.py @@ -40,10 +40,10 @@ def scrape_and_store(db: Session, req: ScrapeRequest) -> List[ScrapedPost]: if db.query(InstagramPost).filter_by(ig_post_id=post_id).first(): continue - media_items = [] + media_urls: List[str] = [] try: - # Handle carousel + # Carousel if post.media_type == 8 and hasattr(post, "resources"): for res in post.resources: media_url = res.video_url if res.media_type == 2 else res.thumbnail_url @@ -52,10 +52,7 @@ def scrape_and_store(db: Session, req: ScrapeRequest) -> List[ScrapedPost]: try: local_url = upload_media_from_url(media_url, str(req.seller_id), f"{post_id}/") if local_url: - media_items.append({ - "media_type": "video" if res.media_type == 2 else "image", - "url": local_url - }) + media_urls.append(local_url) except Exception as e: print(f"⚠️ Error uploading media: {e}") continue @@ -66,10 +63,7 @@ def scrape_and_store(db: Session, req: ScrapeRequest) -> List[ScrapedPost]: try: local_url = upload_media_from_url(media_url, str(req.seller_id), f"{post_id}/") if local_url: - media_items.append({ - "media_type": "video" if post.media_type == 2 else "image", - "url": local_url - }) + media_urls.append(local_url) except Exception as e: print(f"⚠️ Error uploading media: {e}") continue @@ -81,10 +75,10 @@ def scrape_and_store(db: Session, req: ScrapeRequest) -> List[ScrapedPost]: print(f"⚠️ Unexpected error while handling media: {e}") continue - if not media_items: + if not media_urls: continue - # ✅ Upload thumbnail safely + # ✅ Upload and override thumbnail final_thumbnail = post.thumbnail_url or "" if post.thumbnail_url: try: @@ -101,7 +95,7 @@ def scrape_and_store(db: Session, req: ScrapeRequest) -> List[ScrapedPost]: media_type="carousel" if post.media_type == 8 else ("video" if post.media_type == 2 else "image"), caption=post.caption_text or "", thumbnail_url=final_thumbnail, - local_media=media_items, + local_media=media_urls, # ✅ Now a list of strings (S3 URLs) created_at=post.taken_at or datetime.utcnow(), ) db.add(post_entry) @@ -113,7 +107,7 @@ def scrape_and_store(db: Session, req: ScrapeRequest) -> List[ScrapedPost]: media_type=post_entry.media_type, caption=post_entry.caption, thumbnail_url=final_thumbnail, - local_media=media_items, + local_media=media_urls, # ✅ Again, list of strings created_at=post_entry.created_at ))