94 lines
2.9 KiB
Python
94 lines
2.9 KiB
Python
from sqlalchemy.orm import Session
|
|
from database import InstagramAccount, InstagramPost
|
|
from utils.boxapi_client import fetch_boxapi_posts
|
|
from utils.s3_uploader import upload_media_from_url
|
|
from schemas import ScrapeRequest, ScrapedPost
|
|
from typing import List
|
|
from datetime import datetime
|
|
|
|
|
|
def scrape_and_store(db: Session, req: ScrapeRequest) -> List[ScrapedPost]:
|
|
posts = fetch_boxapi_posts(req.username, req.max_count)
|
|
if not posts:
|
|
return []
|
|
|
|
# Get or create InstagramAccount
|
|
account = db.query(InstagramAccount).filter_by(username=req.username).first()
|
|
if not account:
|
|
account = InstagramAccount(
|
|
username=req.username,
|
|
seller_id=req.seller_id,
|
|
is_active=True,
|
|
last_synced=None
|
|
)
|
|
db.add(account)
|
|
db.commit()
|
|
db.refresh(account)
|
|
|
|
result: List[ScrapedPost] = []
|
|
new_post_count = 0
|
|
|
|
for post in posts:
|
|
post_id = str(post.get("id") or post.get("pk"))
|
|
caption = post.get("caption", "")
|
|
thumbnail_url = post.get("thumbnail_url")
|
|
taken_at = post.get("taken_at") or datetime.utcnow()
|
|
media_type = post.get("media_type") or "image"
|
|
|
|
if db.query(InstagramPost).filter_by(ig_post_id=post_id).first():
|
|
continue
|
|
|
|
media_urls: List[str] = []
|
|
for media in post.get("media", []):
|
|
media_url = media.get("media_url")
|
|
if not media_url:
|
|
continue
|
|
try:
|
|
local_url = upload_media_from_url(media_url, str(req.seller_id), f"{post_id}/")
|
|
if local_url:
|
|
media_urls.append(local_url)
|
|
except Exception as e:
|
|
print(f"⚠️ Error uploading media: {e}")
|
|
continue
|
|
|
|
if not media_urls:
|
|
continue
|
|
|
|
# Upload thumbnail (optional)
|
|
final_thumbnail = thumbnail_url
|
|
if thumbnail_url:
|
|
try:
|
|
thumb_uploaded = upload_media_from_url(thumbnail_url, str(req.seller_id), f"{post_id}/thumbnail")
|
|
if thumb_uploaded:
|
|
final_thumbnail = thumb_uploaded
|
|
except Exception as e:
|
|
print(f"⚠️ Failed to upload thumbnail: {e}")
|
|
|
|
# Save to DB
|
|
post_entry = InstagramPost(
|
|
ig_post_id=post_id,
|
|
account_id=account.id,
|
|
media_type=media_type,
|
|
caption=caption,
|
|
thumbnail_url=final_thumbnail,
|
|
local_media=media_urls,
|
|
created_at=taken_at,
|
|
)
|
|
db.add(post_entry)
|
|
db.commit()
|
|
|
|
result.append(ScrapedPost(
|
|
ig_post_id=post_id,
|
|
media_type=media_type,
|
|
caption=caption,
|
|
thumbnail_url=final_thumbnail,
|
|
local_media=media_urls,
|
|
created_at=taken_at
|
|
))
|
|
|
|
new_post_count += 1
|
|
if new_post_count >= req.max_count:
|
|
break
|
|
|
|
return result
|