Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 21 additions & 24 deletions instaparser/article.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
Article class representing a parsed article from Instaparser.
"""

from dataclasses import dataclass, field
from typing import Any


@dataclass(repr=False)
class Article:
"""
Represents a parsed article from Instaparser.
Expand All @@ -27,30 +29,25 @@ class Article:
markdown: The markdown body (if output was 'markdown')
"""

def __init__(self, data: dict[str, Any]):
"""
Initialize an Article from API response data.

Args:
data: Dictionary containing article data from the API
"""
self.url = data.get("url")
self.title = data.get("title")
self.site_name = data.get("site_name")
self.author = data.get("author")
self.date = data.get("date")
self.description = data.get("description")
self.thumbnail = data.get("thumbnail")
self.words = data.get("words", 0)
self.is_rtl = data.get("is_rtl", False)
self.images = data.get("images", [])
self.videos = data.get("videos", [])

self.html = data.get("html")
self.text = data.get("text")
self.markdown = data.get("markdown")

self.body = self.html or self.text or self.markdown
url: str | None = None
title: str | None = None
site_name: str | None = None
author: str | None = None
date: Any | None = None
description: str | None = None
thumbnail: str | None = None
words: int = 0
is_rtl: bool = False
images: list = field(default_factory=list)
videos: list = field(default_factory=list)
html: str | None = None
text: str | None = None
markdown: str | None = None

@property
def body(self) -> str | None:
"""The article body (html if available, otherwise text, then markdown)."""
return self.html or self.text or self.markdown

def __repr__(self) -> str:
return f"<Article url={self.url!r} title={self.title!r}>"
Expand Down
35 changes: 31 additions & 4 deletions instaparser/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,22 @@ def Article(self, url: str, content: str | None = None, output: str = "html", us

response = self.session.post(endpoint, json=payload)
data = self._handle_response(response)

return Article(data)
return Article(
url=data.get("url"),
title=data.get("title"),
site_name=data.get("site_name"),
author=data.get("author"),
date=data.get("date"),
description=data.get("description"),
thumbnail=data.get("thumbnail"),
words=data.get("words", 0),
is_rtl=data.get("is_rtl", False),
images=data.get("images", []),
videos=data.get("videos", []),
html=data.get("html"),
text=data.get("text"),
markdown=data.get("markdown"),
)

def Summary(
self,
Expand Down Expand Up @@ -271,5 +285,18 @@ def PDF(
else:
raise InstaparserValidationError("Either 'url' or 'file' must be provided")

data = self._handle_response(response)
return PDF(data)
result = self._handle_response(response)
return PDF(
url=result.get("url"),
title=result.get("title"),
site_name=result.get("site_name"),
author=result.get("author"),
date=result.get("date"),
description=result.get("description"),
thumbnail=result.get("thumbnail"),
words=result.get("words", 0),
images=result.get("images", []),
html=result.get("html"),
text=result.get("text"),
markdown=result.get("markdown"),
)
15 changes: 3 additions & 12 deletions instaparser/pdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@
PDF class representing a parsed PDF from Instaparser.
"""

from typing import Any
from dataclasses import dataclass

from .article import Article


@dataclass(repr=False)
class PDF(Article):
"""
Represents a parsed PDF from Instaparser.
Expand All @@ -15,17 +16,7 @@ class PDF(Article):
PDFs always have is_rtl=False and videos=[].
"""

def __init__(self, data: dict[str, Any]):
"""
Initialize a PDF from API response data.

Args:
data: Dictionary containing PDF data from the API
"""
# Call parent constructor
super().__init__(data)

# PDFs always have is_rtl=False and videos=[]
def __post_init__(self) -> None:
self.is_rtl = False
self.videos = []

Expand Down
Loading