Skip to content

Commit 5ee41ab

Browse files
committed
Support GEO location when publish media [#6] Added Location.location_info and extract_location
1 parent a4a4e67 commit 5ee41ab

File tree

5 files changed

+58
-41
lines changed

5 files changed

+58
-41
lines changed

instagrapi/extractors.py

+24-25
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,6 @@ def extract_media_v1(data):
66
"""
77
user = data["user"]
88
location = data.get("location")
9-
if location:
10-
# {'pk': 262547125,
11-
# 'name': 'NAME',
12-
# 'address': 'ADDRESS',
13-
# 'city': 'SANKT-PETERBURG',
14-
# 'short_name': 'NAME',
15-
# 'lng': 42.000000000001,
16-
# 'lat': 42.000000000002,
17-
# 'external_source': 'facebook_places',
18-
# 'facebook_places_id': 232571358171010}
19-
location = {
20-
"pk": int(location.get("pk")),
21-
"name": location.get("name"),
22-
"lat": location.get("lat"),
23-
"lng": location.get("lng"),
24-
}
259
video_url = ""
2610
if "video_versions" in data:
2711
# Select Best Quality by Resolutiuon
@@ -45,7 +29,7 @@ def extract_media_v1(data):
4529
"product_type": product_type,
4630
"code": data["code"],
4731
"thumbnail_url": thumbnail_url,
48-
"location": location or {},
32+
"location": extract_location(location) if location else {},
4933
"user": extract_user_short(user),
5034
"comment_count": int(data.get("comment_count") or 0),
5135
# the media just published has no like_count
@@ -83,13 +67,6 @@ def extract_media_gql(data):
8367
else:
8468
user["pk"] = user.pop("id")
8569
location = data.get("location")
86-
if location:
87-
location = {
88-
"pk": int(location.get("id")),
89-
"name": location.get("name"),
90-
"lat": location.get("lat"),
91-
"lng": location.get("lng"),
92-
}
9370
media_type = {"GraphImage": 1, "GraphVideo": 2,
9471
"GraphSidecar": 8}[data["__typename"]]
9572
product_type = data.get("product_type", "")
@@ -113,7 +90,7 @@ def extract_media_gql(data):
11390
data.get("display_resources", data.get('thumbnail_resources')),
11491
key=lambda o: o["config_width"] * o["config_height"],
11592
).pop()["src"],
116-
"location": location or {},
93+
"location": extract_location(location) if location else {},
11794
"user": user,
11895
"comment_count": json_value(data, "edge_media_to_comment", "count"),
11996
"like_count": json_value(data, "edge_media_preview_like", "count"),
@@ -245,3 +222,25 @@ def extract_user_v1(data):
245222
"external_url": data["external_url"],
246223
"is_business": data["is_business"],
247224
}
225+
226+
227+
def extract_location(data):
228+
"""Extract location from v1/gql
229+
v1 example: {
230+
'pk': 262547125,
231+
'name': 'NAME',
232+
'address': 'ADDRESS',
233+
'city': 'SANKT-PETERBURG',
234+
'short_name': 'NAME',
235+
'lng': 42.000000000001,
236+
'lat': 42.000000000002,
237+
'external_source': 'facebook_places',
238+
'facebook_places_id': 232571358171010
239+
}
240+
"""
241+
return {
242+
"pk": int(data.get("pk", data.get("id"))),
243+
"name": data.get("name"),
244+
"lat": data.get("lat"),
245+
"lng": data.get("lng"),
246+
}

instagrapi/location.py

+13
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import json
22

3+
from .extractors import extract_location
4+
35

46
class Location:
57

@@ -43,3 +45,14 @@ def location_build(self, location):
4345
except IndexError:
4446
pass
4547
return json.dumps(location, separators=(",", ":"))
48+
49+
def location_info_a1(self, location_pk: int) -> dict:
50+
"""Return additonal info for location by ?__a=1
51+
"""
52+
data = self.public_a1_request(f"/explore/locations/{location_pk}/")
53+
return extract_location(data['location'])
54+
55+
def location_info(self, location_pk: int) -> dict:
56+
"""Return additonal info for location
57+
"""
58+
return self.location_info_a1(location_pk)

instagrapi/media.py

+9-3
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,13 @@ def media_info_gql(self, media_pk: int) -> dict:
8484
)
8585
if not data.get("shortcode_media"):
8686
raise MediaNotFound(media_pk=media_pk, **data)
87-
return extract_media_gql(data["shortcode_media"])
87+
media = extract_media_gql(data["shortcode_media"])
88+
location = media.get('location')
89+
if location:
90+
location_pk = location.get('pk')
91+
if not location.get('lat') and location_pk:
92+
media['location'] = self.location_info(location_pk)
93+
return media
8894

8995
def media_info_v1(self, media_pk: int) -> dict:
9096
try:
@@ -128,7 +134,7 @@ def media_delete(self, media_id: str, media_type: str = '') -> bool:
128134
self._medias_cache.pop(self.media_pk(media_id), None)
129135
return result.get("did_delete")
130136

131-
def media_edit(self, media_id: str, caption: str, title: str = "", usertags: list = []) -> bool:
137+
def media_edit(self, media_id: str, caption: str, title: str = "", usertags: list = [], location: dict = {}) -> bool:
132138
"""Edit caption for media
133139
Example: https://i.instagram.com/api/v1/media/2154602296692269830_1903424587/edit_media/
134140
@@ -167,7 +173,7 @@ def media_edit(self, media_id: str, caption: str, title: str = "", usertags: lis
167173
"caption_text": caption,
168174
"container_module": "edit_media_info",
169175
"feed_position": "0",
170-
"location": "{}",
176+
"location": self.location_build(location),
171177
"usertags": json.dumps({"in": usertags}),
172178
"is_carousel_bumped_post": "false",
173179
}

setup.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
setup(
2222
name='instagrapi',
23-
version='1.2.1',
23+
version='1.2.2',
2424
author='Mikhail Andreev',
2525
author_email='[email protected]',
2626
license='MIT',

tests.py

+11-12
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,13 @@ def test_media_comment(self):
306306

307307

308308
class ClientCompareExtractTestCase(ClientPrivateTestCase):
309+
def assertLocation(self, one, two):
310+
for key, val in one.items():
311+
gql = two[key]
312+
if isinstance(val, float):
313+
val, gql = round(val, 4), round(gql, 4)
314+
self.assertEqual(val, gql)
315+
309316
def test_two_extract_media_photo(self):
310317
# Photo with usertags
311318
media_pk = 2154602296692269830
@@ -314,9 +321,7 @@ def test_two_extract_media_photo(self):
314321
self.assertTrue(media_v1.pop("thumbnail_url").startswith("https://"))
315322
self.assertTrue(media_gql.pop("thumbnail_url").startswith("https://"))
316323
self.assertTrue(media_v1.pop("comment_count") <= media_gql.pop("comment_count"))
317-
location_v1, location_gql = media_v1.pop('location'), media_gql.pop('location')
318-
for key in ('name', 'pk'):
319-
self.assertEqual(location_v1.get(key), location_gql.get(key))
324+
self.assertLocation(media_v1.pop('location'), media_gql.pop('location'))
320325
self.assertDictEqual(media_v1, media_gql)
321326

322327
def test_two_extract_media_video(self):
@@ -328,9 +333,7 @@ def test_two_extract_media_video(self):
328333
self.assertTrue(media_v1.pop("video_url").startswith("https://"))
329334
self.assertTrue(media_gql.pop("thumbnail_url").startswith("https://"))
330335
self.assertTrue(media_gql.pop("video_url").startswith("https://"))
331-
location_v1, location_gql = media_v1.pop('location'), media_gql.pop('location')
332-
for key in ('name', 'pk'):
333-
self.assertEqual(location_v1.get(key), location_gql.get(key))
336+
self.assertLocation(media_v1.pop('location'), media_gql.pop('location'))
334337
self.assertDictEqual(media_v1, media_gql)
335338

336339
def test_two_extract_media_album(self):
@@ -346,9 +349,7 @@ def test_two_extract_media_album(self):
346349
self.assertTrue(res.pop("thumbnail_url").startswith("https://"))
347350
if res['media_type'] == 2:
348351
self.assertTrue(res.pop("video_url").startswith("https://"))
349-
location_v1, location_gql = media_v1.pop('location'), media_gql.pop('location')
350-
for key in ('name', 'pk'):
351-
self.assertEqual(location_v1.get(key), location_gql.get(key))
352+
self.assertLocation(media_v1.pop('location'), media_gql.pop('location'))
352353
self.assertDictEqual(media_v1, media_gql)
353354

354355
def test_two_extract_media_igtv(self):
@@ -360,9 +361,7 @@ def test_two_extract_media_igtv(self):
360361
self.assertTrue(media_v1.pop("video_url").startswith("https://"))
361362
self.assertTrue(media_gql.pop("thumbnail_url").startswith("https://"))
362363
self.assertTrue(media_gql.pop("video_url").startswith("https://"))
363-
location_v1, location_gql = media_v1.pop('location'), media_gql.pop('location')
364-
for key in ('name', 'pk'):
365-
self.assertEqual(location_v1.get(key), location_gql.get(key))
364+
self.assertLocation(media_v1.pop('location'), media_gql.pop('location'))
366365
self.assertDictEqual(media_v1, media_gql)
367366

368367

0 commit comments

Comments
 (0)