Skip to content

Commit

Permalink
print page title of url
Browse files Browse the repository at this point in the history
limited to newly received url to reduce chan loading
  • Loading branch information
Nanoseb committed Mar 26, 2016
1 parent 980a1be commit 10ef786
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 21 deletions.
5 changes: 1 addition & 4 deletions ncTelegram/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,10 +143,7 @@ def is_image(self, path):

def download_media(self, msg):
if 'url' in msg:
if not msg['url'].startswith('http'):
return 'http://' + msg['url']
else:
return msg['url']
return msg['url']
else:
mtype = msg['media']['type']
mid = msg['id']
Expand Down
52 changes: 35 additions & 17 deletions ncTelegram/ui_msgwidget.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import time
import urwid
import re
import urllib.request



Expand All @@ -15,6 +16,7 @@ def __init__(self, Telegram_ui):
self.urlregex = re.compile("""((?:https?://|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'".,<>?«»“”‘’]))""")
self.msgs = []
self.img_buffer = {}
self.url_buffer = {}
self.separator_pos = -1
self.updateLocked = False
self.Telegram_ui = Telegram_ui
Expand Down Expand Up @@ -66,23 +68,39 @@ def print_msg(self, msg):
urls = self.urlregex.findall(text[0])

if urls:
self.Telegram_ui.last_media = { 'url': urls[0][0]}
text = text

else:
if 'action' in msg:
text = [(urwid.AttrSpec('light gray', ''), '➜ ' + msg['action']['type'].replace('_',' '))]

if 'media' in msg:
self.Telegram_ui.last_media = msg
text = [(urwid.AttrSpec('light gray', ''), "➜ " + msg['media']['type'])]
if 'caption' in msg['media']:
text = text + [" " + msg['media']['caption']]

if self.Telegram_ui.INLINE_IMAGE:
image = self.get_inline_img(msg)
if image != None:
text = text + ['\n'] + self.get_inline_img(msg)
url = urls[0][0]
if not url.startswith('http'):
url = 'http://' + url

self.Telegram_ui.last_media = {'url': url}

if url in self.url_buffer:
text = text + ['\n ➜ ' + self.url_buffer[url]]
elif date > self.Telegram_ui.boot_time:
try:
resource = urllib.request.urlopen(url)
page = resource.read().decode(resource.headers.get_content_charset())
title = re.search('<title>(.*?)</title>', page, re.IGNORECASE|re.DOTALL).group(1)
self.url_buffer[url] = title
text = text + ['\n ➜ ' + title]
except:
self.url_buffer[url] = ''


elif 'action' in msg:
text = [(urwid.AttrSpec('light gray', ''), '➜ ' + msg['action']['type'].replace('_',' '))]


elif 'media' in msg:
self.Telegram_ui.last_media = msg
text = [(urwid.AttrSpec('light gray', ''), "➜ " + msg['media']['type'])]
if 'caption' in msg['media']:
text = text + [" " + msg['media']['caption']]

if self.Telegram_ui.INLINE_IMAGE:
image = self.get_inline_img(msg)
if image != None:
text = text + ['\n'] + self.get_inline_img(msg)


if 'from' in msg:
Expand Down

0 comments on commit 10ef786

Please sign in to comment.