Skip to content

Commit

Permalink
File server workarounf for android? is it necessary?
Browse files Browse the repository at this point in the history
  • Loading branch information
bohdanbobrowski committed Oct 31, 2024
1 parent 55fadd3 commit 0c6c633
Show file tree
Hide file tree
Showing 4 changed files with 605 additions and 548 deletions.
77 changes: 53 additions & 24 deletions blog2epub/blog2epub_gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import math
import time
import subprocess
import http.server
from datetime import datetime
from itertools import cycle
from threading import Thread
Expand Down Expand Up @@ -85,6 +86,17 @@ class Tab(MDBoxLayout, MDTabsBase):
pass


class MyHttpRequestHandler(http.server.SimpleHTTPRequestHandler):
def do_GET(self):
self.send_response(200)
self.send_header("Content-type", "text/html")
self.end_headers()
file_path = parse_qs(urlparse(self.path).query).get("file_path", [""])[0]
html = f'<html><head><title>blog2epub</title></head><body><a href="{file_path}">{file_path}</a></body></html>'
self.wfile.write(bytes(html, "utf8"))
return


class Blog2EpubKivyWindow(MDBoxLayout):
def __init__(self, **kwargs):
super().__init__(**kwargs)
Expand All @@ -101,6 +113,8 @@ def __init__(self, **kwargs):
)
self.blog2epub = None
self.download_thread = None
self.file_server_thread = None
self.file_server = None
self.ebook_data = None
self._generate_lock = False

Expand Down Expand Up @@ -304,6 +318,10 @@ def _update_tab_generate(self):
else:
self.generate_button.disabled = True

@staticmethod
def _open_github_page(inst):
webbrowser.open("https://github.com/bohdanbobrowski/blog2epub")

def _define_tab_about(self):
self.tab_about = Tab(
title="About",
Expand Down Expand Up @@ -337,16 +355,13 @@ def _define_tab_about(self):
)
)

def about_url_click(inst):
webbrowser.open("https://github.com/bohdanbobrowski/blog2epub")

self.tab_about.add_widget(
MDRoundFlatIconButton(
text="blog2epub on github",
font_size=sp(16),
font_name=UI_FONT_NAME,
size_hint=(1, 0.1),
on_press=about_url_click,
on_press=self._open_github_page,
icon="git",
)
)
Expand Down Expand Up @@ -518,6 +533,7 @@ def download(self, button_instance):
self.tab_select.disabled = True
self.save_settings()
try:
self.file_server = None
self.blog2epub = Blog2Epub(
url=self._get_url(),
configuration=self.blog2epub_settings.data,
Expand Down Expand Up @@ -565,6 +581,37 @@ def save_settings(self):
def popup_success(self, ebook: Book):
self.success(ebook)

def _start_file_server(self, ebook):
handler_object = MyHttpRequestHandler
self.file_server = socketserver.TCPServer(("", 8000), handler_object)
self.file_server.serve_forever()
urllib.parse.quote(ebook.file_full_path, safe="")

def _open_ebook_in_default_viewer(self, inst):
self.interface.print(f"Opening file: {ebook.file_full_path} ({platform})")
if platform == "win":
os.startfile(ebook.file_full_path)
elif platform == "android":
# TODO: Fix this
# Epic workaround
self.file_server_thread = Thread(
target=self._start_file_server,
kwargs={"file_full_path": self.blog2epub},
)
self.file_server_thread.start()

webbrowser.open(f"http://localhost:8000?file_path={file_path}")
else:
opener = "open" if sys.platform == "osx" else "xdg-open"
subprocess.call([opener, ebook.file_full_path])

def _send_ebook_via_email(self, inst):
email.send(
recipient=self.blog2epub_settings.data.email,
subject=f"blog2epub - {ebook.title}",
text="Now please attach generated file manually :-)",
)

def success(self, ebook: Book):
success_content = MDBoxLayout(orientation="vertical")
epub_cover_image_widget = MDBoxLayout(
Expand All @@ -580,24 +627,6 @@ def success(self, ebook: Book):
)
success_content.add_widget(epub_cover_image_widget)

def open_ebook_in_default_viewer(inst):
self.interface.print(f"Opening file: {ebook.file_full_path} ({platform})")
if platform == "win":
os.startfile(ebook.file_full_path)
elif platform == "android":
# TODO: Fix this
webbrowser.open(f"file://{ebook.file_full_path}")
else:
opener = "open" if sys.platform == "osx" else "xdg-open"
subprocess.call([opener, ebook.file_full_path])

def send_ebook_via_email(inst):
email.send(
recipient=self.blog2epub_settings.data.email,
subject=f"blog2epub - {ebook.title}",
text="Now please attach generated file manually :-)",
)

buttons_row = MDBoxLayout(
orientation="horizontal",
size_hint=(1, 0.1),
Expand All @@ -611,7 +640,7 @@ def send_ebook_via_email(inst):
font_size=sp(16),
font_name=UI_FONT_NAME,
size_hint=(0.5, 1),
on_press=open_ebook_in_default_viewer,
on_press=self._open_ebook_in_default_viewer,
)
)
if self.blog2epub_settings.data.email:
Expand All @@ -622,7 +651,7 @@ def send_ebook_via_email(inst):
font_size=sp(16),
font_name=UI_FONT_NAME,
size_hint=(0.5, 1),
on_press=send_ebook_via_email,
on_press=self._send_ebook_via_email,
)
)
success_content.add_widget(buttons_row)
Expand Down
10 changes: 9 additions & 1 deletion blog2epub/crawlers/abstract.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from blog2epub.common.book import Book
from blog2epub.common.interfaces import EmptyInterface
from blog2epub.common.language_tools import translate_month
from blog2epub.models.book import DirModel
from blog2epub.models.book import DirModel, BookModel
from blog2epub.models.configuration import ConfigurationModel
from blog2epub.common.crawler import (
prepare_file_name,
Expand Down Expand Up @@ -81,9 +81,17 @@ def crawl(self):
pass


@abstractmethod
def get_book_data(self) -> BookModel:
pass


class Article:
"""
Blog post, article which became book chapter...
TODO: This class is a mess, all logic from here should be moved to crawler... but that's not that easy.
"""

def __init__(self, url, title, crawler: AbstractCrawler):
Expand Down
2 changes: 1 addition & 1 deletion blog2epub/crawlers/universal.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ def _get_sitemap_xml(self):
pass

def crawl(self):
pass
self.interface.print("Crawling")
Loading

0 comments on commit 0c6c633

Please sign in to comment.