Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
bohdanbobrowski committed Sep 4, 2024
1 parent 92a07c5 commit f00e14c
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 151 deletions.
52 changes: 22 additions & 30 deletions blog2epub/blog2epub_gui.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import logging
import os
import platform
import re
import sys
import webbrowser
Expand All @@ -17,7 +18,6 @@
from kivymd.uix.datatables import MDDataTable # type: ignore
from kivymd.uix.tab import MDTabsBase, MDTabs # type: ignore
from kivymd.uix.textfield import MDTextField # type: ignore
from kivy.utils import platform # type: ignore

from plyer import filechooser, notification, email # type: ignore

Expand Down Expand Up @@ -65,7 +65,6 @@ def get_previous():

now = datetime.now()
date_time = now.strftime("%Y-%m-%d[%H.%M.%S]")

logging_filename = os.path.join(
str(Path.home()), ".blog2epub", f"blog2epub_{date_time}.log"
)
Expand Down Expand Up @@ -406,28 +405,6 @@ def _is_int(value) -> Optional[int]:
except ValueError:
return None

def _get_params(self):
if platform == "android":
cache_folder = self.user_data_dir
else:
cache_folder = os.path.join(str(Path.home()), ".blog2epub")
destination_folder = str(Path.home())
return {
"interface": self.interface,
"url": self._get_url(),
"include_images": True,
"images_size": (600, 800),
"images_quality": 40,
"start": None,
"end": None,
"limit": self._is_int(self.limit_entry.text),
"skip": self._is_int(self.skip_entry.text),
"force_download": False,
"file_name": None,
"cache_folder": cache_folder,
"destination_folder": destination_folder,
}

def _download_ebook(self, blog2epub: Blog2Epub):
blog2epub.download()
self._enable_download_button()
Expand Down Expand Up @@ -504,7 +481,23 @@ def download(self, instance):
self.articles_table.update_row_data(self.articles_table, [])
self.tab_select.disabled = True
self.save_settings()
self.blog2epub = Blog2Epub(self._get_params())
self.blog2epub = Blog2Epub(
{
"interface": self.interface,
"url": self._get_url(),
"include_images": True,
"images_size": (600, 800),
"images_quality": 40,
"start": None,
"end": None,
"limit": self._is_int(self.limit_entry.text),
"skip": self._is_int(self.skip_entry.text),
"force_download": False,
"file_name": None,
"cache_folder": os.path.join(str(Path.home()), ".blog2epub"),
"destination_folder": str(Path.home()),
}
)
self.download_thread = Thread(
target=self._download_ebook,
kwargs={"blog2epub": self.blog2epub},
Expand Down Expand Up @@ -578,7 +571,6 @@ def send_ebook_via_email(inst):
orientation="horizontal",
size_hint=(1, 0.1),
spacing=sp(10),
# padding=sp(16),
)

buttons_row.add_widget(
Expand Down Expand Up @@ -638,12 +630,12 @@ def __init__(self):
self.title = f"blog2epub - v. {Blog2Epub.version}"
logging.info(self.title)
logging.debug(f"Metrics.density = {Metrics.density}")
if platform == "linux":
self.icon = asset_path("blog2epub.svg")
elif platform == "win":
if platform.system() == "Darwin":
self.icon = asset_path("blog2epub.icns")
elif platform.system() == "Windows":
self.icon = asset_path("blog2epub_256px.png")
else:
self.icon = asset_path("blog2epub.icns")
self.icon = asset_path("blog2epub.svg")

def build(self):
self.theme_cls.theme_style = "Light"
Expand Down
17 changes: 4 additions & 13 deletions blog2epub/blog2epub_main.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
from typing import Dict

from blog2epub.common.exceptions import NoCrawlerDetectedError
from blog2epub.common.globals import VERSION
from blog2epub.crawlers import BlogspotCrawler, WordpressCrawler

Expand All @@ -10,16 +7,10 @@ class Blog2Epub:

version = VERSION

def __init__(self, params: Dict):
self.crawler = self.select_crawler(params)
if self.crawler is None:
raise NoCrawlerDetectedError("No crawler detected")

@staticmethod
def select_crawler(params: Dict):
if params["url"].find(".blogspot.") > -1:
return BlogspotCrawler(**params)
return WordpressCrawler(**params)
def __init__(self, **kwargs):
if kwargs.get("url").find(".blogspot.") > -1:
self.crawler = BlogspotCrawler(**kwargs)
self.crawler = WordpressCrawler(**kwargs)

def download(self):
self.crawler.crawl()
4 changes: 2 additions & 2 deletions blog2epub/common/book.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ def __init__(
self,
book_data: Any,
configuration: ConfigurationModel,
interface: EmptyInterface,
destination_folder: str,
interface: EmptyInterface = EmptyInterface(),
destination_folder: str = ".",
):
self.title = book_data.title
self.description = book_data.description
Expand Down
11 changes: 3 additions & 8 deletions blog2epub/common/interfaces.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
from abc import ABC, abstractmethod


class EmptyInterface(ABC):
class EmptyInterface:
"""Empty interface for script output."""

@abstractmethod
def print(self, text: str):
pass
print(text)

@abstractmethod
def exception(self, **kwargs):
pass
print(kwargs)
53 changes: 51 additions & 2 deletions blog2epub/crawlers/abstract.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,61 @@
import os
from abc import ABC, abstractmethod
from typing import List, Optional
from datetime import datetime
from pathlib import Path
from typing import List, Optional, Dict

from blog2epub.models.book import ArticleModel
from blog2epub.common.book import Book
from blog2epub.common.interfaces import EmptyInterface
from blog2epub.crawlers.default import Article, Downloader
from blog2epub.models.book import ArticleModel, DirModel
from blog2epub.models.configuration import ConfigurationModel
from blog2epub.common.crawler import (
prepare_file_name,
prepare_port,
prepare_url_to_crawl,
)


class AbstractCrawler(ABC):
ignore_downloads: List[str] = []

def __init__(
self,
url: str,
configuration: ConfigurationModel,
start: Optional[datetime] = None,
end: Optional[datetime] = None,
file_name: Optional[str] = None,
cache_folder: str = os.path.join(str(Path.home()), ".blog2epub"),
interface: EmptyInterface = EmptyInterface(),
):
super().__init__()
self.url = url
self.configuration = configuration
self.url_to_crawl = prepare_url_to_crawl(self.url)
self.port = prepare_port(self.url_to_crawl)
self.file_name = prepare_file_name(file_name, self.url)
self.cache_folder = cache_folder
self.start = start
self.end = end
self.interface = interface
self.dirs = DirModel(
path=str(os.path.join(self.cache_folder, self.url.replace("/", "_"))),
)
self.book: Optional[Book]
self.title = None
self.subtitle = None
self.description = None
self.language: str | None = self.configuration.language
self.atom_feed = False
self.articles: List[Article] = []
self.article_counter = 0
self.images: List[str] = []
self.downloader = Downloader(self)
self.tags: Dict = {}
self.active = False
self.cancelled = False

@abstractmethod
def crawl(self):
pass
Expand Down
Loading

0 comments on commit f00e14c

Please sign in to comment.