Exemplo n.º 1
0
  def _message(self, data):
    if type(data) != dict:
      logger.error("Cannot parse message data: %s", str(data))
      return {}

    m = {}
    m["mid"] = str(data["id"])
    m["service"] = "facebook"
    m["account"] = self.account["id"]

    m["time"] = int(mx.DateTime.DateTimeFrom(str(data.get("updated_time", data["created_time"]))))
    m["url"] = "https://facebook.com/" + data["id"].split("_")[0] + "/posts/" + data["id"].split("_")[1]
    

    if data.get("attribution", 0):
      m["source"] = util.strip_urls(data["attribution"]).replace("via ", "")
    
    if data.has_key("message"):
      m["to_me"] = ("@%s" % self.account["username"]) in data["message"]
    if data.get("message", "").strip():
      m["text"] = data["message"]
      m["html"] = util.linkify(data["message"])
      m["content"] = m["html"]
    else:
      m["text"] = ""
      m["html"] = ""
      m["content"] = ""

    m["sender"] = self._sender(data["from"])

    m["type"] = data["type"]

    if data["type"] == "checkin":
      m["location"] = {}
      if isinstance(data["place"], dict):
        m["location"]["id"] = data["place"]["id"]
        m["location"]["name"] = data["place"].get("name", None)
        if m["location"]["name"]:
          m["html"] += " " + _("at") + " <p><b>%s</b></p>" % m["location"]["name"]
          m["content"] = m["html"]
          m["text"] += " "  + _("at") + " " + m["location"]["name"]
        if data["place"].has_key("location"):
          m["location"]["latitude"] = data["place"]["location"].get("latitude", None)
          m["location"]["longitude"] = data["place"]["location"].get("longitude", None)
          m["location"]["city"] = data["place"]["location"].get("city", None)
          m["location"]["state"] = data["place"]["location"].get("state", None)
          m["location"]["country"] = data["place"]["location"].get("country", None)
    elif data["type"] == "photo":
      m["photo"] = {}
      m["photo"]["picture"] = data.get("picture", None)
      m["photo"]["url"] = data.get("link", None)
      m["photo"]["name"] = data.get("name", None)
    elif data["type"] == "video":
      m["video"] = {}
      m["video"]["picture"] = data.get("picture", None)
      m["video"]["source"] = data.get("source", None)
      m["video"]["url"] = data.get("link", None)
      m["video"]["name"] = data.get("name", None)
      m["video"]["icon"] = data.get("icon", None)
      m["video"]["properties"] = data.get("properties", {})
    elif data["type"] == "link":
      m["link"] = {}
      m["link"]["picture"] = data.get("picture", None)
      m["link"]["name"] = data.get("name", None)
      m["link"]["description"] = data.get("description", None)
      m["link"]["url"] = data.get("link", None)
      m["link"]["icon"] = data.get("icon", None)
      m["link"]["caption"] = data.get("caption", None)
      m["link"]["properties"] = data.get("properties", {})
    elif data["type"] == "question":
      m["question"] = {}
      m["question"]["id"] = data["id"]
      m["question"]["text"] = data.get("question", None)
      if not m["question"]["text"]:
        m["question"]["text"] = data.get("story", None)
      if data.has_key("options") and isinstance(data["options"], dict):
        if isinstance(data["options"]["data"], dict):
          m["question"]["answers"] = []
          for a in data["options"]["data"]:
            answer = {}
            answer["id"] = a.get("id", None)
            answer["name"] = a.get("name", None)
            answer["votes"] = a.get("votes", None)
            m["question"]["answers"].append(answer)
      if m["question"]["text"] and len(m["text"]) < 1:
        m["text"] = m["question"]["text"]
        m["html"] = m["text"]
        m["content"] = m["html"]
    elif data["type"] == "status":
      pass
    else:
      logger.error ("facebook: unexpected type %s", data["type"])
    
    if data.has_key("privacy"):
      m["privacy"] = {}
      m["privacy"]["description"] = data["privacy"]["description"]
      m["privacy"]["value"] = data["privacy"]["value"]

    # Handle target for wall posts with a specific recipient
    if data.has_key("to"):
      m["sender"]["name"] += u" \u25b8 %s"%(data["to"]["data"][0]["name"])

    if data.has_key("likes"):
      m["likes"] = {}
      m["likes"]["liked"] = False
      if isinstance(data["likes"], dict):
        m["likes"]["count"] = data["likes"]["count"]
        if data["likes"].has_key("data"):
          m["likes"]["data"] = data["likes"]["data"]
          for d in m["likes"]["data"]:
            if self.user_id == str(d["id"]):
              m["likes"]["liked"] = True
      else:
        m["likes"]["count"] = data["likes"]

    if data.get("comments", 0):
      m["comments"] = []
      if data["comments"].has_key("data"):
        for item in data["comments"]["data"]:
          m["comments"].append({
              "text": item["message"],
              "time": int(mx.DateTime.DateTimeFrom(str(data.get("updated_time", data["created_time"])))),
              "sender": self._sender(item["from"]),
            })

    if data.get("attachment", 0):
      if data["attachment"].get("name", 0):
        m["content"] += "<p><b>%s</b></p>" % data["attachment"]["name"]

      if data["attachment"].get("description", 0):
        m["content"] += "<p>%s</p>" % data["attachment"]["description"]

    return m
Exemplo n.º 2
0
    def _message(self, data):
        if type(data) != dict:
            log.logger.error("Cannot parse message data: %s", str(data))
            return {}

        m = {}
        m["mid"] = str(data["id"])
        m["service"] = "facebook"
        m["account"] = self.account["id"]

        m["time"] = int(mx.DateTime.DateTimeFrom(str(data.get("updated_time", data["created_time"]))))
        m["url"] = "https://facebook.com/" + data["id"].split("_")[0] + "/posts/" + data["id"].split("_")[1]

        if data.get("attribution", 0):
            m["source"] = util.strip_urls(data["attribution"]).replace("via ", "")

        if data.has_key("message"):
            m["to_me"] = ("@%s" % self.account["username"]) in data["message"]
        if data.get("message", "").strip():
            m["text"] = data["message"]
            m["html"] = util.linkify(data["message"])
            m["content"] = m["html"]
        else:
            m["text"] = ""
            m["html"] = ""
            m["content"] = ""

        m["sender"] = self._sender(data["from"])

        m["type"] = data["type"]

        if data["type"] == "photo":
            m["photo"] = {}
            m["photo"]["picture"] = data.get("picture", None)
            m["photo"]["url"] = data.get("link", None)
            m["photo"]["name"] = data.get("name", None)
        if data["type"] == "video":
            m["video"] = {}
            m["video"]["picture"] = data.get("picture", None)
            m["video"]["source"] = data.get("source", None)
            m["video"]["url"] = data.get("link", None)
            m["video"]["name"] = data.get("name", None)
            m["video"]["icon"] = data.get("icon", None)
            m["video"]["properties"] = data.get("properties", {})
        if data["type"] == "link":
            m["link"] = {}
            m["link"]["picture"] = data.get("picture", None)
            m["link"]["name"] = data.get("name", None)
            m["link"]["description"] = data.get("description", None)
            m["link"]["url"] = data.get("link", None)
            m["link"]["icon"] = data.get("icon", None)
            m["link"]["caption"] = data.get("caption", None)
            m["link"]["properties"] = data.get("properties", {})

        if data.has_key("privacy"):
            m["privacy"] = {}
            m["privacy"]["description"] = data["privacy"]["description"]
            m["privacy"]["value"] = data["privacy"]["value"]

        # Handle target for wall posts with a specific recipient
        if data.has_key("to"):
            m["sender"]["name"] += u" \u25b8 %s" % (data["to"]["data"][0]["name"])

        if data.has_key("likes"):
            m["likes"] = {}
            if isinstance(data["likes"], dict):
                m["likes"]["count"] = data["likes"]["count"]
                m["likes"]["data"] = data["likes"]["data"]
            else:
                m["likes"]["count"] = data["likes"]

        if data.get("comments", 0):
            m["comments"] = []
            if data["comments"].has_key("data"):
                for item in data["comments"]["data"]:
                    m["comments"].append(
                        {
                            "text": item["message"],
                            "time": int(mx.DateTime.DateTimeFrom(str(data.get("updated_time", data["created_time"])))),
                            "sender": self._sender(item["from"]),
                        }
                    )

        if data.get("attachment", 0):
            if data["attachment"].get("name", 0):
                m["content"] += "<p><b>%s</b></p>" % data["attachment"]["name"]

            if data["attachment"].get("description", 0):
                m["content"] += "<p>%s</p>" % data["attachment"]["description"]

        return m
Exemplo n.º 3
0
  def _message(self, data):
    m = {}
    m["mid"] = str(data["id"])
    m["service"] = "facebook"
    m["account"] = self.account["id"]

    m["time"] = int(mx.DateTime.DateTimeFrom(str(data.get("updated_time", data["created_time"]))))
    m["url"] = "https://facebook.com/" + data["id"].split("_")[0] + "/posts/" + data["id"].split("_")[1]
    

    if data.get("attribution", 0):
      m["source"] = util.strip_urls(data["attribution"]).replace("via ", "")
    
    if data.has_key("message"):
      m["to_me"] = ("@%s" % self.account["username"]) in data["message"]
    if data.get("message", "").strip():
      m["text"] = data["message"]
      m["html"] = util.linkify(data["message"])
      m["content"] = m["html"]
    else:
      m["text"] = ""
      m["html"] = ""
      m["content"] = ""

    m["sender"] = self._sender(data["from"])

    m["type"] = data["type"]

    if data["type"] == "photo":
      m["photo"] = {}
      m["photo"]["picture"] = data.get("picture", None)
      m["photo"]["url"] = data.get("link", None)
      m["photo"]["name"] = data.get("name", None)
    if data["type"] == "video":
      m["video"] = {}
      m["video"]["picture"] = data.get("picture", None)
      m["video"]["source"] = data.get("source", None)
      m["video"]["url"] = data.get("link", None)
      m["video"]["name"] = data.get("name", None)
      m["video"]["icon"] = data.get("icon", None)
      m["video"]["properties"] = data.get("properties", {})
    if data["type"] == "link":
      m["link"] = {}
      m["link"]["picture"] = data.get("picture", None)
      m["link"]["name"] = data.get("name", None)
      m["link"]["description"] = data.get("description", None)
      m["link"]["url"] = data.get("link", None)
      m["link"]["icon"] = data.get("icon", None)
      m["link"]["caption"] = data.get("caption", None)
      m["link"]["properties"] = data.get("properties", {})
    
    if data.has_key("privacy"):
      m["privacy"] = {}
      m["privacy"]["description"] = data["privacy"]["description"]
      m["privacy"]["value"] = data["privacy"]["value"]

    # Handle target for wall posts with a specific recipient
    if data.has_key("to"):
      m["sender"]["name"] += u" \u25b8 %s"%(data["to"]["data"][0]["name"])

    if data.has_key("likes"):
      m["likes"] = {}
      if isinstance(data["likes"], dict):
        m["likes"]["count"] = data["likes"]["count"]
        m["likes"]["data"] = data["likes"]["data"]
      else:
        m["likes"]["count"] = data["likes"]

    if data.get("comments", 0):
      m["comments"] = []
      if data["comments"].has_key("data"):
        for item in data["comments"]["data"]:
          m["comments"].append({
              "text": item["message"],
              "time": int(mx.DateTime.DateTimeFrom(str(data.get("updated_time", data["created_time"])))),
              "sender": self._sender(item["from"]),
            })

    if data.get("attachment", 0):
      if data["attachment"].get("name", 0):
        m["content"] += "<p><b>%s</b></p>" % data["attachment"]["name"]

      if data["attachment"].get("description", 0):
        m["content"] += "<p>%s</p>" % data["attachment"]["description"]

    return m