Exemple #1
0
  def _common(self, data):
    m = {};
    try:

      m["mid"] = str(data["id"])
      m["service"] = "twitter"
      m["account"] = self.account["id"]
      if data.has_key("created_at"):
        m["time"] = util.parsetime(data["created_at"])
      m["text"] = util.unescape(data["text"])
      m["to_me"] = ("@%s" % self.account["username"]) in data["text"]

      m["html"] = util.linkify(m["text"],
        ((util.PARSE_HASH, '#<a class="hash" href="%s#search?q=\\1">\\1</a>' % URL_PREFIX),
        (util.PARSE_NICK, '@<a class="nick" href="%s/\\1">\\1</a>' % URL_PREFIX)), escape=False)

      m["content"] = util.linkify(m["text"],
        ((util.PARSE_HASH, '#<a href="gwibber:/tag?acct=%s&query=\\1">\\1</a>' % m["account"]),
        (util.PARSE_NICK, '@<a href="gwibber:/user?acct=%s&name=\\1">\\1</a>' % m["account"])), escape=True)

      m["favorited"] = data.get("favorited", False)

      images = util.imgpreview(m["text"])
      if images:
        m["images"] = images
        m["type"] = "photo"
    except: 
      log.logger.error("%s failure - %s", PROTOCOL_INFO["name"], data)
      return {}
 
    return m
Exemple #2
0
  def _common(self, data):
    m = {}
    try:
      m["mid"] = str(data["id"])
      m["service"] = "identica"
      m["account"] = self.account["id"]
      if data.has_key("created_at"):
        m["time"] = util.parsetime(data["created_at"])
      m["source"] = data.get("source", False)
      m["text"] = util.unescape(data["text"])
      m["to_me"] = ("@%s" % self.account["username"]) in data["text"]

      m["html"] = util.linkify(m["text"],
        ((util.PARSE_HASH, '#<a class="hash" href="%s#search?q=\\1">\\1</a>' % self.url_prefix),
        (util.PARSE_NICK, '@<a class="nick" href="%s/\\1">\\1</a>' % self.url_prefix)), escape=False)

      m["content"] = util.linkify(m["text"],
        ((util.PARSE_HASH, '#<a href="gwibber:/tag?acct=%s&query=\\1">\\1</a>' % m["account"]),
        (util.PARSE_NICK, '@<a href="gwibber:/user?acct=%s&name=\\1">\\1</a>' % m["account"])), escape=True)

      m["favorited"] = data.get("favorited", False)

      images = []
      if data.get("attachments", 0):
        for a in data["attachments"]:
          mime = a.get("mimetype", "")
          if mime and mime.startswith("image") and a.get("url", 0):
            images.append({"src": a["url"], "url": a["url"]})

      images.extend(util.imgpreview(m["text"]))
  
      if images:
        m["images"] = images
        m["type"] = "photo"
    except:
      logger.error("%s failure - %s", PROTOCOL_INFO["name"], data)

    return m
Exemple #3
0
  def _common(self, data):
    m = {}
    try:
      m["mid"] = str(data["id"])
      m["service"] = "twitter"
      m["account"] = self.account["id"]
      if data.has_key("created_at"):
        m["time"] = util.parsetime(data["created_at"])
      m["text"] = util.unescape(data["text"])
      m["text"] = cgi.escape(m["text"])
      m["content"] = m["text"]
      
      # Go through the entities in the tweet and use them to linkify/filter tweeks as appropriate
      if data.has_key("entities"):

        #Get mention entries
        if data["entities"].has_key("user_mentions"):
          names = []
          for mention in data["entities"]["user_mentions"]:
            if not mention["screen_name"] in names:
              try:
                m["content"] = m["content"].replace("@" + mention["screen_name"], "@<a href='gwibber:/user?acct=" + m["account"] + "&name=@" + mention["screen_name"] + "'>" + mention["screen_name"] + "</a>")
              except:
                pass
            names.append(mention["screen_name"])  

        #Get hashtag entities
        if data["entities"].has_key("hashtags"):
          hashtags = []
          for tag in data["entities"]["hashtags"]:
            if not tag["text"] in hashtags:
              try:
                m["content"] = m["content"].replace("#" + tag["text"], "#<a href='gwibber:/tag?acct=" + m["account"] + "&query=#" + tag["text"] + "'>" + tag["text"] + "</a>")
              except:
                pass
            hashtags.append(tag["text"])
        
        # Get url entities - These usually go in the link stream, but if they're picturesor videos, they should go in the proper stream
        if data["entities"].has_key("urls"):
          for urls in data["entities"]["urls"]:
              url = cgi.escape (urls["url"])
              expanded_url = url
              if urls.has_key("expanded_url"):
                if not urls["expanded_url"] is None:
                  expanded_url = cgi.escape(urls["expanded_url"])

              display_url  = url
              if urls.has_key("display_url"):
                display_url = cgi.escape (urls["display_url"])
 
              if url == m["content"]:
                m["content"] = "<a href='" + url + "' title='" + expanded_url + "'>" + display_url + "</a>"
              else:
                try:
                  startindex = m["content"].index(url)
                  endindex   = startindex + len(url)
                  start      = m["content"][0:startindex]
                  end        = m["content"][endindex:]
                  m["content"] = start + "<a href='" + url + "' title='" + expanded_url + "'>" + display_url + "</a>" + end
                except:
                  logger.debug ("Failed to set url for ID: %s",  m["mid"])

              m["type"] = "link"

              images = util.imgpreview(expanded_url)
              videos = util.videopreview(expanded_url)
              if images:
                m["images"] = images
                m["type"] = "photo"
              elif videos:
                m["images"] = videos
                m["type"] = "video"
              else:
                # Well, it's not anything else, so it must be a link
                m["link"] = {}
                m["link"]["picture"] = ""
                m["link"]["name"] = ""
                m["link"]["description"] = m["content"]
                m["link"]["url"] = url
                m["link"]["icon"] = ""
                m["link"]["caption"] = ""
                m["link"]["properties"] = {}
              
        if data["entities"].has_key("media"):
          for media in data["entities"]["media"]:
            try:
              url = cgi.escape (media["url"])
              media_url_https = media["media_url_https"]
              expanded_url = url
              if media.has_key("expanded_url"):
                expanded_url = cgi.escape(media["expanded_url"])
                
              display_url  = url
              if media.has_key("display_url"):
                display_url = cgi.escape (media["display_url"])
 
              startindex = m["content"].index(url)
              endindex   = startindex + len(url)
              start      = m["content"][0:startindex]
              end        = m["content"][endindex:]
              m["content"] = start + "<a href='" + url + "' title='" + expanded_url  + "'>" + display_url + "</a>" + end
              
              if media["type"] == "photo":
                m["type"] = "photo"
                m["photo"] = {}
                m["photo"]["picture"] = media_url_https
                m["photo"]["url"] = None
                m["photo"]["name"] = None

            except:
              pass

      else:
        m["content"] = util.linkify(util.unescape(m["text"]),
          ((util.PARSE_HASH, '#<a href="gwibber:/tag?acct=%s&query=\\1">\\1</a>' % m["account"]),
          (util.PARSE_NICK, '@<a href="gwibber:/user?acct=%s&name=\\1">\\1</a>' % m["account"])), escape=True)

      m["html"] = m["content"]
      
      m["to_me"] = ("@%s" % self.account["username"]) in data["text"]   # Check if it's a reply directed at the user
      m["favorited"] = data.get("favorited", False)                     # Check if the tweet has been favourited

    except: 
      logger.error("%s failure - %s", PROTOCOL_INFO["name"], data)
      return {}
 
    return m