/usr/share/pyshared/gwibber/microblog/util/__init__.py is in gwibber-service 3.4.0-0ubuntu4.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 | import os, locale, re, mx.DateTime, cgi, httplib2
import resources
import dbus
from const import *
from htmlentitydefs import name2codepoint
import logging
logger = logging.getLogger("Microblog Util")
# Try to import * from custom, install custom.py to include packaging
# customizations like distro API keys, etc
try:
from custom import *
except:
pass
COUNT = 200
def parsetime(t):
locale.setlocale(locale.LC_TIME, 'C')
result = mx.DateTime.Parser.DateTimeFromString(t)
locale.setlocale(locale.LC_TIME, '')
return result.ticks()
URL_SCHEMES = ('http', 'https', 'ftp', 'mailto', 'news', 'gopher',
'nntp', 'telnet', 'wais', 'prospero', 'aim', 'webcal')
URL_FORMAT = (r'(?<!\w)(((?:%s)://|(?:www\.))' # protocol + :// or www.
'(?!/)(?:' # get any starting /'s
'[\w$\+\*@&=\-/]' # reserved | unreserved
'|%%[a-fA-F0-9]{2}' # escape
'|[\?\.:\(\),;!\'\~](?!(?:\s|$))' # punctuation
'|(?:(?<=[^/:]{2})#)' # fragment id
'){2,}' # at least two characters in the main url part
')') % ('|'.join(URL_SCHEMES),)
PARSE_LINK = re.compile(URL_FORMAT, re.U)
PARSE_NICK = re.compile("\B@([\w]+|@[\w]$)", re.UNICODE)
PARSE_HASH = re.compile("\B#([\w\-]+|@[\w\-]$)", re.UNICODE)
PARSE_URLS = re.compile(r"<[^<]*?/?>")
def strip_urls(text):
return PARSE_URLS.sub("", text)
def unescape(s):
return re.sub('&(%s);' % '|'.join(name2codepoint),
lambda m: unichr(name2codepoint[m.group(1)]), s)
def linkify(text, subs=[], escape=True):
html_escape_table = {
"&": "&",
'"': """,
"'": "'",
">": ">",
"<": "<",
}
def html_escape(text):
return "".join(html_escape_table.get(c,c) for c in text)
if escape: text = cgi.escape(text)
#if escape: text = html_escape(text)
for f, r in subs: text = f.sub(r, text)
link = PARSE_LINK.sub('<a href="\\1">\\1</a>', text)
# if link has www only, add http:// to the href
return re.compile(r'"www.', re.U).sub('"http://www.', link)
def imgpreview(text):
images = []
# If the text is a direct link to a jpg
if text.endswith((".jpg", ".gif", ".png", ".bmp")):
images.append({"src": text, "url": None})
return images
# For pic.twitter.com images not wrapped in media entities
if "pic.twitter.com" in text:
# Annoyingly, we have to scrape the page of the tweet to get the actual image location
# The mobile site has smaller pages, so we'll use that
page = text.replace("/photo/1", "")
page = thumb.replace("http://", "http://mobile.")
resp, content = httplib2.Http().request(page)
start = content.index("http://p.twimg.com")
end = content.index(':small"><img') + 6
image = content[start:end]
images.append({"src": image, "url": None})
return images
if "instagr.am" in text:
# The location of the image is hidden in the header of the short link
thumb = text + "media?/size=m"
resp, content = httplib2.Http().request(thumb)
thumb = resp["content-location"]
images.append({"src": thumb, "url": None})
return images
thumbre = {
'twitpic': 'http://.*twitpic.com/(?!photos)([A-Za-z0-9]+)',
'img.gd': 'http://img.gd/(?!photos)([A-Za-z0-9]+)',
'imgur': 'http://.*imgur.com/(?!gallery)([A-Za-z0-9]+)',
'twitgoo': 'http://.*twitgoo.com/(?!u/)([A-Za-z0-9]+)',
'yfrog.us': 'http://.*yfrog.us/(?!froggy)([A-Za-z0-9]+)',
'yfrog.com': 'http://.*yfrog.com/(?!froggy)([A-Za-z0-9]+)',
'img.ly': 'http://img.ly/(?!images)([A-Za-z0-9]+)',
'flic.kr': 'http://flic.kr/p/([A-Za-z0-9]+)',
'tweetphoto': 'http://.*tweetphoto.com/([0-9]+)',
'plixi': 'http://plixi.com/p/([0-9]+)',
'pic.gd': 'http://pic.gd/([A-Za-z0-9]+)',
'brizzly': 'http://.*brizzly.com/pic/([A-Za-z0-9]+)',
'twitxr': 'http://.*twitxr.com\/[^ ]+\/updates\/([0-9]+)',
'ow.ly': 'http://ow.ly/i/([A-Za-z0-9]+)',
'ts1.in': 'http://ts1.in/([0-9]+)',
'twitsnaps': 'http://.*twitsnaps.com/([0-9]+)',
'hellotext': 'http://.*hellotxt.com/i/([A-Za-z0-9]+)',
'htxt.it': 'http://htxt.it/i/([A-Za-z0-9]+)',
'moby.to': 'http://moby.to/([A-Za-z0-9]+)',
'movapic': 'http://.*movapic.com/pic/([A-Za-z0-9]+)',
'znl.me': 'http://znl.me/([A-Za-z0-9-_]+)',
'bcphotoshare': 'http://.*bcphotoshare.com/photos/[0-9]+/([0-9]+)'
}
thumburi = {
'twitpic': 'http://twitpic.com/show/thumb/@',
'img.gd': 'http://img.gd/show/thumb/@',
'imgur': 'http://i.imgur.com/@s.jpg',
'twitgoo': 'http://twitgoo.com/show/thumb/@',
'yfrog.us': 'http://yfrog.us/@:iphone',
'yfrog.com': 'http://yfrog.com/@:iphone',
'img.ly': 'http://img.ly/show/thumb/@',
'flic.kr': 'http://flic.kr/p/img/@_m.jpg',
'tweetphoto': 'http://TweetPhotoAPI.com/api/TPAPI.svc/json/imagefromurl?size=thumbnail&url=@',
'plixi': 'http://api.plixi.com/api/tpapi.svc/imagefromurl?size=thumbnail&url=@',
'pic.gd': 'http://TweetPhotoAPI.com/api/TPAPI.svc/json/imagefromurl?size=thumbnail&url=@',
'brizzly': 'http://pics.brizzly.com/thumb_sm_@.jpg',
'twitxr': 'http://twitxr.com/image/@/th/',
'ow.ly': 'http://static.ow.ly/photos/thumb/@.jpg',
'ts1.in': 'http://ts1.in/mini/@',
'twitsnaps': 'http://twitsnaps.com/mini/@',
'hellotext': 'http://hellotxt.com/image/@.s.jpg',
'htxt.it': 'http://hellotxt.com/image/@.s.jpg',
'moby.to': 'http://api.mobypicture.com?s=small&format=plain&k=6JQhCKX6Z9h2m9Lo&t=@',
'movapic': 'http://image.movapic.com/pic/s_@.jpeg',
'znl.me': 'http://app.zannel.com/content/@/Image-160x120-P-JPG.jpg',
'bcphotoshare': 'http://images.bcphotoshare.com/storages/@/thumbnail.jpg'
}
for r, u in zip(thumbre, thumburi):
for match in re.finditer(thumbre[r], text):
if r == 'tweetphoto' or r == 'pic.gd' or r == 'moby.to':
images.append({"src": thumburi[u].replace('@', match.group(0)) , "url": None})
else:
images.append({"src": thumburi[u].replace('@', match.group(1)) , "url": None})
return images
def videopreview(text):
videos = []
thumbre = {
'twitvid': 'http://.*twitvid.com/(?!videos)([A-Za-z0-9]+)',
'youtu.be': 'http://youtu.be/([A-Za-z0-9-_]+)',
'youtube.com': 'http://.*youtube.com/watch\?v=([A-Za-z0-9-_]+)',
'twitvideo.jp': 'http://.*twitvideo.jp/(?!contents)([A-Za-z0-9-_]+)'
}
thumburi = {
'twitvid': 'http://images.twitvid.com/@.jpg',
'youtu.be': 'http://img.youtube.com/vi/@/0.jpg',
'youtube.com': 'http://img.youtube.com/vi/@/0.jpg',
'twitvideo.jp': 'http://twitvideo.jp/img/thumb/@'
}
thumbvid = {
'twitvid': 'http://.*twitvid.com/@',
'youtu.be': 'http://www.youtube.com/watch?v=@',
'youtube.com': 'http://www.youtube.com/watch?v=@',
'twitvideo.jp': 'http://www.twitvideo.jp/@'
}
for r, u in zip(thumbre, thumburi):
for match in re.finditer(thumbre[r], text):
videos.append({ "src": thumburi[u].replace('@', match.group(1)), "url" : text})
return videos
def compact(data):
if isinstance(data, dict):
return dict([(x, y) for x,y in data.items() if y])
elif isinstance(data, list):
return [i for i in data if i]
else: return data
first = lambda fn, lst: next((x for x in i if fn(x)))
def isRTL(s):
""" is given text a RTL content? """
if len(s)==0 :
return False
cc = ord(s[0]) # character code
if cc>=1536 and cc<=1791 : # arabic, persian, ...
return True
if cc>=65136 and cc<=65279 : # arabic peresent 2
return True
if cc>=64336 and cc<=65023 : # arabic peresent 1
return True
if cc>=1424 and cc<=1535 : # hebrew
return True
if cc>=64256 and cc<=64335 : # hebrew peresent
return True
if cc>=1792 and cc<=1871 : # Syriac
return True
if cc>=1920 and cc<=1983 : # Thaana
return True
if cc>=1984 and cc<=2047 : # NKo
return True
if cc>=11568 and cc<=11647 : # Tifinagh
return True
return False
try:
from gi.repository import GdkPixbuf, GLib, Notify
Notify.init("Gwibber")
def notify(title, text, icon = None, timeout = None, iconsize = 48):
if icon is None:
icon = resources.get_ui_asset("icons/hicolor/scalable/apps/gwibber.svg")
if icon is None:
return
caps = Notify.get_server_caps()
notification = Notify.Notification.new(title, text, "gwibber")
pixbuf = None
try:
pixbuf = GdkPixbuf.Pixbuf.new_from_file_at_size(icon, iconsize, iconsize)
except GLib.Error as e:
logger.error("Avatar failure - %s - %s", icon, e.message)
resources.del_avatar(icon)
if pixbuf:
try:
notification.set_icon_from_pixbuf(pixbuf)
except GLib.Error as e:
logger.error("Notification failure - %s", e.message)
if timeout:
notification.set_timeout(timeout)
if "x-canonical-append" in caps:
notification.set_hint_string('x-canonical-append', 'allowed')
try:
notification.show ()
except:
logger.error("Notification failed")
return
can_notify = True
except:
can_notify = False
def getbus(path, address="com.Gwibber"):
if not path.startswith("/"):
path = "/com/gwibber/%s" % path
if len(path.split('gwibber/')[1]) > 1:
address = "com.Gwibber.%s" % path.split('wibber/')[1]
bus = dbus.SessionBus()
obj = bus.get_object(address, path,
follow_name_owner_changes = True)
return dbus.Interface(obj, address)
def service_is_running(name):
return name in dbus.Interface(dbus.SessionBus().get_object(
"org.freedesktop.DBus", "/org/freedesktop/DBus"),
"org.freedesktop.DBus").ListNames()
|