Skip to content

Commit

Permalink
[instagram] Add support for user's saved medias (#644)
Browse files Browse the repository at this point in the history
* [instagram] Gracefully handle possible 'HttpErrorPage' in _extract_page()

`HttpErrorPage' is returned in shared_data at least  when not authenticated or
when trying to fetch other users saved medias
(i.e. `instagram.com/<user>/saved/').

Gracefully handle it by returning nothing.

* [instagram] Add support for user's saved medias

(Please note that this need the user to be authenticated and they can
only see their saved media (not other users ones).)

Close #643.

* [instagram] Bump copyright year
  • Loading branch information
iamleot authored Mar 16, 2020
1 parent e0b0e8d commit 160328d
Showing 1 changed file with 29 additions and 1 deletion.
30 changes: 29 additions & 1 deletion gallery_dl/extractor/instagram.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-

# Copyright 2018-2019 Leonardo Taccari
# Copyright 2018-2020 Leonardo Taccari
# Copyright 2018-2020 Mike Fährmann
#
# This program is free software; you can redistribute it and/or modify
Expand Down Expand Up @@ -268,6 +268,9 @@ def _extract_page(self, shared_data, psdf):
# Deal with different structure of pages: the first page
# has interesting data in `entry_data', next pages in `data'.
if 'entry_data' in shared_data:
if 'HttpErrorPage' in shared_data['entry_data']:
return

base_shared_data = shared_data['entry_data'][psdf['page']][0]['graphql']

# variables_id is available only in the first page
Expand Down Expand Up @@ -404,6 +407,31 @@ def instagrams(self):
return self._extract_stories(url)


class InstagramSavedExtractor(InstagramExtractor):
"""Extractor for ProfilePage saved media"""
subcategory = "saved"
pattern = (r"(?:https?://)?(?:www\.)?instagram\.com"
r"/(?!p/|explore/|directory/|accounts/|stories/|tv/)"
r"([^/?&#]+)/saved")

def __init__(self, match):
InstagramExtractor.__init__(self, match)
self.username = match.group(1)

def instagrams(self):
url = '{}/{}/saved/'.format(self.root, self.username)
shared_data = self._extract_shared_data(url)

return self._extract_page(shared_data, {
'page': 'ProfilePage',
'node': 'user',
'node_id': 'id',
'variables_id': 'id',
'edge_to_medias': 'edge_saved_media',
'query_hash': '8c86fed24fa03a8a2eea2a70a80c7b6b',
})


class InstagramUserExtractor(InstagramExtractor):
"""Extractor for ProfilePage"""
subcategory = "user"
Expand Down

0 comments on commit 160328d

Please sign in to comment.