from html.parser import HTMLParser import os import re import shutil import subprocess from datetime import datetime, timezone HASHTAG_PATTERN = re.compile(r'(?>> highlight_keys("[P]rint [V]iew", "blue") >>> [('blue', 'P'), 'rint ', ('blue', 'V'), 'iew'] """ def _gen(): highlighted = False for part in re.split("\\[|\\]", text): if part: if highlighted: yield (high_attr, part) if high_attr else part else: yield (low_attr, part) if low_attr else part highlighted = not highlighted return list(_gen()) def highlight_hashtags(line, attr="hashtag"): return [ (attr, p) if p.startswith("#") else p for p in re.split(HASHTAG_PATTERN, line) ] def show_media(paths): """ Attempt to open an image viewer to show given media files. FIXME: This is not very thought out, but works for me. Once settings are implemented, add an option for the user to configure their prefered media viewer. """ viewer = None potential_viewers = [ "feh", "eog", "display" ] for v in potential_viewers: viewer = shutil.which(v) if viewer: break if not viewer: raise Exception("Cannot find an image viewer") subprocess.run([viewer] + paths) class LinkParser(HTMLParser): def reset(self): super().reset() self.links = [] def handle_starttag(self, tag, attrs): if tag == "a": href, title = None, None for name, value in attrs: if name == "href": href = value if name == "title": title = value if href: self.links.append((href, title)) def parse_content_links(content): """Parse tags from status's `content` and return them as a list of (href, title), where `title` may be None. """ parser = LinkParser() parser.feed(content) return parser.links[:]