Last active
February 17, 2019 18:41
-
-
Save pedrohdz/3020a9438d06e6a0b49581bee99be521 to your computer and use it in GitHub Desktop.
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
#!/usr/bin/env python | |
# pylint: disable=missing-docstring | |
from urllib import parse | |
import webbrowser | |
import requests | |
from bs4 import BeautifulSoup | |
def get_input_url(): | |
# pylint: disable=import-error | |
import appex | |
return appex.get_url() | |
def build_ithoughts_url(mind_map_path, title, url, body): | |
base_url = 'ithoughts://x-callback-url/makeMap' | |
arguments = { | |
'format': 'md', | |
'link': url, | |
'note': body, | |
'path': mind_map_path, | |
'style': 'Chalkboard', | |
'text': title, | |
} | |
return '{}?{}'.format( | |
base_url, | |
parse.urlencode(arguments, quote_via=parse.quote)) | |
def dispatch(url): | |
try: | |
# pylint: disable=bare-except | |
webbrowser.open(url) | |
except: # NOQA | |
# pylint: disable=import-error | |
from objc_util import UIApplication, nsurl | |
app = UIApplication.sharedApplication() | |
app.openURL_(nsurl(url)) | |
class WebPageNote(): | |
def __init__(self, url, html): | |
self._url = url | |
self._soup = BeautifulSoup(html, 'html.parser') | |
@classmethod | |
def from_url(cls, url): | |
return cls(url, requests.get(url).text) | |
@property | |
def url(self): | |
return self._url | |
@property | |
def raw_title(self): | |
return self._soup.title.text.strip(' \t\n\r') | |
@property | |
def title(self): | |
return '# {}'.format(self.raw_title) | |
@property | |
def body(self): | |
description = '' | |
blocks_processed = 0 | |
for block in self._soup.find_all('p'): | |
text = block.text.strip(' \t\n\r') | |
if not text: | |
continue | |
description += ( | |
'\n\n' if description else '' | |
+ text) | |
blocks_processed += 1 | |
if blocks_processed > 4: | |
break | |
return ('## {}\n\n{}\n\n_[quick link]({})_' | |
.format(self.raw_title, description, self.url)) | |
class IThoughtsNoteView(): | |
def __init__(self, title, url, body, view=None): | |
if not view: | |
# pylint: disable=import-error | |
import ui | |
view = ui.load_view() | |
view['cancel'].action = self.cancel | |
view['ok'].action = self.ok | |
view['title'].text = title | |
view['url'].text = url | |
view['body'].text = body | |
self._view = view | |
def present(self): | |
self._view.present('sheet') | |
def cancel(self, sender): | |
# pylint: disable=unused-argument | |
self._view.close() | |
def ok(self, sender): | |
# pylint: disable=unused-argument,invalid-name | |
self._view.close() | |
ithoughs_url = build_ithoughts_url('/test/test', self.title, self.url, | |
self.body) | |
dispatch(ithoughs_url) | |
@property | |
def view(self): | |
return self._view | |
@property | |
def title(self): | |
# return 'Some title' | |
return self._view['title'].text | |
@property | |
def url(self): | |
return self._view['url'].text | |
@property | |
def body(self): | |
return self._view['body'].text | |
def main(): | |
input_url = get_input_url() | |
web_note = WebPageNote.from_url(input_url) | |
view = IThoughtsNoteView(web_note.title, web_note.url, web_note.body) | |
view.present() | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment