mirror of
https://github.com/paul-nameless/tg
synced 2025-02-16 10:38:23 +00:00
Merge pull request #79 from paul-nameless/url-preview
Add url preview and open url handler
This commit is contained in:
commit
cd57b6b6ee
5 changed files with 49 additions and 2 deletions
|
@ -60,6 +60,7 @@ docker run -it --rm tg
|
|||
brew install tdlib
|
||||
```
|
||||
and then set in config `TDLIB_PATH`
|
||||
- `urlview` to choose urls when there is multiple in message, use `URL_VIEW` in config file to use another app (it should accept urls in stdin)
|
||||
|
||||
|
||||
## Configuration
|
||||
|
@ -127,6 +128,9 @@ MSG_FLAGS = {
|
|||
"pending": "...",
|
||||
"failed": "💩",
|
||||
}
|
||||
|
||||
# use this app to open url when there are multiple
|
||||
URL_VIEW = 'urlview'
|
||||
```
|
||||
|
||||
### Mailcap file
|
||||
|
@ -162,6 +166,7 @@ For navigation arrow keys also can be used.
|
|||
|
||||
- `j,k`: move up/down
|
||||
- `J,K`: move 10 chats up/down
|
||||
- `g`: go to top chat
|
||||
- `l`: open msgs of the chat
|
||||
- `m`: mute/unmute current chat
|
||||
- `p`: pin/unpin current chat
|
||||
|
@ -194,6 +199,7 @@ For navigation arrow keys also can be used.
|
|||
- `sa`: send audio
|
||||
- `sp`: send picture
|
||||
- `sd`: send document
|
||||
- `o`: open url present in message (if multiple urls, `urlview` will be opened)
|
||||
- `]`: next chat
|
||||
- `[`: prev chat
|
||||
- `?`: show help
|
||||
|
|
|
@ -64,6 +64,8 @@ MSG_FLAGS: Dict[str, str] = {}
|
|||
|
||||
ICON_PATH = os.path.join(os.path.dirname(__file__), "resources", "tg.png")
|
||||
|
||||
URL_VIEW = "urlview"
|
||||
|
||||
if os.path.isfile(CONFIG_FILE):
|
||||
config_params = runpy.run_path(CONFIG_FILE)
|
||||
for param, value in config_params.items():
|
||||
|
|
|
@ -70,6 +70,32 @@ class Controller:
|
|||
self.tg = tg
|
||||
self.chat_size = 0.5
|
||||
|
||||
@bind(msg_handler, ["o"])
|
||||
def open_url(self):
|
||||
msg = self.model.current_msg
|
||||
msg = MsgProxy(msg)
|
||||
if not msg.is_text:
|
||||
self.present_error("Does not contain urls")
|
||||
return
|
||||
text = msg["content"]["text"]["text"]
|
||||
urls = []
|
||||
for entity in msg["content"]["text"]["entities"]:
|
||||
if entity["type"]["@type"] != "textEntityTypeUrl":
|
||||
continue
|
||||
offset = entity["offset"]
|
||||
length = entity["length"]
|
||||
url = text[offset : offset + length]
|
||||
urls.append(url)
|
||||
if not urls:
|
||||
self.present_error("No url to open")
|
||||
return
|
||||
if len(urls) == 1:
|
||||
with suspend(self.view) as s:
|
||||
s.call(config.DEFAULT_OPEN.format(file_path=url))
|
||||
return
|
||||
with suspend(self.view) as s:
|
||||
s.run_with_input(config.URL_VIEW, "\n".join(urls))
|
||||
|
||||
def format_help(self, bindings):
|
||||
return "\n".join(
|
||||
f"{key}\t{fun.__name__}\t{fun.__doc__ or ''}"
|
||||
|
|
|
@ -44,7 +44,6 @@ def run(tg: Tdlib, stdscr: window) -> None:
|
|||
|
||||
|
||||
def main():
|
||||
utils.setup_log()
|
||||
tg = Tdlib(
|
||||
api_id=config.API_ID,
|
||||
api_hash=config.API_HASH,
|
||||
|
@ -55,6 +54,8 @@ def main():
|
|||
library_path=config.TDLIB_PATH,
|
||||
)
|
||||
tg.login()
|
||||
|
||||
utils.setup_log()
|
||||
utils.set_shorter_esc_delay()
|
||||
|
||||
wrapper(partial(run, tg))
|
||||
|
|
14
tg/views.py
14
tg/views.py
|
@ -14,7 +14,6 @@ log = logging.getLogger(__name__)
|
|||
|
||||
MAX_KEYBINDING_LENGTH = 5
|
||||
MULTICHAR_KEYBINDINGS = (
|
||||
"gg",
|
||||
"dd",
|
||||
"sd",
|
||||
"sp",
|
||||
|
@ -309,11 +308,24 @@ class MsgView:
|
|||
msg = f"{reply_line}\n{msg}"
|
||||
return msg
|
||||
|
||||
def _format_url(self, msg_proxy: MsgProxy):
|
||||
if not msg_proxy.is_text or "web_page" not in msg_proxy.msg["content"]:
|
||||
return ""
|
||||
web = msg_proxy.msg["content"]["web_page"]
|
||||
name = web["site_name"]
|
||||
title = web["title"]
|
||||
description = web["description"].replace("\n", "")
|
||||
url = f"\n | {name}: {title}"
|
||||
if description:
|
||||
url += f"\n | {description}"
|
||||
return url
|
||||
|
||||
def _format_msg(
|
||||
self, msg_proxy: MsgProxy, user_id_item: int, width_limit: int
|
||||
) -> str:
|
||||
msg = self._parse_msg(msg_proxy)
|
||||
msg = msg.replace("\n", " ")
|
||||
msg += self._format_url(msg_proxy)
|
||||
if reply_to := msg_proxy.reply_msg_id:
|
||||
msg = self._format_reply_msg(
|
||||
msg_proxy.chat_id, msg, reply_to, width_limit
|
||||
|
|
Loading…
Add table
Reference in a new issue