2018-06-04 11:55:49 +02:00
|
|
|
.. _codesnippets:
|
|
|
|
|
|
|
|
Advanced Instaloader Examples
|
|
|
|
=============================
|
|
|
|
|
|
|
|
.. currentmodule:: instaloader
|
|
|
|
|
|
|
|
.. highlight:: python
|
|
|
|
|
|
|
|
.. contents::
|
|
|
|
:backlinks: none
|
|
|
|
|
|
|
|
Here we present code examples that use the :ref:`python-module-instaloader` for
|
2018-06-17 18:10:19 +02:00
|
|
|
more advanced Instagram downloading or metadata mining than what is possible
|
|
|
|
with the Instaloader command line interface.
|
2018-06-04 11:55:49 +02:00
|
|
|
|
2018-08-30 09:18:58 +02:00
|
|
|
The scripts presented here can be downloaded from our source tree:
|
|
|
|
`instaloader/docs/codesnippets/ <https://github.com/instaloader/instaloader/tree/master/docs/codesnippets>`__
|
|
|
|
|
2018-06-04 11:55:49 +02:00
|
|
|
.. For each code snippet:
|
|
|
|
- title
|
|
|
|
- brief description of what it does / motivation / how it works
|
|
|
|
- code, or link to code
|
|
|
|
- link to discussion issue
|
|
|
|
- link used methods
|
|
|
|
|
2018-06-17 18:10:19 +02:00
|
|
|
Download Posts in a Specific Period
|
2018-06-04 11:55:49 +02:00
|
|
|
-----------------------------------
|
|
|
|
|
2018-06-17 18:10:19 +02:00
|
|
|
To only download Instagram pictures (and metadata) that are within a specific
|
|
|
|
period, you can play around with :func:`~itertools.dropwhile` and
|
|
|
|
:func:`~itertools.takewhile` from :mod:`itertools` like in this snippet.
|
2018-06-04 11:55:49 +02:00
|
|
|
|
|
|
|
.. literalinclude:: codesnippets/121_since_until.py
|
|
|
|
|
|
|
|
See also :class:`Post`, :meth:`Instaloader.download_post`.
|
|
|
|
|
|
|
|
Discussed in :issue:`121`.
|
|
|
|
|
|
|
|
Likes of a Profile / Ghost Followers
|
|
|
|
------------------------------------
|
|
|
|
|
2018-06-17 18:10:19 +02:00
|
|
|
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.
|
2018-06-04 11:55:49 +02:00
|
|
|
|
|
|
|
.. 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`.
|
|
|
|
|
|
|
|
Discussed in :issue:`120`.
|
|
|
|
|
|
|
|
Track Deleted Posts
|
|
|
|
-------------------
|
|
|
|
|
2018-06-17 18:10:19 +02:00
|
|
|
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).
|
2018-06-04 11:55:49 +02:00
|
|
|
|
|
|
|
.. literalinclude:: codesnippets/56_track_deleted.py
|
|
|
|
|
|
|
|
See also :func:`load_structure_from_file`, :meth:`Profile.from_username`,
|
|
|
|
:meth:`Profile.get_posts`, :class:`Post`.
|
|
|
|
|
|
|
|
Discussed in :issue:`56`.
|
|
|
|
|
|
|
|
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
|
2018-06-17 18:10:19 +02:00
|
|
|
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.
|
2018-06-04 11:55:49 +02:00
|
|
|
|
|
|
|
.. literalinclude:: codesnippets/113_only_one_per_user.py
|
|
|
|
|
|
|
|
See also :class:`Post`, :meth:`Instaloader.download_post`,
|
|
|
|
:attr:`Post.owner_profile`, :class:`Profile`.
|
|
|
|
|
|
|
|
Discussed in :issue:`113`.
|
|
|
|
|
2019-01-17 11:00:04 +01:00
|
|
|
Top X Posts of User
|
|
|
|
-------------------
|
|
|
|
|
|
|
|
With Instaloader, it is easy to download the few most-liked pictres of a user.
|
|
|
|
|
|
|
|
.. literalinclude:: codesnippets/194_top_x_of_user.py
|
|
|
|
|
|
|
|
Discussed in :issue:`194`.
|
|
|
|
|
2018-06-17 18:10:19 +02:00
|
|
|
Upgrade Images by Local Copies
|
2018-06-04 11:55:49 +02:00
|
|
|
------------------------------
|
|
|
|
|
|
|
|
The following script finds local versions of images fetched by Instaloader, in
|
2018-06-17 18:10:19 +02:00
|
|
|
order to upgrade the downloaded images by locally-found versions with better
|
2018-06-04 11:55:49 +02:00
|
|
|
quality. It uses image hashing to identify similar images.
|
|
|
|
|
|
|
|
`updgrade-instaloader-images.py <https://gist.github.com/pavelkryukov/15f93d19a99428a284a8bcec27e0187b>`__ (external link to GitHub Gist)
|
|
|
|
|
|
|
|
Discussed in :issue:`46`.
|
|
|
|
|
2018-06-17 18:10:19 +02:00
|
|
|
Add Captions to Images
|
|
|
|
----------------------
|
2018-06-04 11:55:49 +02:00
|
|
|
|
|
|
|
Instaloader does not modify the downloaded JPEG file. However, one could combine
|
2018-06-17 18:10:19 +02:00
|
|
|
it with an imaging library such as Pillow or PIL to render the caption on
|
|
|
|
Instagram pictures. The following shows an approach.
|
2018-06-04 11:55:49 +02:00
|
|
|
|
|
|
|
.. literalinclude:: codesnippets/110_pil_captions.py
|
|
|
|
|
|
|
|
See also :attr:`Post.caption`, :attr:`Post.url`, :meth:`Post.from_shortcode`,
|
|
|
|
:func:`load_structure_from_file`.
|
|
|
|
|
|
|
|
Discussed in :issue:`110`.
|
2018-06-17 18:10:19 +02:00
|
|
|
|
|
|
|
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 <https://stedolan.github.io/jq/>`__, a command-line JSON processor, the
|
|
|
|
metadata can be easily post-processed. For example, Instaloader's JSON files can
|
|
|
|
be pretty-formatted with:
|
|
|
|
|
|
|
|
.. 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.
|