home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2011 October / maximum-cd-2011-10.iso / DiscContents / digsby_setup.exe / lib / plugins / twitter / twitter_util.pyo (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2011-06-22  |  5.1 KB  |  129 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.6)
  3.  
  4. import calendar
  5. import re
  6. import rfc822
  7. import string
  8. import time
  9. from util import linkify, preserve_newlines
  10. from util.net import UrlShortener
  11. from util.primitives.functional import compose
  12. at_someone = re.compile('(?<!\\w)(@\\w+)(/\\w+)?', re.UNICODE)
  13. direct_msg = re.compile('^d\\s+(\\S+)\\s+(.+)', re.DOTALL)
  14. hashtag = re.compile('(?<![\\w/])(#[\\w\\-_]+)', re.UNICODE)
  15. all_numbers = re.compile('^#\\d+$')
  16. href = '<a href="%s">%s</a>'
  17.  
  18. def _at_repl(match):
  19.     name = match.group(0)[1:]
  20.     return '@' + href % ('http://twitter.com/' + name, name)
  21.  
  22.  
  23. def at_linkify(s):
  24.     return at_someone.sub(_at_repl, s)
  25.  
  26. spellcheck_regex_ignores = [
  27.     at_someone,
  28.     hashtag]
  29.  
  30. def add_regex_ignores_to_ctrl(ctrl):
  31.     for r in spellcheck_regex_ignores:
  32.         ctrl.AddRegexIgnore(r)
  33.     
  34.  
  35.  
  36. def old_twitter_linkify(s):
  37.     pieces = filter(None, at_someone.split(preserve_newlines(s)))
  38.     s = ''.join((lambda .0: for piece in .0:
  39. namelink(linkify(piece)))(pieces))
  40.     return hashtag_linkify(s)
  41.  
  42.  
  43. def search_link(term):
  44.     return ''.join([
  45.         '<a href="http://search.twitter.com/search?q=%23',
  46.         term,
  47.         '">#',
  48.         term,
  49.         '</a>'])
  50.  
  51.  
  52. def _hashtag_repl(match):
  53.     tag = match.group(1)
  54.     if all_numbers.match(tag) and len(tag) == 2:
  55.         return match.group(0)
  56.     return search_link(tag[1:])
  57.  
  58.  
  59. def hashtag_linkify(text):
  60.     return hashtag.sub(_hashtag_repl, text)
  61.  
  62. twitter_linkify = compose([
  63.     preserve_newlines,
  64.     at_linkify,
  65.     linkify,
  66.     hashtag_linkify])
  67.  
  68. def namelink(name):
  69.     if not name.startswith('@'):
  70.         return name
  71.     if len(name) == 1:
  72.         return name
  73.     if name[1] in string.whitespace:
  74.         return name
  75.     return '@' + href % ('http://twitter.com/' + name[1:], name[1:])
  76.  
  77.  
  78. def at_linkified_text(self):
  79.     pieces = filter(None, at_someone.split(self.text))
  80.     return u''.join((lambda .0: for piece in .0:
  81. namelink(linkify(piece)))(pieces))
  82.  
  83.  
  84. def d_linkified_text(self):
  85.     return linkify(self.text)
  86.  
  87.  
  88. def format_tweet_date(tweet):
  89.     fudge = 1.5
  90.     seconds = calendar.timegm(rfc822.parsedate(tweet.created_at))
  91.     delta = int(time.time()) - int(seconds)
  92.     if delta < 60 * fudge:
  93.         return _('about a minute ago')
  94.     if delta < 3600 * (1 / fudge):
  95.         return _('about %d minutes ago') % (delta / 60 + 1)
  96.     if delta < 3600 * fudge:
  97.         return _('about an hour ago')
  98.     if delta < 86400 * (1 / fudge):
  99.         return _('about %d hours ago') % (delta / 3600 + 1)
  100.     if delta < 86400 * fudge:
  101.         return _('about a day ago')
  102.     return _('about %d days ago') % (delta / 86400 + 1)
  103.  
  104.  
  105. def twitter_mini_img_url(url):
  106.     i = url.rfind('.')
  107.     first = url[:i]
  108.     if i != -1 and first.endswith('_normal'):
  109.         first = first[:-7]
  110.         url = first + '_mini' + url[i:]
  111.     
  112.     return url
  113.  
  114.  
  115. class TweetShrink(UrlShortener):
  116.     endpoint = 'http://tweetshrink.com/shrink'
  117.     
  118.     def get_args(self, url):
  119.         return dict(text = url)
  120.  
  121.     
  122.     def process_response(self, resp):
  123.         ret = UrlShortener.process_response(self, resp)
  124.         import simplejson
  125.         return simplejson.loads(ret)['text']
  126.  
  127.  
  128. shrink_tweet = TweetShrink().shorten
  129.