Minor code adaptions for consistency reasons
Concerning pull request #28.
This commit is contained in:
parent
c355338010
commit
7cac6d53f2
@ -247,6 +247,12 @@ To download the last 20 pictures with hashtag #cat, do
|
|||||||
|
|
||||||
loader.download_hashtag('cat', max_count=20)
|
loader.download_hashtag('cat', max_count=20)
|
||||||
|
|
||||||
|
If logged in, Instaloader is also able to download user stories:
|
||||||
|
|
||||||
|
.. code:: python
|
||||||
|
|
||||||
|
loader.download_stories()
|
||||||
|
|
||||||
Each Instagram profile has its own unique ID which stays unmodified even
|
Each Instagram profile has its own unique ID which stays unmodified even
|
||||||
if a user changes his/her username. To get said ID, given the profile's
|
if a user changes his/her username. To get said ID, given the profile's
|
||||||
name, you may call
|
name, you may call
|
||||||
|
@ -75,6 +75,10 @@ class InvalidArgumentException(NonfatalException):
|
|||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class BadResponseException(NonfatalException):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
class BadCredentialsException(InstaloaderException):
|
class BadCredentialsException(InstaloaderException):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
@ -624,10 +628,7 @@ class Instaloader:
|
|||||||
self._log()
|
self._log()
|
||||||
return downloaded
|
return downloaded
|
||||||
|
|
||||||
|
|
||||||
def download_stories(self,
|
def download_stories(self,
|
||||||
username: str,
|
|
||||||
sessionfile: str,
|
|
||||||
download_videos: bool = True,
|
download_videos: bool = True,
|
||||||
fast_update: bool = False) -> None:
|
fast_update: bool = False) -> None:
|
||||||
"""
|
"""
|
||||||
@ -637,18 +638,21 @@ class Instaloader:
|
|||||||
:param download_videos: True, if videos should be downloaded
|
:param download_videos: True, if videos should be downloaded
|
||||||
"""
|
"""
|
||||||
|
|
||||||
self.load_session_from_file(username, sessionfile)
|
if self.username is None:
|
||||||
|
raise LoginRequiredException('Login required to download stories')
|
||||||
|
|
||||||
header = self.session.headers
|
tempsession = copy_session(self.session)
|
||||||
header['User-Agent'] = 'Instagram 10.3.2 (iPhone7,2; iPhone OS 9_3_3; en_US; en-US; scale=2.00; 750x1334) AppleWebKit/420+'
|
header = tempsession.headers
|
||||||
|
header['User-Agent'] = 'Instagram 10.3.2 (iPhone7,2; iPhone OS 9_3_3; en_US; en-US; scale=2.00; 750x1334) ' \
|
||||||
|
'AppleWebKit/420+'
|
||||||
del header['Host']
|
del header['Host']
|
||||||
del header['Origin']
|
del header['Origin']
|
||||||
del header['X-Instagram-AJAX']
|
del header['X-Instagram-AJAX']
|
||||||
del header['X-Requested-With']
|
del header['X-Requested-With']
|
||||||
|
|
||||||
self._log("Logged in as %s." % username)
|
self._sleep()
|
||||||
url = 'https://i.instagram.com/api/v1/feed/reels_tray/'
|
url = 'https://i.instagram.com/api/v1/feed/reels_tray/'
|
||||||
resp = self.session.get(url)
|
resp = tempsession.get(url)
|
||||||
|
|
||||||
if resp.status_code != 200:
|
if resp.status_code != 200:
|
||||||
raise ConnectionException('Failed to fetch stories.')
|
raise ConnectionException('Failed to fetch stories.')
|
||||||
@ -656,7 +660,7 @@ class Instaloader:
|
|||||||
data = json.loads(resp.text)
|
data = json.loads(resp.text)
|
||||||
|
|
||||||
if not 'tray' in data:
|
if not 'tray' in data:
|
||||||
raise InstaloaderException('Bad story reel JSON.')
|
raise BadResponseException('Bad story reel JSON.')
|
||||||
|
|
||||||
totalcount = sum([len(us["items"]) if "items" in us else 0 for us in data["tray"]])
|
totalcount = sum([len(us["items"]) if "items" in us else 0 for us in data["tray"]])
|
||||||
count = 1
|
count = 1
|
||||||
@ -679,8 +683,8 @@ class Instaloader:
|
|||||||
date /= 1000
|
date /= 1000
|
||||||
date_stamp = datetime.datetime.fromtimestamp(date)
|
date_stamp = datetime.datetime.fromtimestamp(date)
|
||||||
|
|
||||||
dirname = self.dirname_pattern.format(profile=name, target=name)
|
dirname = self.dirname_pattern.format(profile=name, target=':stories')
|
||||||
filename = dirname + '/' + self.filename_pattern.format(profile=name, target=name,
|
filename = dirname + '/' + self.filename_pattern.format(profile=name, target=':stories',
|
||||||
date=date_stamp,
|
date=date_stamp,
|
||||||
shortcode=shortcode)
|
shortcode=shortcode)
|
||||||
os.makedirs(dirname, exist_ok=True)
|
os.makedirs(dirname, exist_ok=True)
|
||||||
@ -702,7 +706,7 @@ class Instaloader:
|
|||||||
date_epoch=date)
|
date_epoch=date)
|
||||||
if "video_duration" in item and self.sleep:
|
if "video_duration" in item and self.sleep:
|
||||||
time.sleep(item["video_duration"])
|
time.sleep(item["video_duration"])
|
||||||
if len(item["story_locations"]) > 0:
|
if item["story_locations"]:
|
||||||
location = item["story_locations"][0]["location"]
|
location = item["story_locations"][0]["location"]
|
||||||
if location:
|
if location:
|
||||||
self.save_location(filename, location, date)
|
self.save_location(filename, location, date)
|
||||||
@ -710,7 +714,6 @@ class Instaloader:
|
|||||||
if fast_update and not downloaded:
|
if fast_update and not downloaded:
|
||||||
break
|
break
|
||||||
|
|
||||||
|
|
||||||
def download_feed_pics(self, max_count: int = None, fast_update: bool = False,
|
def download_feed_pics(self, max_count: int = None, fast_update: bool = False,
|
||||||
filter_func: Optional[Callable[[Dict[str, Dict[str, Any]]], bool]] = None,
|
filter_func: Optional[Callable[[Dict[str, Dict[str, Any]]], bool]] = None,
|
||||||
download_videos: bool = True, geotags: bool = False,
|
download_videos: bool = True, geotags: bool = False,
|
||||||
|
Loading…
Reference in New Issue
Block a user