-
-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature: Add support to read/save web articles(audio) (#20)
* Add article_web_scraper.py * Fix test * Fix file name * Remove html_text_formattings from main * Remove unused imports * Refactor article_web_scraper * Add support for pagination in read/store article * Fix logging Co-authored-by: Deepak Raj <[email protected]>
- Loading branch information
1 parent
72922a1
commit f8b85b0
Showing
5 changed files
with
109 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
from audiobook.main import AudioBook | ||
from audiobook.main import AudioBook |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import requests | ||
|
||
from bs4 import BeautifulSoup | ||
|
||
html_text_formattings = ["p", "a", "b", "strong", "i", "em", "mark", "small", "del", "ins", "sub", "sup"] | ||
|
||
class ArticleWebScraper: | ||
""" | ||
ArticleWebScraper class | ||
methods: | ||
get_json_from_web_article: returns a json from a non-empty <article> tag | ||
get_title_from_article: returns the <title> tag from the html page | ||
sample usage: | ||
ab = AudioBook(speed="normal") | ||
ab.read_book(file_path, password="abcd") | ||
""" | ||
|
||
def __init__(self, article_url): | ||
page = requests.get(article_url) | ||
self.article_url = article_url | ||
self.soup = BeautifulSoup(page.content, "html.parser") | ||
|
||
def get_title_from_article (self): | ||
""" returns the <title> tag from the html page """ | ||
return self.soup.title.text | ||
|
||
def get_json_from_web_article (self): | ||
""" returns a json from a non-empty <article> tag """ | ||
if hasattr(self.soup, 'article') and self.soup.article is not None: | ||
article_text_tag_items = [ | ||
self.soup.article.findChildren(text_formatting , recursive=True) | ||
for text_formatting in html_text_formattings | ||
] | ||
|
||
json_article = {} | ||
text_lines = [] | ||
# list(dict.fromkeys(lines))) removes duplicate words in same tag type | ||
for article_text_tag_item in article_text_tag_items: | ||
for article_text_tag in article_text_tag_item: | ||
text_line = list(dict.fromkeys([tag.string for tag in article_text_tag if tag.string is not None])) | ||
text_lines += text_line | ||
# list(dict.fromkeys(lines))) removes duplicate words among all tags | ||
text_lines = list(dict.fromkeys(text_lines)) | ||
for num in range(0, len(text_lines)): | ||
json_article[num] = text_lines[num] | ||
return json_article, len(json_article) | ||
else: | ||
raise ValueError(f"<article> tag not found in {self.article_url}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
pyttsx3==2.90 | ||
PyPDF2==2.11.1 | ||
ebooklib==0.17.1 | ||
beautifulsoup4==4.11.1 | ||
beautifulsoup4==4.11.1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters