Codebase list instaloader / master docs / codesnippets.rst
master

Tree @master (Download .tar.gz)

codesnippets.rst @masterview markup · raw · history · blame

Advanced Instaloader Examples

System Message: INFO/1 (<string>, line 6)

No directive entry for "currentmodule" in module "docutils.parsers.rst.languages.en". Trying "currentmodule" as canonical directive name.

System Message: ERROR/3 (<string>, line 6)

Unknown directive type "currentmodule".

.. currentmodule:: instaloader

System Message: INFO/1 (<string>, line 8)

No directive entry for "highlight" in module "docutils.parsers.rst.languages.en". Trying "highlight" as canonical directive name.

System Message: ERROR/3 (<string>, line 8)

Unknown directive type "highlight".

.. highlight:: python

Here we present code examples that use the :ref:`python-module-instaloader` for more advanced Instagram downloading or metadata mining than what is possible with the Instaloader command line interface.

System Message: INFO/1 (<string>, line 13)

No role entry for "ref" in module "docutils.parsers.rst.languages.en". Trying "ref" as canonical role name.

System Message: ERROR/3 (<string>, line 13); backlink

Unknown interpreted text role "ref".

The scripts presented here can be downloaded from our source tree: instaloader/docs/codesnippets/

Download Posts in a Specific Period

To only download Instagram pictures (and metadata) that are within a specific period, you can simply use :func:`~itertools.dropwhile` and :func:`~itertools.takewhile` from :mod:`itertools` on a generator that returns Posts in exact chronological order, such as :meth:`Profile.get_posts`.

System Message: INFO/1 (<string>, line 30)

No role entry for "func" in module "docutils.parsers.rst.languages.en". Trying "func" as canonical role name.

System Message: ERROR/3 (<string>, line 30); backlink

Unknown interpreted text role "func".

System Message: INFO/1 (<string>, line 30)

No role entry for "func" in module "docutils.parsers.rst.languages.en". Trying "func" as canonical role name.

System Message: ERROR/3 (<string>, line 30); backlink

Unknown interpreted text role "func".

System Message: INFO/1 (<string>, line 30)

No role entry for "mod" in module "docutils.parsers.rst.languages.en". Trying "mod" as canonical role name.

System Message: ERROR/3 (<string>, line 30); backlink

Unknown interpreted text role "mod".

System Message: INFO/1 (<string>, line 30)

No role entry for "meth" in module "docutils.parsers.rst.languages.en". Trying "meth" as canonical role name.

System Message: ERROR/3 (<string>, line 30); backlink

Unknown interpreted text role "meth".

System Message: INFO/1 (<string>, line 35)

No directive entry for "literalinclude" in module "docutils.parsers.rst.languages.en". Trying "literalinclude" as canonical directive name.

System Message: ERROR/3 (<string>, line 35)

Unknown directive type "literalinclude".

.. literalinclude:: codesnippets/121_since_until.py

See also :class:`Post`, :meth:`Instaloader.download_post`.

System Message: INFO/1 (<string>, line 37)

No role entry for "class" in module "docutils.parsers.rst.languages.en". Trying "class" as canonical role name.

System Message: ERROR/3 (<string>, line 37); backlink

Unknown interpreted text role "class".

System Message: INFO/1 (<string>, line 37)

No role entry for "meth" in module "docutils.parsers.rst.languages.en". Trying "meth" as canonical role name.

System Message: ERROR/3 (<string>, line 37); backlink

Unknown interpreted text role "meth".

Discussed in :issue:`121`.

System Message: INFO/1 (<string>, line 39)

No role entry for "issue" in module "docutils.parsers.rst.languages.en". Trying "issue" as canonical role name.

System Message: ERROR/3 (<string>, line 39); backlink

Unknown interpreted text role "issue".

The code example with :func:`~itertools.dropwhile` and :func:`~itertools.takewhile` makes the assumption that the post iterator returns posts in exact chronological order. As discussed in :issue:`666`, the following approach fits for an almost chronological order, where up to k older posts are inserted into an otherwise chronological order, such as an Hashtag feed.

System Message: INFO/1 (<string>, line 41)

No role entry for "func" in module "docutils.parsers.rst.languages.en". Trying "func" as canonical role name.

System Message: ERROR/3 (<string>, line 41); backlink

Unknown interpreted text role "func".

System Message: INFO/1 (<string>, line 41)

No role entry for "func" in module "docutils.parsers.rst.languages.en". Trying "func" as canonical role name.

System Message: ERROR/3 (<string>, line 41); backlink

Unknown interpreted text role "func".

System Message: INFO/1 (<string>, line 41)

No role entry for "issue" in module "docutils.parsers.rst.languages.en". Trying "issue" as canonical role name.

System Message: ERROR/3 (<string>, line 41); backlink

Unknown interpreted text role "issue".

System Message: INFO/1 (<string>, line 47)

No directive entry for "literalinclude" in module "docutils.parsers.rst.languages.en". Trying "literalinclude" as canonical directive name.

System Message: ERROR/3 (<string>, line 47)

Unknown directive type "literalinclude".

.. literalinclude:: codesnippets/666_historical_hashtag_data.py

Likes of a Profile / Ghost Followers

To obtain a list of your inactive followers, i.e. followers that did not like any of your pictures, into a file you can use this approach.

System Message: INFO/1 (<string>, line 55)

No directive entry for "literalinclude" in module "docutils.parsers.rst.languages.en". Trying "literalinclude" as canonical directive name.

System Message: ERROR/3 (<string>, line 55)

Unknown directive type "literalinclude".

.. literalinclude:: codesnippets/120_ghost_followers.py

See also :meth:`Profile.get_posts`, :meth:`Post.get_likes`, :meth:`Profile.get_followers`, :meth:`Instaloader.load_session_from_file`, :meth:`Profile.from_username`.

System Message: INFO/1 (<string>, line 57)

No role entry for "meth" in module "docutils.parsers.rst.languages.en". Trying "meth" as canonical role name.

System Message: ERROR/3 (<string>, line 57); backlink

Unknown interpreted text role "meth".

System Message: INFO/1 (<string>, line 57)

No role entry for "meth" in module "docutils.parsers.rst.languages.en". Trying "meth" as canonical role name.

System Message: ERROR/3 (<string>, line 57); backlink

Unknown interpreted text role "meth".

System Message: INFO/1 (<string>, line 57)

No role entry for "meth" in module "docutils.parsers.rst.languages.en". Trying "meth" as canonical role name.

System Message: ERROR/3 (<string>, line 57); backlink

Unknown interpreted text role "meth".

System Message: INFO/1 (<string>, line 57)

No role entry for "meth" in module "docutils.parsers.rst.languages.en". Trying "meth" as canonical role name.

System Message: ERROR/3 (<string>, line 57); backlink

Unknown interpreted text role "meth".

System Message: INFO/1 (<string>, line 57)

No role entry for "meth" in module "docutils.parsers.rst.languages.en". Trying "meth" as canonical role name.

System Message: ERROR/3 (<string>, line 57); backlink

Unknown interpreted text role "meth".

Discussed in :issue:`120`.

System Message: INFO/1 (<string>, line 61)

No role entry for "issue" in module "docutils.parsers.rst.languages.en". Trying "issue" as canonical role name.

System Message: ERROR/3 (<string>, line 61); backlink

Unknown interpreted text role "issue".

Track Deleted Posts

This script uses Instaloader to obtain a list of currently-online Instagram and compares it with the set of posts that you already have downloaded. It outputs a list of posts which are online but not offline (i.e. not yet downloaded) and a list of posts which are offline but not online (i.e. deleted in the profile).

System Message: INFO/1 (<string>, line 71)

No directive entry for "literalinclude" in module "docutils.parsers.rst.languages.en". Trying "literalinclude" as canonical directive name.

System Message: ERROR/3 (<string>, line 71)

Unknown directive type "literalinclude".

.. literalinclude:: codesnippets/56_track_deleted.py

See also :func:`load_structure_from_file`, :meth:`Profile.from_username`, :meth:`Profile.get_posts`, :class:`Post`.

System Message: INFO/1 (<string>, line 73)

No role entry for "func" in module "docutils.parsers.rst.languages.en". Trying "func" as canonical role name.

System Message: ERROR/3 (<string>, line 73); backlink

Unknown interpreted text role "func".

System Message: INFO/1 (<string>, line 73)

No role entry for "meth" in module "docutils.parsers.rst.languages.en". Trying "meth" as canonical role name.

System Message: ERROR/3 (<string>, line 73); backlink

Unknown interpreted text role "meth".

System Message: INFO/1 (<string>, line 73)

No role entry for "meth" in module "docutils.parsers.rst.languages.en". Trying "meth" as canonical role name.

System Message: ERROR/3 (<string>, line 73); backlink

Unknown interpreted text role "meth".

System Message: INFO/1 (<string>, line 73)

No role entry for "class" in module "docutils.parsers.rst.languages.en". Trying "class" as canonical role name.

System Message: ERROR/3 (<string>, line 73); backlink

Unknown interpreted text role "class".

Discussed in :issue:`56`.

System Message: INFO/1 (<string>, line 76)

No role entry for "issue" in module "docutils.parsers.rst.languages.en". Trying "issue" as canonical role name.

System Message: ERROR/3 (<string>, line 76); backlink

Unknown interpreted text role "issue".

Only one Post per User

To download only the one most recent post from each user, this snippet creates a :class:`set` that contains the users of which a post has already been downloaded. While iterating the posts, it checks whether the post's owner already is in the set. If not, the post is downloaded from Instagram and the user is added to that set.

System Message: INFO/1 (<string>, line 81)

No role entry for "class" in module "docutils.parsers.rst.languages.en". Trying "class" as canonical role name.

System Message: ERROR/3 (<string>, line 81); backlink

Unknown interpreted text role "class".

System Message: INFO/1 (<string>, line 87)

No directive entry for "literalinclude" in module "docutils.parsers.rst.languages.en". Trying "literalinclude" as canonical directive name.

System Message: ERROR/3 (<string>, line 87)

Unknown directive type "literalinclude".

.. literalinclude:: codesnippets/113_only_one_per_user.py

See also :class:`Post`, :meth:`Instaloader.download_post`, :attr:`Post.owner_profile`, :class:`Profile`.

System Message: INFO/1 (<string>, line 89)

No role entry for "class" in module "docutils.parsers.rst.languages.en". Trying "class" as canonical role name.

System Message: ERROR/3 (<string>, line 89); backlink

Unknown interpreted text role "class".

System Message: INFO/1 (<string>, line 89)

No role entry for "meth" in module "docutils.parsers.rst.languages.en". Trying "meth" as canonical role name.

System Message: ERROR/3 (<string>, line 89); backlink

Unknown interpreted text role "meth".

System Message: INFO/1 (<string>, line 89)

No role entry for "attr" in module "docutils.parsers.rst.languages.en". Trying "attr" as canonical role name.

System Message: ERROR/3 (<string>, line 89); backlink

Unknown interpreted text role "attr".

System Message: INFO/1 (<string>, line 89)

No role entry for "class" in module "docutils.parsers.rst.languages.en". Trying "class" as canonical role name.

System Message: ERROR/3 (<string>, line 89); backlink

Unknown interpreted text role "class".

Discussed in :issue:`113`.

System Message: INFO/1 (<string>, line 92)

No role entry for "issue" in module "docutils.parsers.rst.languages.en". Trying "issue" as canonical role name.

System Message: ERROR/3 (<string>, line 92); backlink

Unknown interpreted text role "issue".

Top X Posts of User

With Instaloader, it is easy to download the few most-liked pictres of a user.

System Message: INFO/1 (<string>, line 99)

No directive entry for "literalinclude" in module "docutils.parsers.rst.languages.en". Trying "literalinclude" as canonical directive name.

System Message: ERROR/3 (<string>, line 99)

Unknown directive type "literalinclude".

.. literalinclude:: codesnippets/194_top_x_of_user.py

Discussed in :issue:`194`.

System Message: INFO/1 (<string>, line 101)

No role entry for "issue" in module "docutils.parsers.rst.languages.en". Trying "issue" as canonical role name.

System Message: ERROR/3 (<string>, line 101); backlink

Unknown interpreted text role "issue".

Upgrade Images by Local Copies

The following script finds local versions of images fetched by Instaloader, in order to upgrade the downloaded images by locally-found versions with better quality. It uses image hashing to identify similar images.

updgrade-instaloader-images.py (external link to GitHub Gist)

Discussed in :issue:`46`.

System Message: INFO/1 (<string>, line 112)

No role entry for "issue" in module "docutils.parsers.rst.languages.en". Trying "issue" as canonical role name.

System Message: ERROR/3 (<string>, line 112); backlink

Unknown interpreted text role "issue".

Add Captions to Images

Instaloader does not modify the downloaded JPEG file. However, one could combine it with an imaging library such as Pillow or PIL to render the caption on Instagram pictures. The following shows an approach.

System Message: INFO/1 (<string>, line 121)

No directive entry for "literalinclude" in module "docutils.parsers.rst.languages.en". Trying "literalinclude" as canonical directive name.

System Message: ERROR/3 (<string>, line 121)

Unknown directive type "literalinclude".

.. literalinclude:: codesnippets/110_pil_captions.py

See also :attr:`Post.caption`, :attr:`Post.url`, :meth:`Post.from_shortcode`, :func:`load_structure_from_file`.

System Message: INFO/1 (<string>, line 123)

No role entry for "attr" in module "docutils.parsers.rst.languages.en". Trying "attr" as canonical role name.

System Message: ERROR/3 (<string>, line 123); backlink

Unknown interpreted text role "attr".

System Message: INFO/1 (<string>, line 123)

No role entry for "attr" in module "docutils.parsers.rst.languages.en". Trying "attr" as canonical role name.

System Message: ERROR/3 (<string>, line 123); backlink

Unknown interpreted text role "attr".

System Message: INFO/1 (<string>, line 123)

No role entry for "meth" in module "docutils.parsers.rst.languages.en". Trying "meth" as canonical role name.

System Message: ERROR/3 (<string>, line 123); backlink

Unknown interpreted text role "meth".

System Message: INFO/1 (<string>, line 123)

No role entry for "func" in module "docutils.parsers.rst.languages.en". Trying "func" as canonical role name.

System Message: ERROR/3 (<string>, line 123); backlink

Unknown interpreted text role "func".

Discussed in :issue:`110`.

System Message: INFO/1 (<string>, line 126)

No role entry for "issue" in module "docutils.parsers.rst.languages.en". Trying "issue" as canonical role name.

System Message: ERROR/3 (<string>, line 126); backlink

Unknown interpreted text role "issue".

Metadata JSON Files

The JSON files Instaloader saves along with each Post contain all the metadata that has been retrieved from Instagram while downloading the picture and associated required information.

With jq, a command-line JSON processor, the metadata can be easily post-processed. For example, Instaloader's JSON files can be pretty-formatted with:

System Message: WARNING/2 (<string>, line 139)

Cannot analyze code. No Pygments lexer found for "none".

.. code-block:: none

   xzcat 2018-05-13_11-18-45_UTC.json.xz | jq .node

However, Instaloader tries to do as few metadata requests as possible, so, depending on how Instaloader has been invoked, it may occur that these files do not contain the complete available metadata structure. Nevertheless, the file can be loaded into Instaloader with :func:`load_structure_from_file` and the required metadata then be accessed via the :class:`Post` or :class:`Profile` attributes, which trigger an Instagram request if that particular information is not present in the JSON file.

System Message: INFO/1 (<string>, line 143)

No role entry for "func" in module "docutils.parsers.rst.languages.en". Trying "func" as canonical role name.

System Message: ERROR/3 (<string>, line 143); backlink

Unknown interpreted text role "func".

System Message: INFO/1 (<string>, line 143)

No role entry for "class" in module "docutils.parsers.rst.languages.en". Trying "class" as canonical role name.

System Message: ERROR/3 (<string>, line 143); backlink

Unknown interpreted text role "class".

System Message: INFO/1 (<string>, line 143)

No role entry for "class" in module "docutils.parsers.rst.languages.en". Trying "class" as canonical role name.

System Message: ERROR/3 (<string>, line 143); backlink

Unknown interpreted text role "class".

Docutils System Messages

System Message: INFO/1 (<string>, line 1)

Hyperlink target "codesnippets" is not referenced.