Skip to content

Commit 51ccd8a

Browse files
authored
Consolidate all models into models.py (#14)
Also convert Summary to a dataclass for consistency
1 parent dc95cd9 commit 51ccd8a

File tree

9 files changed

+101
-134
lines changed

9 files changed

+101
-134
lines changed

instaparser/__init__.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
A Python client library for the Instaparser API.
55
"""
66

7-
from .article import Article
87
from .client import InstaparserClient
98
from .exceptions import (
109
InstaparserAPIError,
@@ -13,8 +12,7 @@
1312
InstaparserRateLimitError,
1413
InstaparserValidationError,
1514
)
16-
from .pdf import PDF
17-
from .summary import Summary
15+
from .models import PDF, Article, Summary
1816

1917
__version__ = "1.0.1"
2018

instaparser/client.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,13 @@
1212
from urllib.parse import urlencode, urljoin
1313
from urllib.request import Request, urlopen
1414

15-
from .article import Article
1615
from .exceptions import (
1716
InstaparserAPIError,
1817
InstaparserAuthenticationError,
1918
InstaparserRateLimitError,
2019
InstaparserValidationError,
2120
)
22-
from .pdf import PDF
23-
from .summary import Summary
21+
from .models import PDF, Article, Summary
2422

2523

2624
def _encode_multipart_formdata(
Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
"""
2-
Article class representing a parsed article from Instaparser.
3-
"""
1+
"""Model classes representing parsed results from Instaparser."""
42

53
from dataclasses import dataclass, field
64
from typing import Any
@@ -54,3 +52,41 @@ def __repr__(self) -> str:
5452

5553
def __str__(self) -> str:
5654
return self.body or ""
55+
56+
57+
@dataclass(repr=False)
58+
class PDF(Article):
59+
"""
60+
Represents a parsed PDF from Instaparser.
61+
62+
Inherits from Article since most fields are the same.
63+
PDFs always have is_rtl=False and videos=[].
64+
"""
65+
66+
def __post_init__(self) -> None:
67+
self.is_rtl = False
68+
self.videos = []
69+
70+
def __repr__(self) -> str:
71+
return f"<PDF url={self.url!r} title={self.title!r}>"
72+
73+
74+
@dataclass(repr=False)
75+
class Summary:
76+
"""
77+
Represents a summary result from Instaparser.
78+
79+
Attributes:
80+
key_sentences: List of key sentences extracted from the article
81+
overview: Concise summary of the article
82+
"""
83+
84+
key_sentences: list[str]
85+
overview: str
86+
87+
def __repr__(self) -> str:
88+
overview = self.overview[:50] + "..." if len(self.overview) > 50 else self.overview
89+
return f"<Summary overview={overview!r} key_sentences={len(self.key_sentences)}>"
90+
91+
def __str__(self) -> str:
92+
return self.overview

instaparser/pdf.py

Lines changed: 0 additions & 24 deletions
This file was deleted.

instaparser/summary.py

Lines changed: 0 additions & 30 deletions
This file was deleted.

tests/test_client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@
66
import pytest
77

88
from instaparser import InstaparserClient
9-
from instaparser.article import Article
109
from instaparser.client import _encode_multipart_formdata
1110
from instaparser.exceptions import (
1211
InstaparserAPIError,
1312
InstaparserAuthenticationError,
1413
InstaparserRateLimitError,
1514
InstaparserValidationError,
1615
)
16+
from instaparser.models import Article
1717

1818
API_KEY = "test-api-key-12345"
1919
BASE_URL = "https://api.test.instaparser.com"
Lines changed: 59 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
"""
2-
Tests for Article class.
2+
Tests for model classes.
33
"""
44

5-
from instaparser.article import Article
5+
from instaparser.models import PDF, Article, Summary
66

77

88
class TestArticle:
@@ -60,3 +60,60 @@ def test_str_returns_empty_when_no_body(self):
6060
"""Test __str__ returns empty string when body is None."""
6161
article = Article()
6262
assert str(article) == ""
63+
64+
65+
class TestPDF:
66+
"""Tests for PDF class."""
67+
68+
def test_inherits_from_article(self):
69+
"""Test that PDF inherits from Article."""
70+
assert issubclass(PDF, Article)
71+
72+
def test_forces_is_rtl_false(self):
73+
"""Test that PDF always sets is_rtl to False, even if True is passed."""
74+
pdf = PDF(is_rtl=True)
75+
assert pdf.is_rtl is False
76+
77+
def test_forces_videos_empty(self):
78+
"""Test that PDF always sets videos to [], even if videos are passed."""
79+
pdf = PDF(videos=["https://example.com/video.mp4"])
80+
assert pdf.videos == []
81+
82+
def test_repr(self):
83+
"""Test __repr__ includes class name, url, and title."""
84+
pdf = PDF(url="https://example.com/doc.pdf", title="Test PDF")
85+
repr_str = repr(pdf)
86+
assert "PDF" in repr_str
87+
assert "https://example.com/doc.pdf" in repr_str
88+
assert "Test PDF" in repr_str
89+
90+
def test_str_returns_body(self):
91+
"""Test that __str__ is inherited from Article and returns body."""
92+
pdf = PDF(html="<p>Content</p>")
93+
assert str(pdf) == "<p>Content</p>"
94+
95+
96+
class TestSummary:
97+
"""Tests for Summary class."""
98+
99+
def test_repr_truncates_long_overview(self):
100+
"""Test __repr__ truncates overview longer than 50 characters."""
101+
summary = Summary(
102+
key_sentences=["Sentence 1", "Sentence 2"],
103+
overview="This is a test overview that is longer than 50 characters for truncation",
104+
)
105+
repr_str = repr(summary)
106+
assert "..." in repr_str
107+
assert "key_sentences=2" in repr_str
108+
109+
def test_repr_does_not_truncate_short_overview(self):
110+
"""Test __repr__ does not add ellipsis for short overview."""
111+
summary = Summary(key_sentences=["Sentence"], overview="Short")
112+
repr_str = repr(summary)
113+
assert "..." not in repr_str
114+
assert "key_sentences=1" in repr_str
115+
116+
def test_str_returns_overview(self):
117+
"""Test __str__ returns the overview."""
118+
summary = Summary(key_sentences=["Sentence"], overview="The overview")
119+
assert str(summary) == "The overview"

tests/test_pdf.py

Lines changed: 0 additions & 37 deletions
This file was deleted.

tests/test_summary.py

Lines changed: 0 additions & 31 deletions
This file was deleted.

0 commit comments

Comments
 (0)