Skip to content

Commit

Permalink
Optionally pass the Beautiful Soup object to constructors
Browse files Browse the repository at this point in the history
  • Loading branch information
biolds committed Sep 27, 2024
1 parent f2fadcb commit 71e1fe1
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 8 deletions.
15 changes: 9 additions & 6 deletions linkpreview/linkpreview.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
from os.path import dirname
from typing import Union

from bs4 import BeautifulSoup

from linkpreview.link import Link
from linkpreview.preview import (
Expand All @@ -14,13 +17,13 @@


class LinkPreview:
def __init__(self, link: Link, parser: str = PARSER):
def __init__(self, link: Link, parser: Union[str, None] = PARSER, soup: Union[BeautifulSoup, None] = None):
self.link = link
self.generic = Generic(link, parser)
self.opengraph = OpenGraph(link, parser)
self.twitter = TwitterCard(link, parser)
self.microdata = Microdata(link, parser)
self.jsonld = JsonLd(link, parser)
self.generic = Generic(link, parser, soup)
self.opengraph = OpenGraph(link, parser, soup)
self.twitter = TwitterCard(link, parser, soup)
self.microdata = Microdata(link, parser, soup)
self.jsonld = JsonLd(link, parser, soup)

@LazyAttribute
def sources(self):
Expand Down
13 changes: 11 additions & 2 deletions linkpreview/preview/base.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from typing import Union

from bs4 import BeautifulSoup
from linkpreview.link import Link

Expand All @@ -7,9 +9,16 @@ class PreviewBase(object): # pragma: nocover
Base for all web preview.
"""

def __init__(self, link: Link, parser: str):
def __init__(self, link: Link, parser: Union[str, None] = None, soup: Union[BeautifulSoup, None] = None):
if parser and soup:
raise Exception(
'Only one of `parser` or `soup` argument must be provided to PreviewBase')

self.link = link
self._soup = BeautifulSoup(self.link.content, parser)
if soup:
self._soup = soup
else:
self._soup = BeautifulSoup(self.link.content, parser)

@property
def title(self):
Expand Down
18 changes: 18 additions & 0 deletions tests/test_generic.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import pytest
from bs4 import BeautifulSoup

from linkpreview import Link
from linkpreview.preview import Generic
Expand Down Expand Up @@ -115,6 +116,23 @@ def test_generic(tin, tout):
assert getattr(preview, key) == tout[key]


def test_bs4_object():
link = Link("http://localhost")
content = get_sample("generic/title.html")
bs_content = BeautifulSoup(content, "html.parser")
preview = Generic(link, soup=bs_content)
assert preview.title == "This title is at the title tag."
assert preview.description is None
assert preview.image is None


def test_invalid_arguments():
link = Link("http://localhost")
bs_content = BeautifulSoup()
with pytest.raises(Exception):
Generic(link, parser="html.parser", soup=bs_content)


@pytest.mark.parametrize(
"tin, tout",
(
Expand Down

0 comments on commit 71e1fe1

Please sign in to comment.