Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[instagram] Add support for stories #371

Merged
merged 3 commits into from
Aug 5, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 82 additions & 2 deletions gallery_dl/extractor/instagram.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def items(self):
data.update(metadata)
yield Message.Directory, data

if data['typename'] == 'GraphImage':
if data['typename'] in ('GraphImage', 'GraphStoryImage', 'GraphStoryVideo'):
yield Message.Url, data['display_url'], \
text.nameext_from_url(data['display_url'], data)
elif data['typename'] == 'GraphVideo':
Expand Down Expand Up @@ -140,6 +140,67 @@ def _extract_postpage(self, url):

return medias

def _extract_stories(self, url):
page = self.request(url).text
shared_data = self._extract_shared_data(page)

# If no stories are present the URL redirect to `ProfilePage'
if 'StoriesPage' not in shared_data['entry_data']:
return []

user_id = shared_data['entry_data']['StoriesPage'][0]['user']['id']
variables = (
'{{'
'"reel_ids":["{user_id}"],'
'"tag_names":[],"location_ids":[],'
'"highlight_reel_ids":[],"precomposed_overlay":true,'
'"show_story_viewer_list":true,'
'"story_viewer_fetch_count":50,"story_viewer_cursor":"",'
'"stories_video_dash_manifest":false}}'
).format(user_id=user_id)
headers = {
"X-Requested-With": "XMLHttpRequest",
}
url = '{}/graphql/query/?query_hash={}&variables={}'.format(
self.root,
'cda12de4f7fd3719c0569ce03589f4c4',
variables,
)
shared_data = self.request(url, headers=headers).json()

# If there are stories present but the user is not authenticated or
# does not have permissions no stories are returned.
if not shared_data['data']['reels_media']:
return [] # no stories present
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe add a warning as to why there aren't any stories?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good question! What other extractors are doing in a similar situations? Actually by using the web browser what happens is that no stories could be seen and a «Log in to view this story» form is showed.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, now that you mention it: most other extractors also don't show any warnings etc. in such situations, even though they should. I guess it's OK to leave it like that for right now, and I'll go and add some more "authentication required" error messages to all other extractors.


medias = []
for media in shared_data['data']['reels_media'][0]['items']:
media_data = {
'owner_id': media['owner']['id'],
'username': media['owner']['username'],
'date': text.parse_timestamp(media['taken_at_timestamp']),
'expires': text.parse_timestamp(media['expiring_at_timestamp']),
'media_id': media['id'],
'typename': media['__typename'],
}
if media['__typename'] == 'GraphStoryImage':
media_data.update({
'display_url': media['display_url'],
'height': text.parse_int(media['dimensions']['height']),
'width': text.parse_int(media['dimensions']['width']),
})
elif media['__typename'] == 'GraphStoryVideo':
vr = media['video_resources'][0]
media_data.update({
'duration': text.parse_float(media['video_duration']),
'display_url': vr['src'],
'height': text.parse_int(vr['config_height']),
'width': text.parse_int(vr['config_width']),
})
medias.append(media_data)

return medias

def _extract_page(self, url, page_type):
shared_data_fields = {
'ProfilePage': {
Expand Down Expand Up @@ -207,6 +268,9 @@ def _extract_profilepage(self, url):
def _extract_tagpage(self, url):
yield from self._extract_page(url, 'TagPage')

def _extract_storiespage(self, url):
yield from self._extract_stories(url)


class InstagramImageExtractor(InstagramExtractor):
"""Extractor for PostPage"""
Expand Down Expand Up @@ -283,7 +347,7 @@ class InstagramUserExtractor(InstagramExtractor):
"""Extractor for ProfilePage"""
subcategory = "user"
pattern = (r"(?:https?://)?(?:www\.)?instagram\.com"
r"/(?!p/|explore/|directory/|accounts/)([^/?&#]+)")
r"/(?!p/|explore/|directory/|accounts/|stories/)([^/?&#]+)")
test = ("https://www.instagram.com/instagram/", {
"range": "1-12",
"count": ">= 12",
Expand Down Expand Up @@ -319,3 +383,19 @@ def get_metadata(self):
def instagrams(self):
url = '{}/explore/tags/{}/'.format(self.root, self.tag)
return self._extract_tagpage(url)


class InstagramStoriesExtractor(InstagramExtractor):
"""Extractor for StoriesPage"""
subcategory = "stories"
pattern = (r"(?:https?://)?(?:www\.)?instagram\.com"
r"/stories/([^/?&#]+)")
test = ("https://www.instagram.com/stories/instagram/",)

iamleot marked this conversation as resolved.
Show resolved Hide resolved
def __init__(self, match):
InstagramExtractor.__init__(self, match)
self.username = match.group(1)

def instagrams(self):
url = '{}/stories/{}/'.format(self.root, self.username)
return self._extract_storiespage(url)