Split graphql_to_extensible_attachment into smaller methods
This commit is contained in:
@@ -2,6 +2,7 @@
|
|||||||
from __future__ import unicode_literals
|
from __future__ import unicode_literals
|
||||||
|
|
||||||
import attr
|
import attr
|
||||||
|
from . import _util
|
||||||
|
|
||||||
|
|
||||||
@attr.s(cmp=False)
|
@attr.s(cmp=False)
|
||||||
@@ -46,3 +47,40 @@ class ShareAttachment(Attachment):
|
|||||||
|
|
||||||
# Put here for backwards compatibility, so that the init argument order is preserved
|
# Put here for backwards compatibility, so that the init argument order is preserved
|
||||||
uid = attr.ib(None)
|
uid = attr.ib(None)
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def _from_graphql(cls, data):
|
||||||
|
from . import _graphql
|
||||||
|
|
||||||
|
url = data.get("url")
|
||||||
|
rtn = cls(
|
||||||
|
uid=data.get("deduplication_key"),
|
||||||
|
author=data["target"]["actors"][0]["id"]
|
||||||
|
if data["target"].get("actors")
|
||||||
|
else None,
|
||||||
|
url=url,
|
||||||
|
original_url=_util.get_url_parameter(url, "u")
|
||||||
|
if "/l.php?u=" in url
|
||||||
|
else url,
|
||||||
|
title=data["title_with_entities"].get("text"),
|
||||||
|
description=data["description"].get("text")
|
||||||
|
if data.get("description")
|
||||||
|
else None,
|
||||||
|
source=data["source"].get("text"),
|
||||||
|
attachments=[
|
||||||
|
_graphql.graphql_to_subattachment(attachment)
|
||||||
|
for attachment in data.get("subattachments")
|
||||||
|
],
|
||||||
|
)
|
||||||
|
media = data.get("media")
|
||||||
|
if media and media.get("image"):
|
||||||
|
image = media["image"]
|
||||||
|
rtn.image_url = image.get("uri")
|
||||||
|
rtn.original_image_url = (
|
||||||
|
_util.get_url_parameter(rtn.image_url, "url")
|
||||||
|
if "/safe_image.php" in rtn.image_url
|
||||||
|
else rtn.image_url
|
||||||
|
)
|
||||||
|
rtn.image_width = image.get("width")
|
||||||
|
rtn.image_height = image.get("height")
|
||||||
|
return rtn
|
||||||
|
@@ -106,88 +106,22 @@ def graphql_to_attachment(a):
|
|||||||
|
|
||||||
def graphql_to_extensible_attachment(a):
|
def graphql_to_extensible_attachment(a):
|
||||||
story = a.get("story_attachment")
|
story = a.get("story_attachment")
|
||||||
if story:
|
if not story:
|
||||||
target = story.get("target")
|
return None
|
||||||
if target:
|
|
||||||
_type = target["__typename"]
|
target = story.get("target")
|
||||||
if _type == "MessageLocation":
|
if not target:
|
||||||
url = story.get("url")
|
return UnsentMessage(uid=a.get("legacy_attachment_id"))
|
||||||
address = get_url_parameter(get_url_parameter(url, "u"), "where1")
|
|
||||||
try:
|
_type = target["__typename"]
|
||||||
latitude, longitude = [float(x) for x in address.split(", ")]
|
if _type == "MessageLocation":
|
||||||
address = None
|
return LocationAttachment._from_graphql(story)
|
||||||
except ValueError:
|
elif _type == "MessageLiveLocation":
|
||||||
latitude, longitude = None, None
|
return LiveLocationAttachment._from_graphql(story)
|
||||||
rtn = LocationAttachment(
|
elif _type in ["ExternalUrl", "Story"]:
|
||||||
uid=int(story["deduplication_key"]),
|
return ShareAttachment._from_graphql(story)
|
||||||
latitude=latitude,
|
|
||||||
longitude=longitude,
|
return None
|
||||||
address=address,
|
|
||||||
)
|
|
||||||
media = story.get("media")
|
|
||||||
if media and media.get("image"):
|
|
||||||
image = media["image"]
|
|
||||||
rtn.image_url = image.get("uri")
|
|
||||||
rtn.image_width = image.get("width")
|
|
||||||
rtn.image_height = image.get("height")
|
|
||||||
rtn.url = url
|
|
||||||
return rtn
|
|
||||||
elif _type == "MessageLiveLocation":
|
|
||||||
rtn = LiveLocationAttachment(
|
|
||||||
uid=int(story["target"]["live_location_id"]),
|
|
||||||
latitude=story["target"]["coordinate"]["latitude"]
|
|
||||||
if story["target"].get("coordinate")
|
|
||||||
else None,
|
|
||||||
longitude=story["target"]["coordinate"]["longitude"]
|
|
||||||
if story["target"].get("coordinate")
|
|
||||||
else None,
|
|
||||||
name=story["title_with_entities"]["text"],
|
|
||||||
expiration_time=story["target"].get("expiration_time"),
|
|
||||||
is_expired=story["target"].get("is_expired"),
|
|
||||||
)
|
|
||||||
media = story.get("media")
|
|
||||||
if media and media.get("image"):
|
|
||||||
image = media["image"]
|
|
||||||
rtn.image_url = image.get("uri")
|
|
||||||
rtn.image_width = image.get("width")
|
|
||||||
rtn.image_height = image.get("height")
|
|
||||||
rtn.url = story.get("url")
|
|
||||||
return rtn
|
|
||||||
elif _type in ["ExternalUrl", "Story"]:
|
|
||||||
url = story.get("url")
|
|
||||||
rtn = ShareAttachment(
|
|
||||||
uid=a.get("legacy_attachment_id"),
|
|
||||||
author=story["target"]["actors"][0]["id"]
|
|
||||||
if story["target"].get("actors")
|
|
||||||
else None,
|
|
||||||
url=url,
|
|
||||||
original_url=get_url_parameter(url, "u")
|
|
||||||
if "/l.php?u=" in url
|
|
||||||
else url,
|
|
||||||
title=story["title_with_entities"].get("text"),
|
|
||||||
description=story["description"].get("text")
|
|
||||||
if story.get("description")
|
|
||||||
else None,
|
|
||||||
source=story["source"].get("text"),
|
|
||||||
attachments=[
|
|
||||||
graphql_to_subattachment(attachment)
|
|
||||||
for attachment in story.get("subattachments")
|
|
||||||
],
|
|
||||||
)
|
|
||||||
media = story.get("media")
|
|
||||||
if media and media.get("image"):
|
|
||||||
image = media["image"]
|
|
||||||
rtn.image_url = image.get("uri")
|
|
||||||
rtn.original_image_url = (
|
|
||||||
get_url_parameter(rtn.image_url, "url")
|
|
||||||
if "/safe_image.php" in rtn.image_url
|
|
||||||
else rtn.image_url
|
|
||||||
)
|
|
||||||
rtn.image_width = image.get("width")
|
|
||||||
rtn.image_height = image.get("height")
|
|
||||||
return rtn
|
|
||||||
else:
|
|
||||||
return UnsentMessage(uid=a.get("legacy_attachment_id"))
|
|
||||||
|
|
||||||
|
|
||||||
def graphql_to_subattachment(a):
|
def graphql_to_subattachment(a):
|
||||||
|
@@ -3,6 +3,7 @@ from __future__ import unicode_literals
|
|||||||
|
|
||||||
import attr
|
import attr
|
||||||
from ._attachment import Attachment
|
from ._attachment import Attachment
|
||||||
|
from . import _util
|
||||||
|
|
||||||
|
|
||||||
@attr.s(cmp=False)
|
@attr.s(cmp=False)
|
||||||
@@ -30,6 +31,30 @@ class LocationAttachment(Attachment):
|
|||||||
# Put here for backwards compatibility, so that the init argument order is preserved
|
# Put here for backwards compatibility, so that the init argument order is preserved
|
||||||
uid = attr.ib(None)
|
uid = attr.ib(None)
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def _from_graphql(cls, data):
|
||||||
|
url = data.get("url")
|
||||||
|
address = _util.get_url_parameter(_util.get_url_parameter(url, "u"), "where1")
|
||||||
|
try:
|
||||||
|
latitude, longitude = [float(x) for x in address.split(", ")]
|
||||||
|
address = None
|
||||||
|
except ValueError:
|
||||||
|
latitude, longitude = None, None
|
||||||
|
rtn = cls(
|
||||||
|
uid=int(data["deduplication_key"]),
|
||||||
|
latitude=latitude,
|
||||||
|
longitude=longitude,
|
||||||
|
address=address,
|
||||||
|
)
|
||||||
|
media = data.get("media")
|
||||||
|
if media and media.get("image"):
|
||||||
|
image = media["image"]
|
||||||
|
rtn.image_url = image.get("uri")
|
||||||
|
rtn.image_width = image.get("width")
|
||||||
|
rtn.image_height = image.get("height")
|
||||||
|
rtn.url = url
|
||||||
|
return rtn
|
||||||
|
|
||||||
|
|
||||||
@attr.s(cmp=False, init=False)
|
@attr.s(cmp=False, init=False)
|
||||||
class LiveLocationAttachment(LocationAttachment):
|
class LiveLocationAttachment(LocationAttachment):
|
||||||
@@ -61,3 +86,27 @@ class LiveLocationAttachment(LocationAttachment):
|
|||||||
expiration_time=data["expirationTime"],
|
expiration_time=data["expirationTime"],
|
||||||
is_expired=bool(data.get("stopReason")),
|
is_expired=bool(data.get("stopReason")),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def _from_graphql(cls, data):
|
||||||
|
target = data["target"]
|
||||||
|
rtn = cls(
|
||||||
|
uid=int(target["live_location_id"]),
|
||||||
|
latitude=target["coordinate"]["latitude"]
|
||||||
|
if target.get("coordinate")
|
||||||
|
else None,
|
||||||
|
longitude=target["coordinate"]["longitude"]
|
||||||
|
if target.get("coordinate")
|
||||||
|
else None,
|
||||||
|
name=data["title_with_entities"]["text"],
|
||||||
|
expiration_time=target.get("expiration_time"),
|
||||||
|
is_expired=target.get("is_expired"),
|
||||||
|
)
|
||||||
|
media = data.get("media")
|
||||||
|
if media and media.get("image"):
|
||||||
|
image = media["image"]
|
||||||
|
rtn.image_url = image.get("uri")
|
||||||
|
rtn.image_width = image.get("width")
|
||||||
|
rtn.image_height = image.get("height")
|
||||||
|
rtn.url = data.get("url")
|
||||||
|
return rtn
|
||||||
|
Reference in New Issue
Block a user