telegram: Improve web media handling
patch 3a9740db19a230cfa53758a4d89dbe82ba024ff5
Author: E. Bosch <presidev@AT@gmail.com>
Date: Sat Feb 19 02:10:00 CET 2022
* telegram: Improve web media handling
hunk ./telegram.py 14
-from utils import sanitize_filename, remove_slash, remove_http_s, get_human_size, get_human_duration
+from utils import sanitize_filename, is_url_equiv, extract_url, get_human_size, get_human_duration
hunk ./telegram.py 413
- if webpage.url != webpage.display_url \
- and remove_slash(webpage.url) != webpage.display_url \
- and remove_http_s(webpage.url) != webpage.display_url:
- media_url_or_data = '{} | {}'.format(webpage.url, webpage.display_url)
+ if is_url_equiv(webpage.url, webpage.display_url):
+ url_data = webpage.url
hunk ./telegram.py 416
- media_url_or_data = webpage.url
+ url_data = '{} | {}'.format(webpage.url, webpage.display_url)
hunk ./telegram.py 424
+ # extract the URL in the message, don't repeat it
+ message_url = extract_url(message.message)
+ if is_url_equiv(message_url, webpage.url):
+ if is_url_equiv(message_url, webpage.display_url):
+ media_url_or_data = message.message
+ else:
+ media_url_or_data = '{} | {}'.format(message.message, webpage.display_url)
+ else:
+ media_url_or_data = '{} | {}'.format(message.message, url_data)
hunk ./telegram.py 435
+ media_url_or_data = url_data
hunk ./utils.py 9
+SIMPLE_URL = re.compile('http(|s)://[^ ]+')
hunk ./utils.py 66
+def is_url_equiv(url1, url2):
+ if url1 and url2:
+ return url1 == url2 or remove_slash(remove_http_s(url1)) == remove_slash(remove_http_s(url2))
+ else:
+ return False
+
+def extract_url(text):
+ url = SIMPLE_URL.search(text)
+ return url.group() if url else None
+