mirror of
https://github.com/instaloader/instaloader.git
synced 2026-10-19 23:22:16 -07:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f71ea562af | ||
|
|
bc40b82f94 | ||
|
|
c817d1901a | ||
|
|
e232c82d5e | ||
|
|
601dd0b106 | ||
|
|
4c20b573e2 | ||
|
|
db63e5c7e5 | ||
|
|
fbe05a1add | ||
|
|
e21d34124d | ||
|
|
fa62025ea3 | ||
|
|
66b3ec6928 | ||
|
|
f7d82495bd | ||
|
|
b0a430051e | ||
|
|
8b56b90e72 | ||
|
|
0c93e63615 | ||
|
|
65025e61d9 | ||
|
|
11baf493db | ||
|
|
2345d90e0e | ||
|
|
71b09aed0f | ||
|
|
fd6d8542c9 | ||
|
|
df6fbbee76 | ||
|
|
1661bf7722 | ||
|
|
8f9723cf62 | ||
|
|
e1567a0342 | ||
|
|
610b6242a0 | ||
|
|
d90f90988a | ||
|
|
d3c747a306 | ||
|
|
225353dc21 | ||
|
|
ed499cb49c | ||
|
|
1c13d5a30b | ||
|
|
2fd1422619 | ||
|
|
c818165db4 | ||
|
|
26a78463f1 | ||
|
|
689dd3613b | ||
|
|
27b7c98be2 | ||
|
|
72d86533f2 | ||
|
|
7bb08a220a | ||
|
|
03aabc52bb | ||
|
|
9d4df0d5fb | ||
|
|
bf2f4f16ed | ||
|
|
1a3712d112 | ||
|
|
65462b01ec | ||
|
|
9857c5fa24 |
@@ -24,4 +24,4 @@ If applicable, add error messages and tracebacks to help explain your problem.
|
|||||||
**Additional context**
|
**Additional context**
|
||||||
Add any other context about the problem here.
|
Add any other context about the problem here.
|
||||||
|
|
||||||
(please also see https://instaloader.github.io/contributing.html for how to report a bug)
|
<!-- please also see https://instaloader.github.io/contributing.html for how to report a bug -->
|
||||||
|
|||||||
@@ -0,0 +1,11 @@
|
|||||||
|
blank_issues_enabled: false
|
||||||
|
contact_links:
|
||||||
|
- name: Contributing to Instaloader
|
||||||
|
url: https://instaloader.github.io/contributing.html
|
||||||
|
about: Guidelines for how to report an issue.
|
||||||
|
- name: Instaloader Troubleshooting
|
||||||
|
url: https://instaloader.github.io/troubleshooting.html
|
||||||
|
about: Many common problems and their workarounds are described here.
|
||||||
|
- name: Instaloader Documentation
|
||||||
|
url: https://instaloader.github.io/
|
||||||
|
about: Many questions might be already answered here.
|
||||||
@@ -15,10 +15,10 @@ A clear and concise description of what you want to happen.
|
|||||||
A clear and concise description of any alternative solutions or features you've considered.
|
A clear and concise description of any alternative solutions or features you've considered.
|
||||||
|
|
||||||
**If the feature request is accepted, would you be willing to submit a PR?**
|
**If the feature request is accepted, would you be willing to submit a PR?**
|
||||||
|
Yes / No
|
||||||
Yes / No _(Help can be provided if you need assistance submitting a PR)_
|
<!-- Help can be provided if you need assistance submitting a PR -->
|
||||||
|
|
||||||
**Additional context**
|
**Additional context**
|
||||||
Add any other context about the feature request here.
|
Add any other context about the feature request here.
|
||||||
|
|
||||||
(please also see https://instaloader.github.io/contributing.html for how to suggest a feature)
|
<!-- please also see https://instaloader.github.io/contributing.html for how to suggest a feature -->
|
||||||
|
|||||||
@@ -6,3 +6,7 @@ labels: question
|
|||||||
---
|
---
|
||||||
|
|
||||||
Your question here...
|
Your question here...
|
||||||
|
|
||||||
|
<!-- Please see our documentation: https://instaloader.github.io/ -->
|
||||||
|
|
||||||
|
<!-- Basic questions regarding Instaloader's usage might be more appropriate to ask on Stack Overflow -->
|
||||||
|
|||||||
@@ -0,0 +1,17 @@
|
|||||||
|
|
||||||
|
|
||||||
|
<!--
|
||||||
|
Please describe:
|
||||||
|
|
||||||
|
- A motivation for this change, e.g.
|
||||||
|
- Fixes # .
|
||||||
|
- More general: What problem does the pull request solve?
|
||||||
|
|
||||||
|
- The changes proposed in this pull request
|
||||||
|
|
||||||
|
- The completeness of this change
|
||||||
|
- Is it just a proof of concept?
|
||||||
|
- Is the documentation updated (if appropriate)?
|
||||||
|
- Do you consider it ready to be merged or is it a draft?
|
||||||
|
- Can we help you at some point?
|
||||||
|
-->
|
||||||
@@ -0,0 +1,30 @@
|
|||||||
|
name: PyLint and MyPy
|
||||||
|
|
||||||
|
on: [push, pull_request]
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
lint:
|
||||||
|
name: PyLint and MyPy
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
strategy:
|
||||||
|
matrix:
|
||||||
|
python-version: [3.8]
|
||||||
|
steps:
|
||||||
|
- name: Checkout Instaloader Repository
|
||||||
|
uses: actions/checkout@v2
|
||||||
|
with:
|
||||||
|
fetch-depth: 0 # needed for building docs
|
||||||
|
- name: Setup Python
|
||||||
|
uses: actions/setup-python@v2
|
||||||
|
with:
|
||||||
|
python-version: ${{ matrix.python-version }}
|
||||||
|
- name: Install Dependencies
|
||||||
|
run: |
|
||||||
|
python -m pip install pipenv==2020.6.2
|
||||||
|
pipenv sync --dev
|
||||||
|
- name: PyLint
|
||||||
|
run: pipenv run pylint instaloader
|
||||||
|
- name: MyPy
|
||||||
|
run: pipenv run mypy -m instaloader
|
||||||
|
- name: Build Documentation
|
||||||
|
run: pipenv run make -C docs html SPHINXOPTS="-W -n"
|
||||||
@@ -2,14 +2,25 @@ name: Mark stale issues and pull requests
|
|||||||
|
|
||||||
on:
|
on:
|
||||||
schedule:
|
schedule:
|
||||||
- cron: "15 16 * * *"
|
- cron: "10 14 * * *"
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
stale:
|
stale:
|
||||||
|
|
||||||
|
name: Mark stale issues and pull requests
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
|
- uses: actions/stale@v3
|
||||||
|
with:
|
||||||
|
repo-token: ${{ secrets.GITHUB_TOKEN }}
|
||||||
|
only-labels: 'question'
|
||||||
|
stale-issue-message: 'There has been no activity on this question for an extended period of time. This issue will be closed after further 14 days of inactivity.'
|
||||||
|
stale-issue-label: 'stale'
|
||||||
|
exempt-issue-labels: 'leave open'
|
||||||
|
days-before-stale: 15
|
||||||
|
days-before-close: -1
|
||||||
|
remove-stale-when-updated: false
|
||||||
- uses: actions/stale@v1
|
- uses: actions/stale@v1
|
||||||
with:
|
with:
|
||||||
repo-token: ${{ secrets.GITHUB_TOKEN }}
|
repo-token: ${{ secrets.GITHUB_TOKEN }}
|
||||||
@@ -21,4 +32,3 @@ jobs:
|
|||||||
exempt-pr-label: 'leave open'
|
exempt-pr-label: 'leave open'
|
||||||
days-before-stale: 90
|
days-before-stale: 90
|
||||||
days-before-close: 14
|
days-before-close: 14
|
||||||
operations-per-run: 7
|
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ on: [push, pull_request]
|
|||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
build:
|
build:
|
||||||
|
name: Windows EXE build
|
||||||
runs-on: windows-latest
|
runs-on: windows-latest
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout Instaloader repository
|
- name: Checkout Instaloader repository
|
||||||
|
|||||||
+1
-1
@@ -27,7 +27,7 @@ deploy:
|
|||||||
repo: instaloader/instaloader.github.io
|
repo: instaloader/instaloader.github.io
|
||||||
target-branch: master
|
target-branch: master
|
||||||
on:
|
on:
|
||||||
tags: true
|
branch: master
|
||||||
python: 3.6
|
python: 3.6
|
||||||
- provider: script
|
- provider: script
|
||||||
script: deploy/arch/deploy.sh $TRAVIS_TAG
|
script: deploy/arch/deploy.sh $TRAVIS_TAG
|
||||||
|
|||||||
+3
-1
@@ -49,7 +49,9 @@
|
|||||||
directory accordingly,
|
directory accordingly,
|
||||||
|
|
||||||
- allows **fine-grained customization** of filters and where to store
|
- allows **fine-grained customization** of filters and where to store
|
||||||
downloaded media.
|
downloaded media,
|
||||||
|
|
||||||
|
- automatically **resumes previously-interrupted** download iterations.
|
||||||
|
|
||||||
::
|
::
|
||||||
|
|
||||||
|
|||||||
@@ -8,33 +8,18 @@ import sys
|
|||||||
# copy the required files into repo root
|
# copy the required files into repo root
|
||||||
shutil.copy('docs/favicon.ico', '.')
|
shutil.copy('docs/favicon.ico', '.')
|
||||||
shutil.copy('deploy/windows/instaloader.spec', '.')
|
shutil.copy('deploy/windows/instaloader.spec', '.')
|
||||||
shutil.unpack_archive('deploy/windows/ps', '.', 'xztar')
|
|
||||||
|
|
||||||
code = """
|
code = """
|
||||||
|
import contextlib
|
||||||
import psutil
|
import psutil
|
||||||
import subprocess
|
import subprocess
|
||||||
|
|
||||||
def __main():
|
def __main():
|
||||||
grandparentpid = psutil.Process(os.getppid()).ppid()
|
with contextlib.suppress(AttributeError, psutil.Error):
|
||||||
grandparentpidsearchstring = ' ' + str(grandparentpid) + ' '
|
if psutil.Process().parent().parent().name() == "explorer.exe":
|
||||||
if hasattr(sys, "_MEIPASS"):
|
subprocess.Popen("powershell -NoExit -Command \\\"& {0}\\\"".format(sys.argv[0]))
|
||||||
ps = os.path.join(sys._MEIPASS, 'tasklist.exe')
|
return
|
||||||
else:
|
main()
|
||||||
ps = 'tasklist'
|
|
||||||
popen = subprocess.Popen(ps, stdout=subprocess.PIPE, universal_newlines=True)
|
|
||||||
for examine in iter(popen.stdout.readline, ""):
|
|
||||||
if grandparentpidsearchstring in examine:
|
|
||||||
pname = examine
|
|
||||||
break
|
|
||||||
popen.stdout.close()
|
|
||||||
return_code = popen.wait()
|
|
||||||
if return_code:
|
|
||||||
raise subprocess.CalledProcessError(return_code, ps)
|
|
||||||
if pname[0:12] == 'explorer.exe':
|
|
||||||
subprocess.Popen("cmd /K \\\"{0}\\\"".format(os.path.splitext(os.path.basename(sys.argv[0]))[0]))
|
|
||||||
else:
|
|
||||||
main()
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
__main()
|
__main()
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ block_cipher = None
|
|||||||
|
|
||||||
a = Analysis(['__main__.py'],
|
a = Analysis(['__main__.py'],
|
||||||
binaries=[],
|
binaries=[],
|
||||||
datas=[('tasklist.exe', '.'), ('framedyn.dll', '.')],
|
datas=[],
|
||||||
hiddenimports=[],
|
hiddenimports=[],
|
||||||
hookspath=[],
|
hookspath=[],
|
||||||
runtime_hooks=[],
|
runtime_hooks=[],
|
||||||
|
|||||||
Binary file not shown.
Vendored
+8
-5
@@ -18,14 +18,17 @@ code {
|
|||||||
padding: 0.7em;
|
padding: 0.7em;
|
||||||
color: #fff; }
|
color: #fff; }
|
||||||
.highlight .c1 {
|
.highlight .c1 {
|
||||||
color: #008d06; }
|
color: #666; }
|
||||||
.highlight .k, .highlight .kn, .highlight .ow {
|
.highlight .k, .highlight .kn, .highlight .ow {
|
||||||
color: #f48400;
|
color: #008d06; }
|
||||||
font-weight: bold; }
|
.highlight .nb, .highlight .ne, .highlight .nf, .highlight .vm {
|
||||||
.highlight .nb {
|
|
||||||
color: #f48400; }
|
color: #f48400; }
|
||||||
.highlight .s1, .highlight .s2, .highlight .mi {
|
.highlight .s1, .highlight .s2, .highlight .sa, .highlight .se, .highlight .si, .highlight .mi {
|
||||||
color: #ff7efd; }
|
color: #ff7efd; }
|
||||||
|
.highlight .c1, .highlight .nf, .highlight .sa, .highlight .se, .highlight .si {
|
||||||
|
font-style: italic; }
|
||||||
|
.highlight .k, .highlight .kn, .highlight .ow {
|
||||||
|
font-weight: bold; }
|
||||||
|
|
||||||
.doc-content {
|
.doc-content {
|
||||||
padding-top: 0.5em;
|
padding-top: 0.5em;
|
||||||
|
|||||||
Vendored
+12
-5
@@ -37,21 +37,28 @@ code {
|
|||||||
}
|
}
|
||||||
|
|
||||||
.c1 {
|
.c1 {
|
||||||
color: $color_instaloader_main
|
color: #666;
|
||||||
}
|
}
|
||||||
|
|
||||||
.k, .kn, .ow {
|
.k, .kn, .ow {
|
||||||
color: $color_instaloader_accent;
|
color: $color_instaloader_main
|
||||||
font-weight: bold
|
|
||||||
}
|
}
|
||||||
|
|
||||||
.nb {
|
.nb, .ne, .nf, .vm {
|
||||||
color: $color_instaloader_accent
|
color: $color_instaloader_accent
|
||||||
}
|
}
|
||||||
|
|
||||||
.s1, .s2, .mi {
|
.s1, .s2, .sa, .se, .si, .mi {
|
||||||
color: $color_instaloader_hyper
|
color: $color_instaloader_hyper
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.c1, .nf, .sa, .se, .si {
|
||||||
|
font-style: italic;
|
||||||
|
}
|
||||||
|
|
||||||
|
.k, .kn, .ow {
|
||||||
|
font-weight: bold
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
.doc-content {
|
.doc-content {
|
||||||
|
|||||||
@@ -226,8 +226,32 @@ Exceptions
|
|||||||
|
|
||||||
.. autoexception:: TooManyRequestsException
|
.. autoexception:: TooManyRequestsException
|
||||||
|
|
||||||
|
Resumable Iterations
|
||||||
|
^^^^^^^^^^^^^^^^^^^^
|
||||||
|
|
||||||
|
.. versionadded:: 4.5
|
||||||
|
|
||||||
|
.. autoclass:: NodeIterator
|
||||||
|
:no-show-inheritance:
|
||||||
|
|
||||||
|
.. autoclass:: FrozenNodeIterator
|
||||||
|
:no-show-inheritance:
|
||||||
|
|
||||||
|
.. autofunction:: resumable_iteration
|
||||||
|
|
||||||
``InstaloaderContext`` (Low-level functions)
|
``InstaloaderContext`` (Low-level functions)
|
||||||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||||
|
|
||||||
|
``InstaloaderContext``
|
||||||
|
""""""""""""""""""""""
|
||||||
|
|
||||||
.. autoclass:: InstaloaderContext
|
.. autoclass:: InstaloaderContext
|
||||||
:no-show-inheritance:
|
:no-show-inheritance:
|
||||||
|
|
||||||
|
``RateController``
|
||||||
|
""""""""""""""""""
|
||||||
|
|
||||||
|
.. autoclass:: RateController
|
||||||
|
:no-show-inheritance:
|
||||||
|
|
||||||
|
.. versionadded:: 4.5
|
||||||
|
|||||||
+14
-9
@@ -51,7 +51,7 @@ invoke it with
|
|||||||
instaloader --login=your_username profile [profile ...]
|
instaloader --login=your_username profile [profile ...]
|
||||||
|
|
||||||
When logging in, Instaloader **stores the session cookies** in a file in your
|
When logging in, Instaloader **stores the session cookies** in a file in your
|
||||||
temporary directory, which will be reused later the next time :option:`--login`
|
home directory, which will be reused later the next time :option:`--login`
|
||||||
is given. So you can download private profiles **non-interactively** when you
|
is given. So you can download private profiles **non-interactively** when you
|
||||||
already have a valid session cookie file.
|
already have a valid session cookie file.
|
||||||
|
|
||||||
@@ -111,8 +111,12 @@ Instaloader supports the following targets:
|
|||||||
``profile`` (requires :option:`--login`).
|
``profile`` (requires :option:`--login`).
|
||||||
|
|
||||||
- ``-post``
|
- ``-post``
|
||||||
The single **post** with the given shortcode. Must be preceeded by ``--`` in
|
Replace **post** with the post's shortcode to download single post. Must be preceeded by ``--`` in
|
||||||
the argument list to not be mistaken as an option flag.
|
the argument list to not be mistaken as an option flag::
|
||||||
|
|
||||||
|
instaloader -- -B_K4CykAOtf
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
.. versionadded:: 4.1
|
.. versionadded:: 4.1
|
||||||
|
|
||||||
@@ -291,17 +295,18 @@ Instaloader as Cronjob
|
|||||||
Instaloader is suitable for running as a cronjob to periodically update your
|
Instaloader is suitable for running as a cronjob to periodically update your
|
||||||
personal Instagram archive. The :option:`--quiet` option disables user
|
personal Instagram archive. The :option:`--quiet` option disables user
|
||||||
interactions and logging of non-error messages. To non-interactively use
|
interactions and logging of non-error messages. To non-interactively use
|
||||||
Instaloader logged-in, create a session file, e.g. in your home directory::
|
Instaloader logged-in, create a session file::
|
||||||
|
|
||||||
instaloader --login=your_username --sessionfile=~/.instaloader-session
|
instaloader --login=your_username
|
||||||
|
|
||||||
Then use the same parameters in your cronjob to load the session and download
|
Then use the same parameter in your cronjob to load the session and download
|
||||||
the given targets::
|
the given targets::
|
||||||
|
|
||||||
instaloader --login=your_username --sessionfile=~/.instaloader-session --quiet <target> [...]
|
instaloader --login=your_username --quiet <target> [...]
|
||||||
|
|
||||||
Without :option:`--sessionfile` option, Instaloader saves the session file in
|
Instaloader saves the session file to
|
||||||
a path within your temporary directory.
|
``~/.config/instaloader/session-YOUR-USERNAME``. See
|
||||||
|
:option:`--sessionfile` option for how to override this path.
|
||||||
|
|
||||||
Programming Instaloader
|
Programming Instaloader
|
||||||
^^^^^^^^^^^^^^^^^^^^^^^
|
^^^^^^^^^^^^^^^^^^^^^^^
|
||||||
|
|||||||
+30
-3
@@ -193,9 +193,8 @@ Instaloader to login.
|
|||||||
|
|
||||||
.. option:: --sessionfile SESSIONFILE, -f SESSIONFILE
|
.. option:: --sessionfile SESSIONFILE, -f SESSIONFILE
|
||||||
|
|
||||||
Path for loading and storing session key file. Defaults to a path within
|
Path for loading and storing session key file. Defaults to
|
||||||
your temporary directory, encoding your local username and your Instagram
|
``~/.config/instaloader/session-YOUR-USERNAME``.
|
||||||
profile name.
|
|
||||||
|
|
||||||
.. option:: --password YOUR-PASSWORD, -p YOUR-PASSWORD
|
.. option:: --password YOUR-PASSWORD, -p YOUR-PASSWORD
|
||||||
|
|
||||||
@@ -224,6 +223,29 @@ How to Download
|
|||||||
``#hashtag`` or the profile name. Defaults to ``{date_utc}_UTC``.
|
``#hashtag`` or the profile name. Defaults to ``{date_utc}_UTC``.
|
||||||
See :ref:`filename-specification` for a list of supported tokens.
|
See :ref:`filename-specification` for a list of supported tokens.
|
||||||
|
|
||||||
|
.. option:: --resume-prefix prefix
|
||||||
|
|
||||||
|
For many targets, Instaloader is capable of resuming a previously-aborted
|
||||||
|
download loop. To do so, it creates a JSON file within the target directory
|
||||||
|
when interrupted. This option controls the prefix for filenames that are
|
||||||
|
used to save the information to resume an interrupted download. The default
|
||||||
|
prefix is ``iterator``.
|
||||||
|
|
||||||
|
Resuming an interrupted download is supported for most, but not all targets.
|
||||||
|
JSON files with resume information are always compressed, regardless of
|
||||||
|
:option:`--no-compress-json`.
|
||||||
|
|
||||||
|
This feature is turned off entirely with :option:`--no-resume`.
|
||||||
|
|
||||||
|
.. versionadded:: 4.5
|
||||||
|
|
||||||
|
.. option:: --no-resume
|
||||||
|
|
||||||
|
Do not resume a previously-aborted download iteration, and do not save such
|
||||||
|
information when interrupted.
|
||||||
|
|
||||||
|
.. versionadded:: 4.5
|
||||||
|
|
||||||
.. option:: --user-agent USER_AGENT
|
.. option:: --user-agent USER_AGENT
|
||||||
|
|
||||||
User Agent to use for HTTP requests. Per default, Instaloader pretends being
|
User Agent to use for HTTP requests. Per default, Instaloader pretends being
|
||||||
@@ -243,6 +265,11 @@ How to Download
|
|||||||
|
|
||||||
.. versionadded:: 4.2
|
.. versionadded:: 4.2
|
||||||
|
|
||||||
|
.. warning::
|
||||||
|
We discourage from using the "commit mode" due to its bugs when being used
|
||||||
|
with other command line options (e.g. :issue:`257` with
|
||||||
|
:option:`--fast-update` and :issue:`483` with :option:`--tagged`).
|
||||||
|
|
||||||
.. option:: --request-timeout N
|
.. option:: --request-timeout N
|
||||||
|
|
||||||
Seconds to wait before timing out a connection request.
|
Seconds to wait before timing out a connection request.
|
||||||
|
|||||||
+11
-2
@@ -28,8 +28,9 @@ Download Posts in a Specific Period
|
|||||||
-----------------------------------
|
-----------------------------------
|
||||||
|
|
||||||
To only download Instagram pictures (and metadata) that are within a specific
|
To only download Instagram pictures (and metadata) that are within a specific
|
||||||
period, you can play around with :func:`~itertools.dropwhile` and
|
period, you can simply use :func:`~itertools.dropwhile` and
|
||||||
:func:`~itertools.takewhile` from :mod:`itertools` like in this snippet.
|
:func:`~itertools.takewhile` from :mod:`itertools` on a generator that returns
|
||||||
|
Posts in **exact chronological order**, such as :meth:`Profile.get_posts`.
|
||||||
|
|
||||||
.. literalinclude:: codesnippets/121_since_until.py
|
.. literalinclude:: codesnippets/121_since_until.py
|
||||||
|
|
||||||
@@ -37,6 +38,14 @@ See also :class:`Post`, :meth:`Instaloader.download_post`.
|
|||||||
|
|
||||||
Discussed in :issue:`121`.
|
Discussed in :issue:`121`.
|
||||||
|
|
||||||
|
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.
|
||||||
|
|
||||||
|
.. literalinclude:: codesnippets/666_historical_hashtag_data.py
|
||||||
|
|
||||||
Likes of a Profile / Ghost Followers
|
Likes of a Profile / Ghost Followers
|
||||||
------------------------------------
|
------------------------------------
|
||||||
|
|
||||||
|
|||||||
@@ -5,13 +5,11 @@ import instaloader
|
|||||||
|
|
||||||
L = instaloader.Instaloader()
|
L = instaloader.Instaloader()
|
||||||
|
|
||||||
posts = instaloader.Hashtag.from_name(L.context, 'urbanphotography').get_posts()
|
posts = instaloader.Profile.from_username(L.context, "instagram").get_posts()
|
||||||
# or
|
|
||||||
# posts = instaloader.Profile.from_username(L.context, PROFILE).get_posts()
|
|
||||||
|
|
||||||
SINCE = datetime(2015, 5, 1)
|
SINCE = datetime(2015, 5, 1)
|
||||||
UNTIL = datetime(2015, 3, 1)
|
UNTIL = datetime(2015, 3, 1)
|
||||||
|
|
||||||
for post in takewhile(lambda p: p.date > UNTIL, dropwhile(lambda p: p.date > SINCE, posts)):
|
for post in takewhile(lambda p: p.date > UNTIL, dropwhile(lambda p: p.date > SINCE, posts)):
|
||||||
print(post.date)
|
print(post.date)
|
||||||
L.download_post(post, '#urbanphotography')
|
L.download_post(post, "instagram")
|
||||||
|
|||||||
@@ -0,0 +1,53 @@
|
|||||||
|
from argparse import ArgumentParser
|
||||||
|
from glob import glob
|
||||||
|
from os.path import expanduser
|
||||||
|
from platform import system
|
||||||
|
from sqlite3 import OperationalError, connect
|
||||||
|
|
||||||
|
try:
|
||||||
|
from instaloader import ConnectionException, Instaloader
|
||||||
|
except ModuleNotFoundError:
|
||||||
|
raise SystemExit("Instaloader not found.\n pip install [--user] instaloader")
|
||||||
|
|
||||||
|
|
||||||
|
def get_cookiefile():
|
||||||
|
default_cookiefile = {
|
||||||
|
"Windows": "~/AppData/Roaming/Mozilla/Firefox/Profiles/*/cookies.sqlite",
|
||||||
|
"Darwin": "~/Library/Application Support/Firefox/Profiles/*/cookies.sqlite",
|
||||||
|
}.get(system(), "~/.mozilla/firefox/*/cookies.sqlite")
|
||||||
|
cookiefiles = glob(expanduser(default_cookiefile))
|
||||||
|
if not cookiefiles:
|
||||||
|
raise SystemExit("No Firefox cookies.sqlite file found. Use -c COOKIEFILE.")
|
||||||
|
return cookiefiles[0]
|
||||||
|
|
||||||
|
|
||||||
|
def import_session(cookiefile, sessionfile):
|
||||||
|
print("Using cookies from {}.".format(cookiefile))
|
||||||
|
conn = connect(cookiefile)
|
||||||
|
try:
|
||||||
|
cookie_data = conn.execute(
|
||||||
|
"SELECT name, value FROM moz_cookies WHERE baseDomain='instagram.com'"
|
||||||
|
)
|
||||||
|
except OperationalError:
|
||||||
|
cookie_data = conn.execute(
|
||||||
|
"SELECT name, value FROM moz_cookies WHERE host LIKE '%instagram.com'"
|
||||||
|
)
|
||||||
|
instaloader = Instaloader(max_connection_attempts=1)
|
||||||
|
instaloader.context._session.cookies.update(cookie_data)
|
||||||
|
username = instaloader.test_login()
|
||||||
|
if not username:
|
||||||
|
raise SystemExit("Not logged in. Are you logged in successfully in Firefox?")
|
||||||
|
print("Imported session cookie for {}.".format(username))
|
||||||
|
instaloader.context.username = username
|
||||||
|
instaloader.save_session_to_file(sessionfile)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
p = ArgumentParser()
|
||||||
|
p.add_argument("-c", "--cookiefile")
|
||||||
|
p.add_argument("-f", "--sessionfile")
|
||||||
|
args = p.parse_args()
|
||||||
|
try:
|
||||||
|
import_session(args.cookiefile or get_cookiefile(), args.sessionfile)
|
||||||
|
except (ConnectionException, OperationalError) as e:
|
||||||
|
raise SystemExit("Cookie import failed: {}".format(e))
|
||||||
@@ -0,0 +1,30 @@
|
|||||||
|
from datetime import datetime
|
||||||
|
import instaloader
|
||||||
|
|
||||||
|
L = instaloader.Instaloader()
|
||||||
|
|
||||||
|
posts = instaloader.Hashtag.from_name(L.context, "urbanphotography").get_posts()
|
||||||
|
|
||||||
|
SINCE = datetime(2020, 5, 10) # further from today, inclusive
|
||||||
|
UNTIL = datetime(2020, 5, 11) # closer to today, not inclusive
|
||||||
|
|
||||||
|
k = 0 # initiate k
|
||||||
|
k_list = [] # uncomment this to tune k
|
||||||
|
|
||||||
|
for post in posts:
|
||||||
|
postdate = post.date
|
||||||
|
|
||||||
|
if postdate > UNTIL:
|
||||||
|
continue
|
||||||
|
elif postdate <= SINCE:
|
||||||
|
k += 1
|
||||||
|
if k == 50:
|
||||||
|
break
|
||||||
|
else:
|
||||||
|
continue
|
||||||
|
else:
|
||||||
|
L.download_post(post, "#urbanphotography")
|
||||||
|
k = 0 # set k to 0
|
||||||
|
# if you want to tune k, uncomment below to get your k max
|
||||||
|
#k_list.append(k)
|
||||||
|
#max(k_list)
|
||||||
@@ -1,23 +0,0 @@
|
|||||||
from glob import glob
|
|
||||||
from os.path import expanduser
|
|
||||||
from sqlite3 import connect
|
|
||||||
|
|
||||||
from instaloader import ConnectionException, Instaloader
|
|
||||||
|
|
||||||
# FIREFOXCOOKIEFILE = "/home/alex/.mozilla/firefox/l96w6b90.default/cookies.sqlite"
|
|
||||||
FIREFOXCOOKIEFILE = glob(expanduser("~/.mozilla/firefox/*.default/cookies.sqlite"))[0]
|
|
||||||
|
|
||||||
instaloader = Instaloader(max_connection_attempts=1)
|
|
||||||
instaloader.context._session.cookies.update(connect(FIREFOXCOOKIEFILE)
|
|
||||||
.execute("SELECT name, value FROM moz_cookies "
|
|
||||||
"WHERE baseDomain='instagram.com'"))
|
|
||||||
|
|
||||||
try:
|
|
||||||
username = instaloader.test_login()
|
|
||||||
if not username:
|
|
||||||
raise ConnectionException()
|
|
||||||
except ConnectionException:
|
|
||||||
raise SystemExit("Cookie import failed. Are you logged in successfully in Firefox?")
|
|
||||||
|
|
||||||
instaloader.context.username = username
|
|
||||||
instaloader.save_session_to_file()
|
|
||||||
+21
-8
@@ -18,6 +18,7 @@
|
|||||||
# documentation root, use os.path.abspath to make it absolute, like shown here.
|
# documentation root, use os.path.abspath to make it absolute, like shown here.
|
||||||
#
|
#
|
||||||
import os
|
import os
|
||||||
|
import platform
|
||||||
import subprocess
|
import subprocess
|
||||||
import sys
|
import sys
|
||||||
import typing
|
import typing
|
||||||
@@ -49,7 +50,25 @@ autodoc_member_order = 'bysource'
|
|||||||
intersphinx_mapping = {'python': ('https://docs.python.org/3', None),
|
intersphinx_mapping = {'python': ('https://docs.python.org/3', None),
|
||||||
'requests': ('https://requests.kennethreitz.org/en/master/', None)}
|
'requests': ('https://requests.kennethreitz.org/en/master/', None)}
|
||||||
|
|
||||||
extlinks = {'issue': ('https://github.com/instaloader/instaloader/issues/%s', 'issue #')}
|
nitpick_ignore = [('py:class', 'typing.Tuple')]
|
||||||
|
|
||||||
|
current_release = subprocess.check_output(["git", "describe", "--abbrev=0"]).decode("ascii")[1:-1]
|
||||||
|
date_format = "%e %b %Y" if platform.system() != "Windows" else "%d %b %Y"
|
||||||
|
current_release_date = subprocess.check_output(
|
||||||
|
["git", "log", "-1", "--tags", "--format=%ad", "--date=format:"+date_format]).decode("ascii")[:-1]
|
||||||
|
|
||||||
|
html_context = {'current_release': current_release, 'current_release_date': current_release_date}
|
||||||
|
|
||||||
|
extlinks = {
|
||||||
|
'issue': (
|
||||||
|
'https://github.com/instaloader/instaloader/issues/%s',
|
||||||
|
'Issue #'
|
||||||
|
),
|
||||||
|
'example': (
|
||||||
|
'https://raw.githubusercontent.com/instaloader/instaloader/v' + current_release + '/docs/codesnippets/%s',
|
||||||
|
'Example '
|
||||||
|
),
|
||||||
|
}
|
||||||
|
|
||||||
# Add any paths that contain templates here, relative to this directory.
|
# Add any paths that contain templates here, relative to this directory.
|
||||||
templates_path = ['_templates']
|
templates_path = ['_templates']
|
||||||
@@ -69,7 +88,7 @@ master_doc = 'index'
|
|||||||
|
|
||||||
# General information about the project.
|
# General information about the project.
|
||||||
project = 'Instaloader'
|
project = 'Instaloader'
|
||||||
copyright = '2017, Alexander Graf and Andre Koch-Kramer'
|
copyright = '2017-2020, Alexander Graf and Andre Koch-Kramer'
|
||||||
author = 'Alexander Graf and Andre Koch-Kramer'
|
author = 'Alexander Graf and Andre Koch-Kramer'
|
||||||
|
|
||||||
# The version info for the project you're documenting, acts as replacement for
|
# The version info for the project you're documenting, acts as replacement for
|
||||||
@@ -353,12 +372,6 @@ texinfo_documents = [
|
|||||||
# texinfo_no_detailmenu = False
|
# texinfo_no_detailmenu = False
|
||||||
|
|
||||||
|
|
||||||
current_release = subprocess.check_output(["git", "describe", "--abbrev=0"]).decode("ascii")[1:-1]
|
|
||||||
current_release_date = subprocess.check_output(["git", "log", "-1", "--tags", "--format=%ad", "--date=format:%e %b %Y"]).decode("ascii")[:-1]
|
|
||||||
|
|
||||||
html_context = {'current_release': current_release, 'current_release_date': current_release_date}
|
|
||||||
|
|
||||||
|
|
||||||
def setup(app):
|
def setup(app):
|
||||||
typing.TYPE_CHECKING = True
|
typing.TYPE_CHECKING = True
|
||||||
app.add_stylesheet("instaloader.css")
|
app.add_stylesheet("instaloader.css")
|
||||||
|
|||||||
+27
-26
@@ -35,8 +35,8 @@ reporting a problem, please keep the following in mind:
|
|||||||
- If not obvious, describe **which behavior you expected**
|
- If not obvious, describe **which behavior you expected**
|
||||||
instead of what actually happened.
|
instead of what actually happened.
|
||||||
|
|
||||||
- If you think an issue has been closed accidentally or inappropriately, feel
|
- If we have closed an issue apparently inadvertently or inappropriately, please
|
||||||
free to reopen it.
|
let us know.
|
||||||
|
|
||||||
Writing Code or Improving the Documentation
|
Writing Code or Improving the Documentation
|
||||||
-------------------------------------------
|
-------------------------------------------
|
||||||
@@ -45,28 +45,25 @@ Changes of the Instaloader source can be proposed as a
|
|||||||
`Pull Request <https://github.com/instaloader/instaloader/pulls>`__. There are only
|
`Pull Request <https://github.com/instaloader/instaloader/pulls>`__. There are only
|
||||||
few things to consider:
|
few things to consider:
|
||||||
|
|
||||||
- Sometimes, the most current code is not in the ``master`` branch. Check that
|
- Base your Pull Request on the ``master`` branch if it fixes a bug,
|
||||||
you forked from the most recent branch.
|
or the ``upcoming/v4.X`` branch (if it exists at the moment of submitting the PR)
|
||||||
|
otherwise.
|
||||||
|
|
||||||
- We use `Pylint <https://www.pylint.org/>`__ for error and syntax checking of
|
- We use `Pylint <https://www.pylint.org/>`__ for error and syntax checking of
|
||||||
the source. The file ``.travis.yml`` in the project's root directory
|
the source and `MyPy <https://github.com/python/mypy>`__ for type checking.
|
||||||
shows how it is invoked. Note that sometimes it might be better to disable a
|
Beware that sometimes it might be better to disable a warning rather than
|
||||||
warning rather than adapting the code to Pylint's desires.
|
adapting the code to a tool's desires.
|
||||||
|
|
||||||
- The documentation source is located in the ``docs`` folder. The file
|
- The documentation source is located in the ``docs`` folder. The file
|
||||||
``cli-options.rst`` is merely an RST-formatted copy of ``instaloader --help``
|
``cli-options.rst`` is merely an RST-formatted copy of ``instaloader --help``
|
||||||
output, of which the source is in ``instaloader/__main__.py``.
|
output, of which the source is in ``instaloader/__main__.py``.
|
||||||
|
|
||||||
- Feel free to contact us, even if you "only" have Proof-of-Concepts or
|
Proposing Features
|
||||||
not-fully integrated changes. They already might be an advance for the
|
------------------
|
||||||
project.
|
|
||||||
|
|
||||||
Suggesting Features
|
|
||||||
-------------------
|
|
||||||
|
|
||||||
.. goal-start
|
.. goal-start
|
||||||
|
|
||||||
Instaloader's goal is to mimick the browser's behavior to access the data that
|
Instaloader's goal is to mimic the browser's behavior to access the data that
|
||||||
is available through the Instagram web interface, pack this data into complete
|
is available through the Instagram web interface, pack this data into complete
|
||||||
and easily-(re)usable python objects, and provide a user interface for the most
|
and easily-(re)usable python objects, and provide a user interface for the most
|
||||||
common downloading and metadata collection tasks, without changing any of the
|
common downloading and metadata collection tasks, without changing any of the
|
||||||
@@ -74,10 +71,9 @@ online data.
|
|||||||
|
|
||||||
.. goal-end
|
.. goal-end
|
||||||
|
|
||||||
If you have an idea of how Instaloader should be enhanced, but do not want to
|
Prior spending effort on implementing a new feature, it might be appropriate to
|
||||||
implement the feature yourself, feel free to open a ticket in our
|
clarify how it could fit into the project's scope or discuss implementation
|
||||||
`Issue Tracker <https://github.com/instaloader/instaloader/issues>`__.
|
details. If you feel the need to do so, please create a "feature suggestion".
|
||||||
Please consider the following:
|
|
||||||
|
|
||||||
- Instaloader already has plenty of features. **Check the documentation**
|
- Instaloader already has plenty of features. **Check the documentation**
|
||||||
beforehand to ensure your desired suggestion is not already implemented.
|
beforehand to ensure your desired suggestion is not already implemented.
|
||||||
@@ -86,16 +82,21 @@ Please consider the following:
|
|||||||
an issue suggesting the same or a similar feature, share your thoughts in a
|
an issue suggesting the same or a similar feature, share your thoughts in a
|
||||||
comment there, instead of opening a new issue.
|
comment there, instead of opening a new issue.
|
||||||
|
|
||||||
- If possible, provide us a **use case of the feature**: How could the user
|
- **Motivate the feature**, i.e.
|
||||||
invoke the new function? Which problem would it solve? If new information is
|
|
||||||
obtained, how would it be further processed?
|
|
||||||
|
|
||||||
- If not obvious, briefly motivate how your suggested feature **conforms with
|
- Provide us a **use case of the feature**: How could the user
|
||||||
Instaloader's project goal**.
|
invoke the new function? Which problem would it solve? If new information is
|
||||||
|
obtained, how would it be further processed?
|
||||||
|
|
||||||
- **Be patient**. Naturally, bugs and pull requests have a higher priority than
|
- Describe already-working **alternatives of the feature** and how they
|
||||||
feature suggestions. Keep in mind that this is a free software project, and
|
compare to your proposed feature.
|
||||||
unfortunately we only have limited time to work on it.
|
|
||||||
|
- Briefly describe how your suggested feature **conforms with Instaloader's
|
||||||
|
project goal**.
|
||||||
|
|
||||||
|
- Explain your **solution ideas**. Describe your ideas on how the feature could
|
||||||
|
be implemented and the underlying problem could be solved. Also **describe
|
||||||
|
alternatives** that you have considered.
|
||||||
|
|
||||||
Donations
|
Donations
|
||||||
---------
|
---------
|
||||||
|
|||||||
@@ -40,6 +40,8 @@ See :ref:`install` for more options on how to install Instaloader.
|
|||||||
- allows **fine-grained customization** of filters and where to store
|
- allows **fine-grained customization** of filters and where to store
|
||||||
downloaded media,
|
downloaded media,
|
||||||
|
|
||||||
|
- automatically **resumes previously-interrupted** download iterations,
|
||||||
|
|
||||||
- is free `open source <https://github.com/instaloader/instaloader>`__
|
- is free `open source <https://github.com/instaloader/instaloader>`__
|
||||||
software written in Python.
|
software written in Python.
|
||||||
|
|
||||||
|
|||||||
+37
-13
@@ -27,12 +27,26 @@ expired. In case the retry continuously fails for some reason, which should not
|
|||||||
happen in normal conditions, consider adjusting the
|
happen in normal conditions, consider adjusting the
|
||||||
:option:`--max-connection-attempts` option.
|
:option:`--max-connection-attempts` option.
|
||||||
|
|
||||||
|
There have been observations that services, that in their nature offer
|
||||||
|
promiscious IP addresses, such as cloud, VPN and public proxy services, might be
|
||||||
|
subject to significantly stricter limits for anonymous access. However,
|
||||||
|
logged-in accesses (see :option:`--login`) do not seem to be affected.
|
||||||
|
|
||||||
|
Too many queries in the last time
|
||||||
|
---------------------------------
|
||||||
|
|
||||||
**"Too many queries in the last time"** is not an error. It is a notice that the
|
**"Too many queries in the last time"** is not an error. It is a notice that the
|
||||||
rate limit has almost been reached, according to Instaloader's own rate
|
rate limit has almost been reached, according to Instaloader's own rate
|
||||||
accounting mechanism. We regularly adjust this mechanism to match Instagram's
|
accounting mechanism. We regularly adjust this mechanism to match Instagram's
|
||||||
current rate limiting.
|
current rate limiting.
|
||||||
|
|
||||||
Login error
|
Private but not followed
|
||||||
|
------------------------
|
||||||
|
|
||||||
|
You have to follow a private account to access most of its associated
|
||||||
|
information.
|
||||||
|
|
||||||
|
Login Error
|
||||||
-----------
|
-----------
|
||||||
|
|
||||||
Instaloader's login *should* work fine, both with and without
|
Instaloader's login *should* work fine, both with and without
|
||||||
@@ -40,21 +54,31 @@ Two-Factor-Authentication. It also supports handling the *checkpoint challenge*,
|
|||||||
issued when Instagram suspects authentication activity on your account, by
|
issued when Instagram suspects authentication activity on your account, by
|
||||||
pointing the user to an URL to be opened in a browser.
|
pointing the user to an URL to be opened in a browser.
|
||||||
|
|
||||||
Nevertheless, in :issue:`92` users report problems with logging in. To still use
|
Nevertheless, in :issue:`92` and :issue:`615` users reported problems with
|
||||||
Instaloader's logged-in functionality, you may use the following script to
|
logging in. We recommend to always keep the sessionfile which Instaloader
|
||||||
workaround login problems by importing the session cookies from Firefox and
|
creates when using :option:`--login`. If a sessionfile is present,
|
||||||
bypassing Instaloader's login.
|
:option:`--login` does not make make use of the failure-prone login procedure.
|
||||||
|
Also, sessionfiles usually do not expire and can be copied between different
|
||||||
|
computers without any problems.
|
||||||
|
|
||||||
.. literalinclude:: codesnippets/92_import_firefox_session.py
|
If you do not have a sessionfile present, you may use the following script
|
||||||
|
(:example:`615_import_firefox_session.py`) to workaround login problems by
|
||||||
|
importing the session cookies from Firefox and bypassing Instaloader's login and
|
||||||
|
so still use Instaloader's logged-in functionality.
|
||||||
|
|
||||||
To use this,
|
.. literalinclude:: codesnippets/615_import_firefox_session.py
|
||||||
|
|
||||||
#. login to Instagram in Firefox,
|
To use this script,
|
||||||
|
|
||||||
#. execute the snippet,
|
#. Download the script: :example:`615_import_firefox_session.py`,
|
||||||
|
|
||||||
#. then, ``instaloader -l USERNAME`` should work fine.
|
#. Login to Instagram in Firefox,
|
||||||
|
|
||||||
If you do not use your default firefox profile, or your operating system has the
|
#. Execute the snippet, e.g. with ``python 615_import_firefox_session.py``,
|
||||||
paths differently set up, you may have to alter the ``FIREFOXCOOKIEFILE``
|
|
||||||
variable first.
|
#. Then, ``instaloader -l USERNAME`` should work fine.
|
||||||
|
|
||||||
|
This script also supports specifying a cookiefile path, which may be useful if
|
||||||
|
you use multiple Firefox profiles or if your operating system has the directory
|
||||||
|
structure differently set up. Also, you can specify an alternative sessionfile
|
||||||
|
path.
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
"""Download pictures (or videos) along with their captions and other metadata from Instagram."""
|
"""Download pictures (or videos) along with their captions and other metadata from Instagram."""
|
||||||
|
|
||||||
|
|
||||||
__version__ = '4.4'
|
__version__ = '4.5a1'
|
||||||
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
@@ -14,6 +14,7 @@ else:
|
|||||||
|
|
||||||
from .exceptions import *
|
from .exceptions import *
|
||||||
from .instaloader import Instaloader
|
from .instaloader import Instaloader
|
||||||
from .instaloadercontext import InstaloaderContext
|
from .instaloadercontext import InstaloaderContext, RateController
|
||||||
|
from .nodeiterator import NodeIterator, FrozenNodeIterator, resumable_iteration
|
||||||
from .structures import (Hashtag, Highlight, Post, PostSidecarNode, PostComment, PostCommentAnswer, PostLocation,
|
from .structures import (Hashtag, Highlight, Post, PostSidecarNode, PostComment, PostCommentAnswer, PostLocation,
|
||||||
Profile, Story, StoryItem, TopSearchResults, load_structure_from_file, save_structure_to_file)
|
Profile, Story, StoryItem, TopSearchResults, load_structure_from_file, save_structure_to_file)
|
||||||
|
|||||||
+19
-11
@@ -82,6 +82,8 @@ def _main(instaloader: Instaloader, targetlist: List[str],
|
|||||||
instaloader.context.log('Only download storyitems with property "{}".'.format(storyitem_filter_str))
|
instaloader.context.log('Only download storyitems with property "{}".'.format(storyitem_filter_str))
|
||||||
# Login, if desired
|
# Login, if desired
|
||||||
if username is not None:
|
if username is not None:
|
||||||
|
if not re.match(r"^[A-Za-z0-9._]+$", username):
|
||||||
|
instaloader.context.error("Warning: Parameter \"{}\" for --login is not a valid username.".format(username))
|
||||||
try:
|
try:
|
||||||
instaloader.load_session_from_file(username, sessionfile)
|
instaloader.load_session_from_file(username, sessionfile)
|
||||||
except FileNotFoundError as err:
|
except FileNotFoundError as err:
|
||||||
@@ -143,7 +145,7 @@ def _main(instaloader: Instaloader, targetlist: List[str],
|
|||||||
for followee in profile.get_followees():
|
for followee in profile.get_followees():
|
||||||
instaloader.save_profile_id(followee)
|
instaloader.save_profile_id(followee)
|
||||||
profiles.add(followee)
|
profiles.add(followee)
|
||||||
elif re.match(r"^#[A-Za-z0-9]+$", target):
|
elif re.match(r"^#\w+$", target):
|
||||||
instaloader.download_hashtag(hashtag=target[1:], max_count=max_count, fast_update=fast_update,
|
instaloader.download_hashtag(hashtag=target[1:], max_count=max_count, fast_update=fast_update,
|
||||||
post_filter=post_filter,
|
post_filter=post_filter,
|
||||||
profile_pic=download_profile_pic, posts=download_posts)
|
profile_pic=download_profile_pic, posts=download_posts)
|
||||||
@@ -196,8 +198,8 @@ def _main(instaloader: Instaloader, targetlist: List[str],
|
|||||||
if len(profiles) > 1:
|
if len(profiles) > 1:
|
||||||
instaloader.context.log("Downloading {} profiles: {}".format(len(profiles),
|
instaloader.context.log("Downloading {} profiles: {}".format(len(profiles),
|
||||||
' '.join([p.username for p in profiles])))
|
' '.join([p.username for p in profiles])))
|
||||||
if profiles and download_profile_pic and not instaloader.context.is_logged_in:
|
if profiles and (download_profile_pic or download_posts) and not instaloader.context.is_logged_in:
|
||||||
instaloader.context.error("Warning: Use --login to download HD version of profile pictures.")
|
instaloader.context.error("Warning: Use --login to download higher-quality versions of pictures.")
|
||||||
instaloader.download_profiles(profiles,
|
instaloader.download_profiles(profiles,
|
||||||
download_profile_pic, download_posts, download_tagged, download_igtv,
|
download_profile_pic, download_posts, download_tagged, download_igtv,
|
||||||
download_highlights, download_stories,
|
download_highlights, download_stories,
|
||||||
@@ -349,6 +351,13 @@ def main():
|
|||||||
'--dirname-pattern. {profile} is replaced by the profile name,'
|
'--dirname-pattern. {profile} is replaced by the profile name,'
|
||||||
'{target} is replaced by the target you specified, i.e. either :feed'
|
'{target} is replaced by the target you specified, i.e. either :feed'
|
||||||
'#hashtag or the profile name. Defaults to \'{date_utc}_UTC\'')
|
'#hashtag or the profile name. Defaults to \'{date_utc}_UTC\'')
|
||||||
|
g_how.add_argument('--resume-prefix', metavar='PREFIX',
|
||||||
|
help='Prefix for filenames that are used to save the information to resume an interrupted '
|
||||||
|
'download.')
|
||||||
|
g_how.add_argument('--no-resume', action='store_true',
|
||||||
|
help='Do not resume a previously-aborted download iteration, and do not save such information '
|
||||||
|
'when interrupted.')
|
||||||
|
g_how.add_argument('--use-aged-resume-files', action='store_true', help=SUPPRESS)
|
||||||
g_how.add_argument('--user-agent',
|
g_how.add_argument('--user-agent',
|
||||||
help='User Agent to use for HTTP requests. Defaults to \'{}\'.'.format(default_user_agent()))
|
help='User Agent to use for HTTP requests. Defaults to \'{}\'.'.format(default_user_agent()))
|
||||||
g_how.add_argument('-S', '--no-sleep', action='store_true', help=SUPPRESS)
|
g_how.add_argument('-S', '--no-sleep', action='store_true', help=SUPPRESS)
|
||||||
@@ -356,10 +365,7 @@ def main():
|
|||||||
help='Maximum number of connection attempts until a request is aborted. Defaults to 3. If a '
|
help='Maximum number of connection attempts until a request is aborted. Defaults to 3. If a '
|
||||||
'connection fails, it can be manually skipped by hitting CTRL+C. Set this to 0 to retry '
|
'connection fails, it can be manually skipped by hitting CTRL+C. Set this to 0 to retry '
|
||||||
'infinitely.')
|
'infinitely.')
|
||||||
g_how.add_argument('--commit-mode', action='store_true',
|
g_how.add_argument('--commit-mode', action='store_true', help=SUPPRESS)
|
||||||
help='Tries to ensure downloaded images avoid corruption in case of unexpected interruption. '
|
|
||||||
'If the last picture is corrupted, Instaloader will fix the picture the next time it is run. '
|
|
||||||
'Requires the JSON metadata to be saved.')
|
|
||||||
g_how.add_argument('--request-timeout', metavar='N', type=float,
|
g_how.add_argument('--request-timeout', metavar='N', type=float,
|
||||||
help='seconds to wait before timing out a connection request')
|
help='seconds to wait before timing out a connection request')
|
||||||
|
|
||||||
@@ -395,6 +401,10 @@ def main():
|
|||||||
raise SystemExit("--no-captions and --post-metadata-txt or --storyitem-metadata-txt given; "
|
raise SystemExit("--no-captions and --post-metadata-txt or --storyitem-metadata-txt given; "
|
||||||
"That contradicts.")
|
"That contradicts.")
|
||||||
|
|
||||||
|
if args.no_resume and args.resume_prefix:
|
||||||
|
raise SystemExit("--no-resume and --resume-prefix given; That contradicts.")
|
||||||
|
resume_prefix = (args.resume_prefix if args.resume_prefix else 'iterator') if not args.no_resume else None
|
||||||
|
|
||||||
if args.no_pictures and args.fast_update:
|
if args.no_pictures and args.fast_update:
|
||||||
raise SystemExit('--no-pictures and --fast-update cannot be used together.')
|
raise SystemExit('--no-pictures and --fast-update cannot be used together.')
|
||||||
|
|
||||||
@@ -403,9 +413,6 @@ def main():
|
|||||||
download_posts = not (args.no_posts or args.stories_only or args.profile_pic_only)
|
download_posts = not (args.no_posts or args.stories_only or args.profile_pic_only)
|
||||||
download_stories = args.stories or args.stories_only
|
download_stories = args.stories or args.stories_only
|
||||||
|
|
||||||
if args.commit_mode and args.no_metadata_json:
|
|
||||||
raise SystemExit('--commit-mode requires JSON metadata to be saved.')
|
|
||||||
|
|
||||||
loader = Instaloader(sleep=not args.no_sleep, quiet=args.quiet, user_agent=args.user_agent,
|
loader = Instaloader(sleep=not args.no_sleep, quiet=args.quiet, user_agent=args.user_agent,
|
||||||
dirname_pattern=args.dirname_pattern, filename_pattern=args.filename_pattern,
|
dirname_pattern=args.dirname_pattern, filename_pattern=args.filename_pattern,
|
||||||
download_pictures=not args.no_pictures,
|
download_pictures=not args.no_pictures,
|
||||||
@@ -417,7 +424,8 @@ def main():
|
|||||||
storyitem_metadata_txt_pattern=storyitem_metadata_txt_pattern,
|
storyitem_metadata_txt_pattern=storyitem_metadata_txt_pattern,
|
||||||
max_connection_attempts=args.max_connection_attempts,
|
max_connection_attempts=args.max_connection_attempts,
|
||||||
request_timeout=args.request_timeout,
|
request_timeout=args.request_timeout,
|
||||||
commit_mode=args.commit_mode)
|
resume_prefix=resume_prefix,
|
||||||
|
check_resume_bbd=not args.use_aged_resume_files)
|
||||||
_main(loader,
|
_main(loader,
|
||||||
args.profile,
|
args.profile,
|
||||||
username=args.login.lower() if args.login is not None else None,
|
username=args.login.lower() if args.login is not None else None,
|
||||||
|
|||||||
+151
-127
@@ -1,6 +1,5 @@
|
|||||||
import getpass
|
import getpass
|
||||||
import json
|
import json
|
||||||
import lzma
|
|
||||||
import os
|
import os
|
||||||
import platform
|
import platform
|
||||||
import re
|
import re
|
||||||
@@ -20,13 +19,28 @@ import requests
|
|||||||
import urllib3 # type: ignore
|
import urllib3 # type: ignore
|
||||||
|
|
||||||
from .exceptions import *
|
from .exceptions import *
|
||||||
from .instaloadercontext import InstaloaderContext
|
from .instaloadercontext import InstaloaderContext, RateController
|
||||||
|
from .nodeiterator import NodeIterator, resumable_iteration
|
||||||
from .structures import (Hashtag, Highlight, JsonExportable, Post, PostLocation, Profile, Story, StoryItem,
|
from .structures import (Hashtag, Highlight, JsonExportable, Post, PostLocation, Profile, Story, StoryItem,
|
||||||
save_structure_to_file, load_structure_from_file)
|
load_structure_from_file, save_structure_to_file)
|
||||||
|
|
||||||
|
|
||||||
def get_default_session_filename(username: str) -> str:
|
def get_default_session_filename(username: str) -> str:
|
||||||
"""Returns default session filename for given username."""
|
"""Returns default session filename for given username."""
|
||||||
|
sessionfilename = "session-{}".format(username)
|
||||||
|
if platform.system() == "Windows":
|
||||||
|
# on Windows, use %LOCALAPPDATA%\Instaloader\session-USERNAME
|
||||||
|
localappdata = os.getenv("LOCALAPPDATA")
|
||||||
|
if localappdata is not None:
|
||||||
|
return os.path.join(localappdata, "Instaloader", sessionfilename)
|
||||||
|
# legacy fallback - store in temp dir if %LOCALAPPDATA% is not set
|
||||||
|
return os.path.join(tempfile.gettempdir(), ".instaloader-" + getpass.getuser(), sessionfilename)
|
||||||
|
# on Unix, use ~/.config/instaloader/session-USERNAME
|
||||||
|
return os.path.join(os.getenv("XDG_CONFIG_HOME", os.path.expanduser("~/.config")), "instaloader", sessionfilename)
|
||||||
|
|
||||||
|
|
||||||
|
def get_legacy_session_filename(username: str) -> str:
|
||||||
|
"""Returns legacy (until v4.4.3) default session filename for given username."""
|
||||||
dirname = tempfile.gettempdir() + "/" + ".instaloader-" + getpass.getuser()
|
dirname = tempfile.gettempdir() + "/" + ".instaloader-" + getpass.getuser()
|
||||||
filename = dirname + "/" + "session-" + username
|
filename = dirname + "/" + "session-" + username
|
||||||
return filename.lower()
|
return filename.lower()
|
||||||
@@ -103,22 +117,20 @@ class _ArbitraryItemFormatter(string.Formatter):
|
|||||||
|
|
||||||
|
|
||||||
class _PostPathFormatter(_ArbitraryItemFormatter):
|
class _PostPathFormatter(_ArbitraryItemFormatter):
|
||||||
def vformat(self, format_string, args, kwargs):
|
|
||||||
"""Override :meth:`string.Formatter.vformat` for character substitution in paths for Windows, see issue #84."""
|
|
||||||
ret = super().vformat(format_string, args, kwargs)
|
|
||||||
if platform.system() == 'Windows':
|
|
||||||
ret = ret.replace(':', '\ua789').replace('<', '\ufe64').replace('>', '\ufe65').replace('\"', '\uff02')
|
|
||||||
ret = ret.replace('\\', '\ufe68').replace('|', '\uff5c').replace('?', '\ufe16').replace('*', '\uff0a')
|
|
||||||
return ret
|
|
||||||
|
|
||||||
def get_value(self, key, args, kwargs):
|
def get_value(self, key, args, kwargs):
|
||||||
"""Replaces '/' with similar looking Division Slash and on windows newline with space"""
|
|
||||||
ret = super().get_value(key, args, kwargs)
|
ret = super().get_value(key, args, kwargs)
|
||||||
if not isinstance(ret, str):
|
if not isinstance(ret, str):
|
||||||
return ret
|
return ret
|
||||||
|
return self.sanitize_path(ret)
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def sanitize_path(ret: str) -> str:
|
||||||
|
"""Replaces '/' with similar looking Division Slash and some other illegal filename characters on Windows."""
|
||||||
ret = ret.replace('/', '\u2215')
|
ret = ret.replace('/', '\u2215')
|
||||||
if platform.system() == 'Windows':
|
if platform.system() == 'Windows':
|
||||||
ret = ret.replace('\n', ' ')
|
ret = ret.replace(':', '\uff1a').replace('<', '\ufe64').replace('>', '\ufe65').replace('\"', '\uff02')
|
||||||
|
ret = ret.replace('\\', '\ufe68').replace('|', '\uff5c').replace('?', '\ufe16').replace('*', '\uff0a')
|
||||||
|
ret = ret.replace('\n', ' ').replace('\r', ' ')
|
||||||
return ret
|
return ret
|
||||||
|
|
||||||
|
|
||||||
@@ -141,8 +153,10 @@ class Instaloader:
|
|||||||
txt file.
|
txt file.
|
||||||
:param storyitem_metadata_txt_pattern: :option:`--storyitem-metadata-txt`, default is empty (=none)
|
:param storyitem_metadata_txt_pattern: :option:`--storyitem-metadata-txt`, default is empty (=none)
|
||||||
:param max_connection_attempts: :option:`--max-connection-attempts`
|
:param max_connection_attempts: :option:`--max-connection-attempts`
|
||||||
:param commit_mode: :option:`--commit-mode`
|
|
||||||
:param request_timeout: :option:`--request-timeout`, set per-request timeout (seconds)
|
:param request_timeout: :option:`--request-timeout`, set per-request timeout (seconds)
|
||||||
|
:param rate_controller: Generator for a :class:`RateController` to override rate controlling behavior
|
||||||
|
:param resume_prefix: :option:`--resume-prefix`, or None for :option:`--no-resume`.
|
||||||
|
:param check_resume_bbd: Whether to check the date of expiry of resume files and reject them if expired.
|
||||||
|
|
||||||
.. attribute:: context
|
.. attribute:: context
|
||||||
|
|
||||||
@@ -166,9 +180,12 @@ class Instaloader:
|
|||||||
storyitem_metadata_txt_pattern: str = None,
|
storyitem_metadata_txt_pattern: str = None,
|
||||||
max_connection_attempts: int = 3,
|
max_connection_attempts: int = 3,
|
||||||
request_timeout: Optional[float] = None,
|
request_timeout: Optional[float] = None,
|
||||||
commit_mode: bool = False):
|
rate_controller: Optional[Callable[[InstaloaderContext], RateController]] = None,
|
||||||
|
resume_prefix: Optional[str] = "iterator",
|
||||||
|
check_resume_bbd: bool = True):
|
||||||
|
|
||||||
self.context = InstaloaderContext(sleep, quiet, user_agent, max_connection_attempts, request_timeout)
|
self.context = InstaloaderContext(sleep, quiet, user_agent, max_connection_attempts,
|
||||||
|
request_timeout, rate_controller)
|
||||||
|
|
||||||
# configuration parameters
|
# configuration parameters
|
||||||
self.dirname_pattern = dirname_pattern or "{target}"
|
self.dirname_pattern = dirname_pattern or "{target}"
|
||||||
@@ -184,12 +201,8 @@ class Instaloader:
|
|||||||
else post_metadata_txt_pattern
|
else post_metadata_txt_pattern
|
||||||
self.storyitem_metadata_txt_pattern = '' if storyitem_metadata_txt_pattern is None \
|
self.storyitem_metadata_txt_pattern = '' if storyitem_metadata_txt_pattern is None \
|
||||||
else storyitem_metadata_txt_pattern
|
else storyitem_metadata_txt_pattern
|
||||||
self.commit_mode = commit_mode
|
self.resume_prefix = resume_prefix
|
||||||
if self.commit_mode and not self.save_metadata:
|
self.check_resume_bbd = check_resume_bbd
|
||||||
raise InvalidArgumentException("Commit mode requires JSON metadata to be saved.")
|
|
||||||
|
|
||||||
# Used to keep state in commit mode
|
|
||||||
self._committed = None # type: Optional[bool]
|
|
||||||
|
|
||||||
@contextmanager
|
@contextmanager
|
||||||
def anonymous_copy(self):
|
def anonymous_copy(self):
|
||||||
@@ -211,7 +224,8 @@ class Instaloader:
|
|||||||
storyitem_metadata_txt_pattern=self.storyitem_metadata_txt_pattern,
|
storyitem_metadata_txt_pattern=self.storyitem_metadata_txt_pattern,
|
||||||
max_connection_attempts=self.context.max_connection_attempts,
|
max_connection_attempts=self.context.max_connection_attempts,
|
||||||
request_timeout=self.context.request_timeout,
|
request_timeout=self.context.request_timeout,
|
||||||
commit_mode=self.commit_mode)
|
resume_prefix=self.resume_prefix,
|
||||||
|
check_resume_bbd=self.check_resume_bbd)
|
||||||
yield new_loader
|
yield new_loader
|
||||||
self.context.error_log.extend(new_loader.context.error_log)
|
self.context.error_log.extend(new_loader.context.error_log)
|
||||||
new_loader.context.error_log = [] # avoid double-printing of errors
|
new_loader.context.error_log = [] # avoid double-printing of errors
|
||||||
@@ -237,15 +251,9 @@ class Instaloader:
|
|||||||
if filename_suffix is not None:
|
if filename_suffix is not None:
|
||||||
filename += '_' + filename_suffix
|
filename += '_' + filename_suffix
|
||||||
filename += '.' + file_extension
|
filename += '.' + file_extension
|
||||||
# A post is considered "commited" if the json file exists and is not malformed.
|
if os.path.isfile(filename):
|
||||||
if self.commit_mode:
|
self.context.log(filename + ' exists', end=' ', flush=True)
|
||||||
if self._committed and os.path.isfile(filename):
|
return False
|
||||||
self.context.log(filename + ' exists', end=' ', flush=True)
|
|
||||||
return False
|
|
||||||
else:
|
|
||||||
if os.path.isfile(filename):
|
|
||||||
self.context.log(filename + ' exists', end=' ', flush=True)
|
|
||||||
return False
|
|
||||||
self.context.get_and_write_raw(url, filename)
|
self.context.get_and_write_raw(url, filename)
|
||||||
os.utime(filename, (datetime.now().timestamp(), mtime.timestamp()))
|
os.utime(filename, (datetime.now().timestamp(), mtime.timestamp()))
|
||||||
return True
|
return True
|
||||||
@@ -357,6 +365,24 @@ class Instaloader:
|
|||||||
os.utime(filename, (datetime.now().timestamp(), mtime.timestamp()))
|
os.utime(filename, (datetime.now().timestamp(), mtime.timestamp()))
|
||||||
self.context.log('geo', end=' ', flush=True)
|
self.context.log('geo', end=' ', flush=True)
|
||||||
|
|
||||||
|
def format_filename_within_target_path(self,
|
||||||
|
target: Union[str, Path],
|
||||||
|
owner_profile: Optional[Profile],
|
||||||
|
identifier: str,
|
||||||
|
name_suffix: str,
|
||||||
|
extension: str):
|
||||||
|
"""Returns a filename within the target path.
|
||||||
|
|
||||||
|
.. versionadded:: 4.5"""
|
||||||
|
if ((format_string_contains_key(self.dirname_pattern, 'profile') or
|
||||||
|
format_string_contains_key(self.dirname_pattern, 'target'))):
|
||||||
|
profile_str = owner_profile.username.lower() if owner_profile is not None else target
|
||||||
|
return os.path.join(self.dirname_pattern.format(profile=profile_str, target=target),
|
||||||
|
'{0}_{1}.{2}'.format(identifier, name_suffix, extension))
|
||||||
|
else:
|
||||||
|
return os.path.join(self.dirname_pattern.format(),
|
||||||
|
'{0}_{1}_{2}.{3}'.format(target, identifier, name_suffix, extension))
|
||||||
|
|
||||||
@_retry_on_connection_error
|
@_retry_on_connection_error
|
||||||
def download_title_pic(self, url: str, target: Union[str, Path], name_suffix: str, owner_profile: Optional[Profile],
|
def download_title_pic(self, url: str, target: Union[str, Path], name_suffix: str, owner_profile: Optional[Profile],
|
||||||
_attempt: int = 1) -> None:
|
_attempt: int = 1) -> None:
|
||||||
@@ -377,16 +403,7 @@ class Instaloader:
|
|||||||
else:
|
else:
|
||||||
pic_bytes = http_response.content
|
pic_bytes = http_response.content
|
||||||
pic_identifier = md5(pic_bytes).hexdigest()[:16]
|
pic_identifier = md5(pic_bytes).hexdigest()[:16]
|
||||||
pic_extension = 'jpg'
|
filename = self.format_filename_within_target_path(target, owner_profile, pic_identifier, name_suffix, 'jpg')
|
||||||
if ((format_string_contains_key(self.dirname_pattern, 'profile') or
|
|
||||||
format_string_contains_key(self.dirname_pattern, 'target'))):
|
|
||||||
profile_str = owner_profile.username.lower() if owner_profile is not None else target
|
|
||||||
filename = '{0}/{1}_{2}.{3}'.format(self.dirname_pattern.format(profile=profile_str,
|
|
||||||
target=target),
|
|
||||||
pic_identifier, name_suffix, pic_extension)
|
|
||||||
else:
|
|
||||||
filename = '{0}/{1}_{2}_{3}.{4}'.format(self.dirname_pattern.format(), target,
|
|
||||||
pic_identifier, name_suffix, pic_extension)
|
|
||||||
content_length = http_response.headers.get('Content-Length', None)
|
content_length = http_response.headers.get('Content-Length', None)
|
||||||
if os.path.isfile(filename) and (not self.context.is_logged_in or
|
if os.path.isfile(filename) and (not self.context.is_logged_in or
|
||||||
(content_length is not None and
|
(content_length is not None and
|
||||||
@@ -443,6 +460,8 @@ class Instaloader:
|
|||||||
"""
|
"""
|
||||||
if filename is None:
|
if filename is None:
|
||||||
filename = get_default_session_filename(username)
|
filename = get_default_session_filename(username)
|
||||||
|
if not os.path.exists(filename):
|
||||||
|
filename = get_legacy_session_filename(username)
|
||||||
with open(filename, 'rb') as sessionfile:
|
with open(filename, 'rb') as sessionfile:
|
||||||
self.context.load_session_from_file(username, sessionfile)
|
self.context.load_session_from_file(username, sessionfile)
|
||||||
self.context.log("Loaded session from %s." % filename)
|
self.context.log("Loaded session from %s." % filename)
|
||||||
@@ -487,12 +506,11 @@ class Instaloader:
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
dirname = _PostPathFormatter(post).format(self.dirname_pattern, target=target)
|
dirname = _PostPathFormatter(post).format(self.dirname_pattern, target=target)
|
||||||
filename = dirname + '/' + self.format_filename(post, target=target)
|
filename = os.path.join(dirname, self.format_filename(post, target=target))
|
||||||
os.makedirs(os.path.dirname(filename), exist_ok=True)
|
os.makedirs(os.path.dirname(filename), exist_ok=True)
|
||||||
|
|
||||||
# Download the image(s) / video thumbnail and videos within sidecars if desired
|
# Download the image(s) / video thumbnail and videos within sidecars if desired
|
||||||
downloaded = True
|
downloaded = True
|
||||||
self._committed = self.check_if_committed(filename)
|
|
||||||
if self.download_pictures:
|
if self.download_pictures:
|
||||||
if post.typename == 'GraphSidecar':
|
if post.typename == 'GraphSidecar':
|
||||||
edge_number = 1
|
edge_number = 1
|
||||||
@@ -616,7 +634,7 @@ class Instaloader:
|
|||||||
|
|
||||||
date_local = item.date_local
|
date_local = item.date_local
|
||||||
dirname = _PostPathFormatter(item).format(self.dirname_pattern, target=target)
|
dirname = _PostPathFormatter(item).format(self.dirname_pattern, target=target)
|
||||||
filename = dirname + '/' + self.format_filename(item, target=target)
|
filename = os.path.join(dirname, self.format_filename(item, target=target))
|
||||||
os.makedirs(os.path.dirname(filename), exist_ok=True)
|
os.makedirs(os.path.dirname(filename), exist_ok=True)
|
||||||
downloaded = False
|
downloaded = False
|
||||||
if not item.is_video or self.download_video_thumbnails is True:
|
if not item.is_video or self.download_video_thumbnails is True:
|
||||||
@@ -681,7 +699,8 @@ class Instaloader:
|
|||||||
name = user_highlight.owner_username
|
name = user_highlight.owner_username
|
||||||
highlight_target = (filename_target
|
highlight_target = (filename_target
|
||||||
if filename_target
|
if filename_target
|
||||||
else Path(name) / Path(user_highlight.title)) # type: Union[str, Path]
|
else (Path(_PostPathFormatter.sanitize_path(name)) /
|
||||||
|
_PostPathFormatter.sanitize_path(user_highlight.title))) # type: Union[str, Path]
|
||||||
self.context.log("Retrieving highlights \"{}\" from profile {}".format(user_highlight.title, name))
|
self.context.log("Retrieving highlights \"{}\" from profile {}".format(user_highlight.title, name))
|
||||||
self.download_highlight_cover(user_highlight, highlight_target)
|
self.download_highlight_cover(user_highlight, highlight_target)
|
||||||
totalcount = user_highlight.itemcount
|
totalcount = user_highlight.itemcount
|
||||||
@@ -704,59 +723,75 @@ class Instaloader:
|
|||||||
fast_update: bool = False,
|
fast_update: bool = False,
|
||||||
post_filter: Optional[Callable[[Post], bool]] = None,
|
post_filter: Optional[Callable[[Post], bool]] = None,
|
||||||
max_count: Optional[int] = None,
|
max_count: Optional[int] = None,
|
||||||
total_count: Optional[int] = None) -> None:
|
total_count: Optional[int] = None,
|
||||||
|
owner_profile: Optional[Profile] = None) -> None:
|
||||||
"""
|
"""
|
||||||
Download the Posts returned by given Post Iterator.
|
Download the Posts returned by given Post Iterator.
|
||||||
|
|
||||||
..versionadded:: 4.4
|
..versionadded:: 4.4
|
||||||
|
|
||||||
|
..versionchanged:: 4.5
|
||||||
|
Transparently resume an aborted operation if `posts` is a :class:`NodeIterator`.
|
||||||
|
|
||||||
:param posts: Post Iterator to loop through.
|
:param posts: Post Iterator to loop through.
|
||||||
:param target: Target name
|
:param target: Target name.
|
||||||
:param fast_update: :option:`--fast-update`
|
:param fast_update: :option:`--fast-update`.
|
||||||
:param post_filter: :option:`--post-filter`
|
:param post_filter: :option:`--post-filter`.
|
||||||
:param max_count: Maximum count of Posts to download (:option:`--count`)
|
:param max_count: Maximum count of Posts to download (:option:`--count`).
|
||||||
:param total_count: Total number of posts returned by given iterator
|
:param total_count: Total number of posts returned by given iterator.
|
||||||
|
:param owner_profile: Associated profile, if any.
|
||||||
"""
|
"""
|
||||||
for number, post in enumerate(posts):
|
displayed_count = (max_count if total_count is None or max_count is not None and max_count < total_count
|
||||||
if max_count is not None and number >= max_count:
|
else total_count)
|
||||||
break
|
with resumable_iteration(
|
||||||
if total_count is not None:
|
context=self.context,
|
||||||
self.context.log("[{0:{w}d}/{1:{w}d}] ".format(number + 1, total_count,
|
iterator=posts,
|
||||||
w=len(str(total_count))),
|
load=load_structure_from_file,
|
||||||
end="", flush=True)
|
save=save_structure_to_file,
|
||||||
else:
|
format_path=lambda magic: self.format_filename_within_target_path(
|
||||||
if max_count is not None:
|
target, owner_profile, self.resume_prefix or '', magic, 'json.xz'
|
||||||
self.context.log("[{0:{w}d}/{1:{w}d}] ".format(number + 1, max_count,
|
),
|
||||||
w=len(str(max_count))),
|
check_bbd=self.check_resume_bbd,
|
||||||
|
enabled=self.resume_prefix is not None
|
||||||
|
) as resume_info:
|
||||||
|
is_resuming, start_index = resume_info
|
||||||
|
for number, post in enumerate(posts):
|
||||||
|
if max_count is not None and number + start_index >= max_count:
|
||||||
|
break
|
||||||
|
if displayed_count is not None:
|
||||||
|
self.context.log("[{0:{w}d}/{1:{w}d}] ".format(number + start_index + 1, displayed_count,
|
||||||
|
w=len(str(displayed_count))),
|
||||||
end="", flush=True)
|
end="", flush=True)
|
||||||
else:
|
else:
|
||||||
self.context.log("[{:3d}] ".format(number + 1), end="", flush=True)
|
self.context.log("[{:3d}] ".format(number + start_index + 1), end="", flush=True)
|
||||||
if post_filter is not None:
|
if post_filter is not None:
|
||||||
try:
|
|
||||||
if not post_filter(post):
|
|
||||||
self.context.log("{} skipped".format(post))
|
|
||||||
continue
|
|
||||||
except (InstaloaderException, KeyError, TypeError) as err:
|
|
||||||
self.context.error("{} skipped. Filter evaluation failed: {}".format(post, err))
|
|
||||||
continue
|
|
||||||
with self.context.error_catcher("Download {} of {}".format(post, target)):
|
|
||||||
# The PostChangedException gets raised if the Post's id/shortcode changed while obtaining
|
|
||||||
# additional metadata. This is most likely the case if a HTTP redirect takes place while
|
|
||||||
# resolving the shortcode URL.
|
|
||||||
# The `post_changed` variable keeps the fast-update functionality alive: A Post which is
|
|
||||||
# obained after a redirect has probably already been downloaded as a previous Post of the
|
|
||||||
# same Profile.
|
|
||||||
# Observed in issue #225: https://github.com/instaloader/instaloader/issues/225
|
|
||||||
post_changed = False
|
|
||||||
while True:
|
|
||||||
try:
|
try:
|
||||||
downloaded = self.download_post(post, target=target)
|
if not post_filter(post):
|
||||||
break
|
self.context.log("{} skipped".format(post))
|
||||||
except PostChangedException:
|
continue
|
||||||
post_changed = True
|
except (InstaloaderException, KeyError, TypeError) as err:
|
||||||
|
self.context.error("{} skipped. Filter evaluation failed: {}".format(post, err))
|
||||||
continue
|
continue
|
||||||
if fast_update and not downloaded and not post_changed:
|
with self.context.error_catcher("Download {} of {}".format(post, target)):
|
||||||
break
|
# The PostChangedException gets raised if the Post's id/shortcode changed while obtaining
|
||||||
|
# additional metadata. This is most likely the case if a HTTP redirect takes place while
|
||||||
|
# resolving the shortcode URL.
|
||||||
|
# The `post_changed` variable keeps the fast-update functionality alive: A Post which is
|
||||||
|
# obained after a redirect has probably already been downloaded as a previous Post of the
|
||||||
|
# same Profile.
|
||||||
|
# Observed in issue #225: https://github.com/instaloader/instaloader/issues/225
|
||||||
|
post_changed = False
|
||||||
|
while True:
|
||||||
|
try:
|
||||||
|
downloaded = self.download_post(post, target=target)
|
||||||
|
break
|
||||||
|
except PostChangedException:
|
||||||
|
post_changed = True
|
||||||
|
continue
|
||||||
|
if fast_update and not downloaded and not post_changed:
|
||||||
|
# disengage fast_update for first post when resuming
|
||||||
|
if not is_resuming or number > 0:
|
||||||
|
break
|
||||||
|
|
||||||
@_requires_login
|
@_requires_login
|
||||||
def get_feed_posts(self) -> Iterator[Post]:
|
def get_feed_posts(self) -> Iterator[Post]:
|
||||||
@@ -816,8 +851,10 @@ class Instaloader:
|
|||||||
"""
|
"""
|
||||||
self.context.log("Retrieving saved posts...")
|
self.context.log("Retrieving saved posts...")
|
||||||
assert self.context.username is not None # safe due to @_requires_login; required by typechecker
|
assert self.context.username is not None # safe due to @_requires_login; required by typechecker
|
||||||
self.posts_download_loop(Profile.from_username(self.context, self.context.username).get_saved_posts(), ":saved",
|
node_iterator = Profile.from_username(self.context, self.context.username).get_saved_posts()
|
||||||
fast_update, post_filter, max_count=max_count)
|
self.posts_download_loop(node_iterator, ":saved",
|
||||||
|
fast_update, post_filter,
|
||||||
|
max_count=max_count, total_count=node_iterator.count)
|
||||||
|
|
||||||
@_requires_login
|
@_requires_login
|
||||||
def get_location_posts(self, location: str) -> Iterator[Post]:
|
def get_location_posts(self, location: str) -> Iterator[Post]:
|
||||||
@@ -872,18 +909,20 @@ class Instaloader:
|
|||||||
max_count=max_count)
|
max_count=max_count)
|
||||||
|
|
||||||
@_requires_login
|
@_requires_login
|
||||||
def get_explore_posts(self) -> Iterator[Post]:
|
def get_explore_posts(self) -> NodeIterator[Post]:
|
||||||
"""Get Posts which are worthy of exploring suggested by Instagram.
|
"""Get Posts which are worthy of exploring suggested by Instagram.
|
||||||
|
|
||||||
:return: Iterator over Posts of the user's suggested posts.
|
:return: Iterator over Posts of the user's suggested posts.
|
||||||
|
:rtype: NodeIterator[Post]
|
||||||
:raises LoginRequiredException: If called without being logged in.
|
:raises LoginRequiredException: If called without being logged in.
|
||||||
"""
|
"""
|
||||||
data = self.context.get_json('explore/', {})
|
return NodeIterator(
|
||||||
yield from (Post(self.context, node)
|
self.context,
|
||||||
for node in self.context.graphql_node_list("df0dcc250c2b18d9fd27c5581ef33c7c",
|
'df0dcc250c2b18d9fd27c5581ef33c7c',
|
||||||
{}, 'https://www.instagram.com/explore/',
|
lambda d: d['data']['user']['edge_web_discover_media'],
|
||||||
lambda d: d['data']['user']['edge_web_discover_media'],
|
lambda n: Post(self.context, n),
|
||||||
data.get('rhx_gis')))
|
query_referer='https://www.instagram.com/explore/',
|
||||||
|
)
|
||||||
|
|
||||||
def get_hashtag_posts(self, hashtag: str) -> Iterator[Post]:
|
def get_hashtag_posts(self, hashtag: str) -> Iterator[Post]:
|
||||||
"""Get Posts associated with a #hashtag.
|
"""Get Posts associated with a #hashtag.
|
||||||
@@ -942,8 +981,10 @@ class Instaloader:
|
|||||||
.. versionadded:: 4.1"""
|
.. versionadded:: 4.1"""
|
||||||
self.context.log("Retrieving tagged posts for profile {}.".format(profile.username))
|
self.context.log("Retrieving tagged posts for profile {}.".format(profile.username))
|
||||||
self.posts_download_loop(profile.get_tagged_posts(),
|
self.posts_download_loop(profile.get_tagged_posts(),
|
||||||
target if target else Path(profile.username) / Path(':tagged'), fast_update,
|
target if target
|
||||||
post_filter)
|
else (Path(_PostPathFormatter.sanitize_path(profile.username)) /
|
||||||
|
_PostPathFormatter.sanitize_path(':tagged')),
|
||||||
|
fast_update, post_filter)
|
||||||
|
|
||||||
def download_igtv(self, profile: Profile, fast_update: bool = False,
|
def download_igtv(self, profile: Profile, fast_update: bool = False,
|
||||||
post_filter: Optional[Callable[[Post], bool]] = None) -> None:
|
post_filter: Optional[Callable[[Post], bool]] = None) -> None:
|
||||||
@@ -952,15 +993,17 @@ class Instaloader:
|
|||||||
.. versionadded:: 4.3"""
|
.. versionadded:: 4.3"""
|
||||||
self.context.log("Retrieving IGTV videos for profile {}.".format(profile.username))
|
self.context.log("Retrieving IGTV videos for profile {}.".format(profile.username))
|
||||||
self.posts_download_loop(profile.get_igtv_posts(), profile.username, fast_update, post_filter,
|
self.posts_download_loop(profile.get_igtv_posts(), profile.username, fast_update, post_filter,
|
||||||
total_count=profile.igtvcount)
|
total_count=profile.igtvcount, owner_profile=profile)
|
||||||
|
|
||||||
def _get_id_filename(self, profile_name: str) -> str:
|
def _get_id_filename(self, profile_name: str) -> str:
|
||||||
if ((format_string_contains_key(self.dirname_pattern, 'profile') or
|
if ((format_string_contains_key(self.dirname_pattern, 'profile') or
|
||||||
format_string_contains_key(self.dirname_pattern, 'target'))):
|
format_string_contains_key(self.dirname_pattern, 'target'))):
|
||||||
return '{0}/id'.format(self.dirname_pattern.format(profile=profile_name.lower(),
|
return os.path.join(self.dirname_pattern.format(profile=profile_name.lower(),
|
||||||
target=profile_name.lower()))
|
target=profile_name.lower()),
|
||||||
|
'id')
|
||||||
else:
|
else:
|
||||||
return '{0}/{1}_id'.format(self.dirname_pattern.format(), profile_name.lower())
|
return os.path.join(self.dirname_pattern.format(),
|
||||||
|
'{0}_id'.format(profile_name.lower()))
|
||||||
|
|
||||||
def save_profile_id(self, profile: Profile):
|
def save_profile_id(self, profile: Profile):
|
||||||
"""
|
"""
|
||||||
@@ -1074,9 +1117,9 @@ class Instaloader:
|
|||||||
|
|
||||||
# Save metadata as JSON if desired.
|
# Save metadata as JSON if desired.
|
||||||
if self.save_metadata:
|
if self.save_metadata:
|
||||||
json_filename = '{0}/{1}_{2}'.format(self.dirname_pattern.format(profile=profile_name,
|
json_filename = os.path.join(self.dirname_pattern.format(profile=profile_name,
|
||||||
target=profile_name),
|
target=profile_name),
|
||||||
profile_name, profile.userid)
|
'{0}_{1}'.format(profile_name, profile.userid))
|
||||||
self.save_metadata_json(json_filename, profile)
|
self.save_metadata_json(json_filename, profile)
|
||||||
|
|
||||||
# Catch some errors
|
# Catch some errors
|
||||||
@@ -1105,7 +1148,7 @@ class Instaloader:
|
|||||||
if posts:
|
if posts:
|
||||||
self.context.log("Retrieving posts from profile {}.".format(profile_name))
|
self.context.log("Retrieving posts from profile {}.".format(profile_name))
|
||||||
self.posts_download_loop(profile.get_posts(), profile_name, fast_update, post_filter,
|
self.posts_download_loop(profile.get_posts(), profile_name, fast_update, post_filter,
|
||||||
total_count=profile.mediacount)
|
total_count=profile.mediacount, owner_profile=profile)
|
||||||
|
|
||||||
if stories and profiles:
|
if stories and profiles:
|
||||||
with self.context.error_catcher("Download stories"):
|
with self.context.error_catcher("Download stories"):
|
||||||
@@ -1185,26 +1228,7 @@ class Instaloader:
|
|||||||
# Iterate over pictures and download them
|
# Iterate over pictures and download them
|
||||||
self.context.log("Retrieving posts from profile {}.".format(profile_name))
|
self.context.log("Retrieving posts from profile {}.".format(profile_name))
|
||||||
self.posts_download_loop(profile.get_posts(), profile_name, fast_update, post_filter,
|
self.posts_download_loop(profile.get_posts(), profile_name, fast_update, post_filter,
|
||||||
total_count=profile.mediacount)
|
total_count=profile.mediacount, owner_profile=profile)
|
||||||
|
|
||||||
def check_if_committed(self, filename: str) -> bool:
|
|
||||||
"""Checks to see if the current post has been committed.
|
|
||||||
|
|
||||||
A post is considered committed if its json metadata file exists and is not malformed.
|
|
||||||
|
|
||||||
.. versionadded:: 4.2
|
|
||||||
"""
|
|
||||||
if os.path.isfile(filename + '.json.xz'):
|
|
||||||
filename += '.json.xz'
|
|
||||||
elif os.path.isfile(filename + '.json'):
|
|
||||||
filename += '.json'
|
|
||||||
else:
|
|
||||||
return False
|
|
||||||
try:
|
|
||||||
load_structure_from_file(self.context, filename)
|
|
||||||
return True
|
|
||||||
except (FileNotFoundError, lzma.LZMAError, json.decoder.JSONDecodeError):
|
|
||||||
return False
|
|
||||||
|
|
||||||
def interactive_login(self, username: str) -> None:
|
def interactive_login(self, username: str) -> None:
|
||||||
"""Logs in and internally stores session, asking user for password interactively.
|
"""Logs in and internally stores session, asking user for password interactively.
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
import hashlib
|
import hashlib
|
||||||
import json
|
import json
|
||||||
|
import os
|
||||||
import pickle
|
import pickle
|
||||||
import random
|
import random
|
||||||
import re
|
import re
|
||||||
@@ -52,7 +53,8 @@ class InstaloaderContext:
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, sleep: bool = True, quiet: bool = False, user_agent: Optional[str] = None,
|
def __init__(self, sleep: bool = True, quiet: bool = False, user_agent: Optional[str] = None,
|
||||||
max_connection_attempts: int = 3, request_timeout: Optional[float] = None):
|
max_connection_attempts: int = 3, request_timeout: Optional[float] = None,
|
||||||
|
rate_controller: Optional[Callable[["InstaloaderContext"], "RateController"]] = None):
|
||||||
|
|
||||||
self.user_agent = user_agent if user_agent is not None else default_user_agent()
|
self.user_agent = user_agent if user_agent is not None else default_user_agent()
|
||||||
self.request_timeout = request_timeout
|
self.request_timeout = request_timeout
|
||||||
@@ -68,9 +70,7 @@ class InstaloaderContext:
|
|||||||
# error log, filled with error() and printed at the end of Instaloader.main()
|
# error log, filled with error() and printed at the end of Instaloader.main()
|
||||||
self.error_log = [] # type: List[str]
|
self.error_log = [] # type: List[str]
|
||||||
|
|
||||||
# For the adaption of sleep intervals (rate control)
|
self._rate_controller = rate_controller(self) if rate_controller is not None else RateController(self)
|
||||||
self._graphql_query_timestamps = dict() # type: Dict[str, List[float]]
|
|
||||||
self._graphql_earliest_next_request_time = 0.0
|
|
||||||
|
|
||||||
# Can be set to True for testing, disables supression of InstaloaderContext._error_catcher
|
# Can be set to True for testing, disables supression of InstaloaderContext._error_catcher
|
||||||
self.raise_all_errors = False
|
self.raise_all_errors = False
|
||||||
@@ -113,7 +113,7 @@ class InstaloaderContext:
|
|||||||
def close(self):
|
def close(self):
|
||||||
"""Print error log and close session"""
|
"""Print error log and close session"""
|
||||||
if self.error_log and not self.quiet:
|
if self.error_log and not self.quiet:
|
||||||
print("\nErrors occurred:", file=sys.stderr)
|
print("\nErrors or warnings occurred:", file=sys.stderr)
|
||||||
for err in self.error_log:
|
for err in self.error_log:
|
||||||
print(err, file=sys.stderr)
|
print(err, file=sys.stderr)
|
||||||
self._session.close()
|
self._session.close()
|
||||||
@@ -215,8 +215,11 @@ class InstaloaderContext:
|
|||||||
session.headers.update({'X-CSRFToken': csrf_token})
|
session.headers.update({'X-CSRFToken': csrf_token})
|
||||||
# Not using self.get_json() here, because we need to access csrftoken cookie
|
# Not using self.get_json() here, because we need to access csrftoken cookie
|
||||||
self.do_sleep()
|
self.do_sleep()
|
||||||
|
# Workaround credits to pgrimaud.
|
||||||
|
# See: https://github.com/pgrimaud/instagram-user-feed/commit/96ad4cf54d1ad331b337f325c73e664999a6d066
|
||||||
|
enc_password = '#PWD_INSTAGRAM_BROWSER:0:{}:{}'.format(int(datetime.now().timestamp()), passwd)
|
||||||
login = session.post('https://www.instagram.com/accounts/login/ajax/',
|
login = session.post('https://www.instagram.com/accounts/login/ajax/',
|
||||||
data={'password': passwd, 'username': user}, allow_redirects=True)
|
data={'enc_password': enc_password, 'username': user}, allow_redirects=True)
|
||||||
try:
|
try:
|
||||||
resp_json = login.json()
|
resp_json = login.json()
|
||||||
except json.decoder.JSONDecodeError:
|
except json.decoder.JSONDecodeError:
|
||||||
@@ -291,77 +294,6 @@ class InstaloaderContext:
|
|||||||
if self.sleep:
|
if self.sleep:
|
||||||
time.sleep(min(random.expovariate(0.7), 5.0))
|
time.sleep(min(random.expovariate(0.7), 5.0))
|
||||||
|
|
||||||
def _dump_query_timestamps(self, current_time: float):
|
|
||||||
"""Output the number of GraphQL queries grouped by their query_hash within the last time."""
|
|
||||||
windows = [10, 11, 15, 20, 30, 60]
|
|
||||||
print("GraphQL requests:", file=sys.stderr)
|
|
||||||
for query_hash, times in self._graphql_query_timestamps.items():
|
|
||||||
print(" {}".format(query_hash), file=sys.stderr)
|
|
||||||
for window in windows:
|
|
||||||
reqs_in_sliding_window = sum(t > current_time - window * 60 for t in times)
|
|
||||||
print(" last {} minutes: {} requests".format(window, reqs_in_sliding_window), file=sys.stderr)
|
|
||||||
|
|
||||||
def _graphql_request_count_per_sliding_window(self, query_hash: str) -> int:
|
|
||||||
"""Return how many GraphQL requests can be done within the sliding window."""
|
|
||||||
if self.is_logged_in:
|
|
||||||
max_reqs = {'1cb6ec562846122743b61e492c85999f': 20,
|
|
||||||
'33ba35852cb50da46f5b5e889df7d159': 20,
|
|
||||||
'iphone': 100,
|
|
||||||
'other': 100}
|
|
||||||
else:
|
|
||||||
max_reqs = {'1cb6ec562846122743b61e492c85999f': 200,
|
|
||||||
'33ba35852cb50da46f5b5e889df7d159': 200,
|
|
||||||
'other': 200}
|
|
||||||
return max_reqs.get(query_hash) or min(max_reqs.values())
|
|
||||||
|
|
||||||
def _graphql_query_waittime(self, query_hash: str, current_time: float, untracked_queries: bool = False) -> float:
|
|
||||||
"""Calculate time needed to wait before GraphQL query can be executed."""
|
|
||||||
sliding_window = 660
|
|
||||||
if query_hash not in self._graphql_query_timestamps:
|
|
||||||
self._graphql_query_timestamps[query_hash] = []
|
|
||||||
self._graphql_query_timestamps[query_hash] = list(filter(lambda t: t > current_time - 60 * 60,
|
|
||||||
self._graphql_query_timestamps[query_hash]))
|
|
||||||
reqs_in_sliding_window = list(filter(lambda t: t > current_time - sliding_window,
|
|
||||||
self._graphql_query_timestamps[query_hash]))
|
|
||||||
count_per_sliding_window = self._graphql_request_count_per_sliding_window(query_hash)
|
|
||||||
if len(reqs_in_sliding_window) < count_per_sliding_window and not untracked_queries:
|
|
||||||
return max(0, self._graphql_earliest_next_request_time - current_time)
|
|
||||||
next_request_time = min(reqs_in_sliding_window) + sliding_window + 6
|
|
||||||
if untracked_queries:
|
|
||||||
self._graphql_earliest_next_request_time = next_request_time
|
|
||||||
return round(max(next_request_time, self._graphql_earliest_next_request_time) - current_time)
|
|
||||||
|
|
||||||
def _ratecontrol_graphql_query(self, query_hash: str, untracked_queries: bool = False):
|
|
||||||
"""Called before a GraphQL query is made in order to stay within Instagram's rate limits.
|
|
||||||
|
|
||||||
:param query_hash: The query_hash parameter of the query.
|
|
||||||
:param untracked_queries: True, if 429 has been returned to apply 429 logic.
|
|
||||||
"""
|
|
||||||
if not untracked_queries:
|
|
||||||
waittime = self._graphql_query_waittime(query_hash, time.monotonic(), untracked_queries)
|
|
||||||
assert waittime >= 0
|
|
||||||
if waittime > 10:
|
|
||||||
self.log('\nToo many queries in the last time. Need to wait {} seconds, until {:%H:%M}.'
|
|
||||||
.format(waittime, datetime.now() + timedelta(seconds=waittime)))
|
|
||||||
time.sleep(waittime)
|
|
||||||
if query_hash not in self._graphql_query_timestamps:
|
|
||||||
self._graphql_query_timestamps[query_hash] = [time.monotonic()]
|
|
||||||
else:
|
|
||||||
self._graphql_query_timestamps[query_hash].append(time.monotonic())
|
|
||||||
else:
|
|
||||||
text_for_429 = ("HTTP error code 429 was returned because too many queries occurred in the last time. "
|
|
||||||
"Please do not use Instagram in your browser or run multiple instances of Instaloader "
|
|
||||||
"in parallel.")
|
|
||||||
print(textwrap.fill(text_for_429), file=sys.stderr)
|
|
||||||
current_time = time.monotonic()
|
|
||||||
waittime = self._graphql_query_waittime(query_hash, current_time, untracked_queries)
|
|
||||||
assert waittime >= 0
|
|
||||||
if waittime > 10:
|
|
||||||
self.log('The request will be retried in {} seconds, at {:%H:%M}.'
|
|
||||||
.format(waittime, datetime.now() + timedelta(seconds=waittime)))
|
|
||||||
self._dump_query_timestamps(current_time)
|
|
||||||
time.sleep(waittime)
|
|
||||||
|
|
||||||
def get_json(self, path: str, params: Dict[str, Any], host: str = 'www.instagram.com',
|
def get_json(self, path: str, params: Dict[str, Any], host: str = 'www.instagram.com',
|
||||||
session: Optional[requests.Session] = None, _attempt=1) -> Dict[str, Any]:
|
session: Optional[requests.Session] = None, _attempt=1) -> Dict[str, Any]:
|
||||||
"""JSON request to Instagram.
|
"""JSON request to Instagram.
|
||||||
@@ -382,11 +314,11 @@ class InstaloaderContext:
|
|||||||
try:
|
try:
|
||||||
self.do_sleep()
|
self.do_sleep()
|
||||||
if is_graphql_query:
|
if is_graphql_query:
|
||||||
self._ratecontrol_graphql_query(params['query_hash'])
|
self._rate_controller.wait_before_query(params['query_hash'])
|
||||||
if is_iphone_query:
|
if is_iphone_query:
|
||||||
self._ratecontrol_graphql_query('iphone')
|
self._rate_controller.wait_before_query('iphone')
|
||||||
if is_other_query:
|
if is_other_query:
|
||||||
self._ratecontrol_graphql_query('other')
|
self._rate_controller.wait_before_query('other')
|
||||||
resp = sess.get('https://{0}/{1}'.format(host, path), params=params, allow_redirects=False)
|
resp = sess.get('https://{0}/{1}'.format(host, path), params=params, allow_redirects=False)
|
||||||
while resp.is_redirect:
|
while resp.is_redirect:
|
||||||
redirect_url = resp.headers['location']
|
redirect_url = resp.headers['location']
|
||||||
@@ -442,12 +374,13 @@ class InstaloaderContext:
|
|||||||
raise ConnectionException(error_string) from err
|
raise ConnectionException(error_string) from err
|
||||||
self.error(error_string + " [retrying; skip with ^C]", repeat_at_end=False)
|
self.error(error_string + " [retrying; skip with ^C]", repeat_at_end=False)
|
||||||
try:
|
try:
|
||||||
if is_graphql_query and isinstance(err, TooManyRequestsException):
|
if isinstance(err, TooManyRequestsException):
|
||||||
self._ratecontrol_graphql_query(params['query_hash'], untracked_queries=True)
|
if is_graphql_query:
|
||||||
if is_iphone_query and isinstance(err, TooManyRequestsException):
|
self._rate_controller.handle_429(params['query_hash'])
|
||||||
self._ratecontrol_graphql_query('iphone', untracked_queries=True)
|
if is_iphone_query:
|
||||||
if is_other_query and isinstance(err, TooManyRequestsException):
|
self._rate_controller.handle_429('iphone')
|
||||||
self._ratecontrol_graphql_query('other', untracked_queries=True)
|
if is_other_query:
|
||||||
|
self._rate_controller.handle_429('other')
|
||||||
return self.get_json(path=path, params=params, host=host, session=sess, _attempt=_attempt + 1)
|
return self.get_json(path=path, params=params, host=host, session=sess, _attempt=_attempt + 1)
|
||||||
except KeyboardInterrupt:
|
except KeyboardInterrupt:
|
||||||
self.error("[skipped by user]", repeat_at_end=False)
|
self.error("[skipped by user]", repeat_at_end=False)
|
||||||
@@ -495,7 +428,12 @@ class InstaloaderContext:
|
|||||||
edge_extractor: Callable[[Dict[str, Any]], Dict[str, Any]],
|
edge_extractor: Callable[[Dict[str, Any]], Dict[str, Any]],
|
||||||
rhx_gis: Optional[str] = None,
|
rhx_gis: Optional[str] = None,
|
||||||
first_data: Optional[Dict[str, Any]] = None) -> Iterator[Dict[str, Any]]:
|
first_data: Optional[Dict[str, Any]] = None) -> Iterator[Dict[str, Any]]:
|
||||||
"""Retrieve a list of GraphQL nodes."""
|
"""
|
||||||
|
Retrieve a list of GraphQL nodes.
|
||||||
|
|
||||||
|
..deprecated:: 4.5
|
||||||
|
Use :class:`NodeIterator` instead, which provides more functionality.
|
||||||
|
"""
|
||||||
|
|
||||||
def _query():
|
def _query():
|
||||||
query_variables['first'] = self._graphql_page_length
|
query_variables['first'] = self._graphql_page_length
|
||||||
@@ -533,8 +471,8 @@ class InstaloaderContext:
|
|||||||
|
|
||||||
.. versionadded:: 4.2.1"""
|
.. versionadded:: 4.2.1"""
|
||||||
with copy_session(self._session, self.request_timeout) as tempsession:
|
with copy_session(self._session, self.request_timeout) as tempsession:
|
||||||
tempsession.headers['User-Agent'] = 'Instagram 10.3.2 (iPhone7,2; iPhone OS 9_3_3; en_US; en-US; ' \
|
tempsession.headers['User-Agent'] = 'Instagram 123.1.0.26.115 (iPhone12,1; iOS 13_3; en_US; en-US; ' \
|
||||||
'scale=2.00; 750x1334) AppleWebKit/420+'
|
'scale=2.00; 1656x3584; 190542906)'
|
||||||
for header in ['Host', 'Origin', 'X-Instagram-AJAX', 'X-Requested-With']:
|
for header in ['Host', 'Origin', 'X-Instagram-AJAX', 'X-Requested-With']:
|
||||||
tempsession.headers.pop(header, None)
|
tempsession.headers.pop(header, None)
|
||||||
return self.get_json(path, params, 'i.instagram.com', tempsession)
|
return self.get_json(path, params, 'i.instagram.com', tempsession)
|
||||||
@@ -544,11 +482,12 @@ class InstaloaderContext:
|
|||||||
|
|
||||||
.. versionadded:: 4.2.1"""
|
.. versionadded:: 4.2.1"""
|
||||||
self.log(filename, end=' ', flush=True)
|
self.log(filename, end=' ', flush=True)
|
||||||
with open(filename, 'wb') as file:
|
with open(filename + '.temp', 'wb') as file:
|
||||||
if isinstance(resp, requests.Response):
|
if isinstance(resp, requests.Response):
|
||||||
shutil.copyfileobj(resp.raw, file)
|
shutil.copyfileobj(resp.raw, file)
|
||||||
else:
|
else:
|
||||||
file.write(resp)
|
file.write(resp)
|
||||||
|
os.rename(filename + '.temp', filename)
|
||||||
|
|
||||||
def get_raw(self, url: str, _attempt=1) -> requests.Response:
|
def get_raw(self, url: str, _attempt=1) -> requests.Response:
|
||||||
"""Downloads a file anonymously.
|
"""Downloads a file anonymously.
|
||||||
@@ -590,3 +529,97 @@ class InstaloaderContext:
|
|||||||
if self._root_rhx_gis is None:
|
if self._root_rhx_gis is None:
|
||||||
self._root_rhx_gis = self.get_json('', {}).get('rhx_gis', '')
|
self._root_rhx_gis = self.get_json('', {}).get('rhx_gis', '')
|
||||||
return self._root_rhx_gis or None
|
return self._root_rhx_gis or None
|
||||||
|
|
||||||
|
|
||||||
|
class RateController:
|
||||||
|
"""
|
||||||
|
Class providing request tracking and rate controlling to stay within rate limits.
|
||||||
|
|
||||||
|
It can be overridden to change Instaloader's behavior regarding rate limits, for example to raise a custom
|
||||||
|
exception when the rate limit is hit::
|
||||||
|
|
||||||
|
import instaloader
|
||||||
|
|
||||||
|
class MyRateController(instaloader.RateController):
|
||||||
|
def sleep(self, secs):
|
||||||
|
raise MyCustomException()
|
||||||
|
|
||||||
|
L = instaloader.Instaloader(rate_controller=lambda ctx: MyRateController(ctx))
|
||||||
|
"""
|
||||||
|
|
||||||
|
def __init__(self, context: InstaloaderContext):
|
||||||
|
self._context = context
|
||||||
|
self._graphql_query_timestamps = dict() # type: Dict[str, List[float]]
|
||||||
|
self._graphql_earliest_next_request_time = 0.0
|
||||||
|
|
||||||
|
def sleep(self, secs: float):
|
||||||
|
"""Wait given number of seconds."""
|
||||||
|
# Not static, to allow for the behavior of this method to depend on context-inherent properties, such as
|
||||||
|
# whether we are logged in.
|
||||||
|
# pylint:disable=no-self-use
|
||||||
|
time.sleep(secs)
|
||||||
|
|
||||||
|
def _dump_query_timestamps(self, current_time: float):
|
||||||
|
windows = [10, 11, 15, 20, 30, 60]
|
||||||
|
print("GraphQL requests:", file=sys.stderr)
|
||||||
|
for query_hash, times in self._graphql_query_timestamps.items():
|
||||||
|
print(" {}".format(query_hash), file=sys.stderr)
|
||||||
|
for window in windows:
|
||||||
|
reqs_in_sliding_window = sum(t > current_time - window * 60 for t in times)
|
||||||
|
print(" last {} minutes: {} requests".format(window, reqs_in_sliding_window), file=sys.stderr)
|
||||||
|
|
||||||
|
def count_per_sliding_window(self, query_type: str) -> int:
|
||||||
|
"""Return how many GraphQL requests can be done within the sliding window."""
|
||||||
|
# Not static, to allow for the count_per_sliding_window to depend on context-inherent properties, such as
|
||||||
|
# whether we are logged in.
|
||||||
|
# pylint:disable=no-self-use,unused-argument
|
||||||
|
return 200
|
||||||
|
|
||||||
|
def query_waittime(self, query_type: str, current_time: float, untracked_queries: bool = False) -> float:
|
||||||
|
"""Calculate time needed to wait before GraphQL query can be executed."""
|
||||||
|
sliding_window = 660
|
||||||
|
if query_type not in self._graphql_query_timestamps:
|
||||||
|
self._graphql_query_timestamps[query_type] = []
|
||||||
|
self._graphql_query_timestamps[query_type] = list(filter(lambda t: t > current_time - 60 * 60,
|
||||||
|
self._graphql_query_timestamps[query_type]))
|
||||||
|
reqs_in_sliding_window = list(filter(lambda t: t > current_time - sliding_window,
|
||||||
|
self._graphql_query_timestamps[query_type]))
|
||||||
|
count_per_sliding_window = self.count_per_sliding_window(query_type)
|
||||||
|
if len(reqs_in_sliding_window) < count_per_sliding_window and not untracked_queries:
|
||||||
|
return max(0.0, self._graphql_earliest_next_request_time - current_time)
|
||||||
|
next_request_time = min(reqs_in_sliding_window) + sliding_window + 6
|
||||||
|
if untracked_queries:
|
||||||
|
self._graphql_earliest_next_request_time = next_request_time
|
||||||
|
return max(next_request_time, self._graphql_earliest_next_request_time) - current_time
|
||||||
|
|
||||||
|
def wait_before_query(self, query_type: str) -> None:
|
||||||
|
"""This method is called before a query to Instagram. It calls :meth:`RateController.sleep` to wait
|
||||||
|
until the request can be made."""
|
||||||
|
waittime = self.query_waittime(query_type, time.monotonic(), False)
|
||||||
|
assert waittime >= 0
|
||||||
|
if waittime > 15:
|
||||||
|
self._context.log("\nToo many queries in the last time. Need to wait {} seconds, until {:%H:%M}."
|
||||||
|
.format(round(waittime), datetime.now() + timedelta(seconds=waittime)))
|
||||||
|
if waittime > 0:
|
||||||
|
self.sleep(waittime)
|
||||||
|
if query_type not in self._graphql_query_timestamps:
|
||||||
|
self._graphql_query_timestamps[query_type] = [time.monotonic()]
|
||||||
|
else:
|
||||||
|
self._graphql_query_timestamps[query_type].append(time.monotonic())
|
||||||
|
|
||||||
|
def handle_429(self, query_type: str) -> None:
|
||||||
|
"""This method is called to handle a 429 Too Many Requests response. It calls :meth:`RateController.sleep` to
|
||||||
|
wait until we can repeat the same request."""
|
||||||
|
text_for_429 = ("HTTP error code 429 was returned because too many queries occurred in the last time. "
|
||||||
|
"Please do not use Instagram in your browser or run multiple instances of Instaloader "
|
||||||
|
"in parallel.")
|
||||||
|
print(textwrap.fill(text_for_429), file=sys.stderr)
|
||||||
|
current_time = time.monotonic()
|
||||||
|
waittime = self.query_waittime(query_type, current_time, True)
|
||||||
|
assert waittime >= 0
|
||||||
|
if waittime > 15:
|
||||||
|
self._context.log("The request will be retried in {} seconds, at {:%H:%M}."
|
||||||
|
.format(round(waittime), datetime.now() + timedelta(seconds=waittime)))
|
||||||
|
self._dump_query_timestamps(current_time)
|
||||||
|
if waittime > 0:
|
||||||
|
self.sleep(waittime)
|
||||||
|
|||||||
@@ -0,0 +1,261 @@
|
|||||||
|
import base64
|
||||||
|
import hashlib
|
||||||
|
import json
|
||||||
|
import os
|
||||||
|
from contextlib import contextmanager
|
||||||
|
from datetime import datetime, timedelta
|
||||||
|
from lzma import LZMAError
|
||||||
|
from typing import Any, Callable, Dict, Iterator, NamedTuple, Optional, Tuple, TypeVar
|
||||||
|
|
||||||
|
from .exceptions import InvalidArgumentException, QueryReturnedBadRequestException
|
||||||
|
from .instaloadercontext import InstaloaderContext
|
||||||
|
|
||||||
|
FrozenNodeIterator = NamedTuple('FrozenNodeIterator',
|
||||||
|
[('query_hash', str),
|
||||||
|
('query_variables', Dict),
|
||||||
|
('query_referer', Optional[str]),
|
||||||
|
('context_username', Optional[str]),
|
||||||
|
('total_index', int),
|
||||||
|
('best_before', Optional[float]),
|
||||||
|
('remaining_data', Optional[Dict])])
|
||||||
|
FrozenNodeIterator.__doc__ = \
|
||||||
|
"""A serializable representation of a :class:`NodeIterator` instance, saving its iteration state."""
|
||||||
|
FrozenNodeIterator.query_hash.__doc__ = """The GraphQL ``query_hash`` parameter."""
|
||||||
|
FrozenNodeIterator.query_variables.__doc__ = """The GraphQL ``query_variables`` parameter."""
|
||||||
|
FrozenNodeIterator.query_referer.__doc__ = """The HTTP referer used for the GraphQL query."""
|
||||||
|
FrozenNodeIterator.context_username.__doc__ = """The username who created the iterator, or ``None``."""
|
||||||
|
FrozenNodeIterator.total_index.__doc__ = """Number of items that have already been returned."""
|
||||||
|
FrozenNodeIterator.best_before.__doc__ = """Date when parts of the stored nodes might have expired."""
|
||||||
|
FrozenNodeIterator.remaining_data.__doc__ = \
|
||||||
|
"""The already-retrieved, yet-unprocessed ``edges`` and the ``page_info`` at time of freezing."""
|
||||||
|
|
||||||
|
|
||||||
|
T = TypeVar('T')
|
||||||
|
|
||||||
|
|
||||||
|
class NodeIterator(Iterator[T]):
|
||||||
|
"""
|
||||||
|
Iterate the nodes within edges in a GraphQL pagination. Instances of this class are returned by many (but not all)
|
||||||
|
of Instaloader's :class:`Post`-returning functions (such as :meth:`Profile.get_posts` etc.).
|
||||||
|
|
||||||
|
What makes this iterator special is its ability to freeze/store its current state, e.g. to interrupt an iteration,
|
||||||
|
and later thaw/resume from where it left off.
|
||||||
|
|
||||||
|
You can freeze a NodeIterator with :meth:`NodeIterator.freeze`::
|
||||||
|
|
||||||
|
post_iterator = profile.get_posts()
|
||||||
|
try:
|
||||||
|
for post in post_iterator:
|
||||||
|
do_something_with(post)
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
save("resume_information.json", post_iterator.freeze())
|
||||||
|
|
||||||
|
and later reuse it with :meth:`NodeIterator.thaw` on an equally-constructed NodeIterator::
|
||||||
|
|
||||||
|
post_iterator = profile.get_posts()
|
||||||
|
post_iterator.thaw(load("resume_information.json"))
|
||||||
|
|
||||||
|
A :class:`FrozenNodeIterator` can only be thawn with a matching NodeIterator, i.e. a NodeIterator instance that has
|
||||||
|
been constructed with the same parameters as the instance that is represented by the :class:`FrozenNodeIterator` in
|
||||||
|
question. This is to ensure that an iteration cannot be resumed in a wrong, unmatching loop. As a quick way to
|
||||||
|
distinguish iterators that are saved e.g. in files, there is the :attr:`NodeIterator.magic` string: Two
|
||||||
|
NodeIterators are matching if they have the same magic.
|
||||||
|
|
||||||
|
See also :func:`resumable_iteration` for a high-level context manager that handles a resumable iteration.
|
||||||
|
"""
|
||||||
|
|
||||||
|
_graphql_page_length = 50
|
||||||
|
shelf_life = timedelta(days=29)
|
||||||
|
|
||||||
|
def __init__(self,
|
||||||
|
context: InstaloaderContext,
|
||||||
|
query_hash: str,
|
||||||
|
edge_extractor: Callable[[Dict[str, Any]], Dict[str, Any]],
|
||||||
|
node_wrapper: Callable[[Dict], T],
|
||||||
|
query_variables: Optional[Dict[str, Any]] = None,
|
||||||
|
query_referer: Optional[str] = None,
|
||||||
|
first_data: Optional[Dict[str, Any]] = None):
|
||||||
|
self._context = context
|
||||||
|
self._query_hash = query_hash
|
||||||
|
self._edge_extractor = edge_extractor
|
||||||
|
self._node_wrapper = node_wrapper
|
||||||
|
self._query_variables = query_variables if query_variables is not None else {}
|
||||||
|
self._query_referer = query_referer
|
||||||
|
self._data = first_data
|
||||||
|
self._page_index = 0
|
||||||
|
self._total_index = 0
|
||||||
|
self._best_before = (None if first_data is None else
|
||||||
|
datetime.now() + NodeIterator.shelf_life)
|
||||||
|
|
||||||
|
def _query(self, after: Optional[str] = None) -> Dict:
|
||||||
|
pagination_variables = {'first': NodeIterator._graphql_page_length} # type: Dict[str, Any]
|
||||||
|
if after is not None:
|
||||||
|
pagination_variables['after'] = after
|
||||||
|
try:
|
||||||
|
data = self._edge_extractor(
|
||||||
|
self._context.graphql_query(
|
||||||
|
self._query_hash, {**self._query_variables, **pagination_variables}, self._query_referer
|
||||||
|
)
|
||||||
|
)
|
||||||
|
self._best_before = datetime.now() + NodeIterator.shelf_life
|
||||||
|
return data
|
||||||
|
except QueryReturnedBadRequestException:
|
||||||
|
new_page_length = int(NodeIterator._graphql_page_length / 2)
|
||||||
|
if new_page_length >= 12:
|
||||||
|
NodeIterator._graphql_page_length = new_page_length
|
||||||
|
self._context.error("HTTP Error 400 (Bad Request) on GraphQL Query. Retrying with shorter page length.",
|
||||||
|
repeat_at_end=False)
|
||||||
|
return self._query(after)
|
||||||
|
else:
|
||||||
|
raise
|
||||||
|
|
||||||
|
def __iter__(self):
|
||||||
|
return self
|
||||||
|
|
||||||
|
def __next__(self):
|
||||||
|
if self._data is None:
|
||||||
|
self._data = self._query()
|
||||||
|
if self._page_index < len(self._data['edges']):
|
||||||
|
node = self._data['edges'][self._page_index]['node']
|
||||||
|
page_index, total_index = self._page_index, self._total_index
|
||||||
|
try:
|
||||||
|
self._page_index += 1
|
||||||
|
self._total_index += 1
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
self._page_index, self._total_index = page_index, total_index
|
||||||
|
raise
|
||||||
|
return self._node_wrapper(node)
|
||||||
|
if self._data['page_info']['has_next_page']:
|
||||||
|
query_response = self._query(self._data['page_info']['end_cursor'])
|
||||||
|
page_index, data = self._page_index, self._data
|
||||||
|
try:
|
||||||
|
self._page_index = 0
|
||||||
|
self._data = query_response
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
self._page_index, self._data = page_index, data
|
||||||
|
raise
|
||||||
|
return self.__next__()
|
||||||
|
raise StopIteration()
|
||||||
|
|
||||||
|
@property
|
||||||
|
def count(self) -> Optional[int]:
|
||||||
|
"""The ``count`` as returned by Instagram. This is not always the total count this iterator will yield."""
|
||||||
|
return self._data.get('count') if self._data is not None else None
|
||||||
|
|
||||||
|
@property
|
||||||
|
def total_index(self) -> int:
|
||||||
|
"""Number of items that have already been returned."""
|
||||||
|
return self._total_index
|
||||||
|
|
||||||
|
@property
|
||||||
|
def magic(self) -> str:
|
||||||
|
"""Magic string for easily identifying a matching iterator file for resuming (hash of some parameters)."""
|
||||||
|
if 'blake2b' not in hashlib.algorithms_available:
|
||||||
|
magic_hash = hashlib.new('sha224')
|
||||||
|
else:
|
||||||
|
# Use blake2b when possible, i.e. on Python >= 3.6.
|
||||||
|
magic_hash = hashlib.blake2b(digest_size=6) # type:ignore # pylint: disable=no-member
|
||||||
|
magic_hash.update(json.dumps(
|
||||||
|
[self._query_hash, self._query_variables, self._query_referer, self._context.username]
|
||||||
|
).encode())
|
||||||
|
return base64.urlsafe_b64encode(magic_hash.digest()).decode()
|
||||||
|
|
||||||
|
def freeze(self) -> FrozenNodeIterator:
|
||||||
|
"""Freeze the iterator for later resuming."""
|
||||||
|
remaining_data = None
|
||||||
|
if self._data is not None:
|
||||||
|
remaining_data = {**self._data,
|
||||||
|
'edges': (self._data['edges'][(max(self._page_index - 1, 0)):])}
|
||||||
|
return FrozenNodeIterator(
|
||||||
|
query_hash=self._query_hash,
|
||||||
|
query_variables=self._query_variables,
|
||||||
|
query_referer=self._query_referer,
|
||||||
|
context_username=self._context.username,
|
||||||
|
total_index=max(self.total_index - 1, 0),
|
||||||
|
best_before=self._best_before.timestamp() if self._best_before else None,
|
||||||
|
remaining_data=remaining_data,
|
||||||
|
)
|
||||||
|
|
||||||
|
def thaw(self, frozen: FrozenNodeIterator) -> None:
|
||||||
|
"""Use this iterator for resuming from earlier iteration."""
|
||||||
|
if self._total_index or self._page_index:
|
||||||
|
raise InvalidArgumentException("thaw() called on already-used iterator.")
|
||||||
|
if (self._query_hash != frozen.query_hash or
|
||||||
|
self._query_variables != frozen.query_variables or
|
||||||
|
self._query_referer != frozen.query_referer or
|
||||||
|
self._context.username != frozen.context_username):
|
||||||
|
raise InvalidArgumentException("Mismatching resume information.")
|
||||||
|
self._total_index = frozen.total_index
|
||||||
|
self._best_before = datetime.fromtimestamp(frozen.best_before) if frozen.best_before else None
|
||||||
|
self._data = frozen.remaining_data
|
||||||
|
|
||||||
|
|
||||||
|
@contextmanager
|
||||||
|
def resumable_iteration(context: InstaloaderContext,
|
||||||
|
iterator: Iterator,
|
||||||
|
load: Callable[[InstaloaderContext, str], Any],
|
||||||
|
save: Callable[[FrozenNodeIterator, str], None],
|
||||||
|
format_path: Callable[[str], str],
|
||||||
|
check_bbd: bool = True,
|
||||||
|
enabled: bool = True) -> Iterator[Tuple[bool, int]]:
|
||||||
|
"""
|
||||||
|
High-level context manager to handle a resumable iteration that can be interrupted with a KeyboardInterrupt.
|
||||||
|
|
||||||
|
It can be used as follows to automatically load a previously-saved state into the iterator, save the iterator's
|
||||||
|
state when interrupted, and delete the resume file upon completion::
|
||||||
|
|
||||||
|
post_iterator = profile.get_posts()
|
||||||
|
with resumable_iteration(
|
||||||
|
context=L.context,
|
||||||
|
iterator=post_iterator,
|
||||||
|
load=lambda _, path: FrozenNodeIterator(**json.load(open(path))),
|
||||||
|
save=lambda fni, path: json.dump(fni._asdict(), open(path, 'w')),
|
||||||
|
format_path=lambda magic: "resume_info_{}.json".format(magic)
|
||||||
|
) as resume_info:
|
||||||
|
is_resuming, start_index = resume_info
|
||||||
|
for post in post_iterator:
|
||||||
|
do_something_with(post)
|
||||||
|
|
||||||
|
It yields a tuple (is_resuming, start_index).
|
||||||
|
|
||||||
|
When the passed iterator is not a :class:`NodeIterator`, it behaves as if ``resumable_iteration`` was not used,
|
||||||
|
just executing the inner body.
|
||||||
|
|
||||||
|
:param context: The :class:`InstaloaderContext`.
|
||||||
|
:param iterator: The fresh :class:`NodeIterator`.
|
||||||
|
:param load: Loads a FrozenNodeIterator from given path. The object is ignored if it has a different type.
|
||||||
|
:param save: Saves the given FrozenNodeIterator to the given path.
|
||||||
|
:param format_path: Returns the path to the resume file for the given magic.
|
||||||
|
:param check_bbd: Whether to check the best before date and reject an expired FrozenNodeIterator.
|
||||||
|
:param enabled: Set to False to disable all functionality and simply execute the inner body.
|
||||||
|
"""
|
||||||
|
if not enabled or not isinstance(iterator, NodeIterator):
|
||||||
|
yield False, 0
|
||||||
|
return
|
||||||
|
is_resuming = False
|
||||||
|
start_index = 0
|
||||||
|
resume_file_path = format_path(iterator.magic)
|
||||||
|
resume_file_exists = os.path.isfile(resume_file_path)
|
||||||
|
if resume_file_exists:
|
||||||
|
try:
|
||||||
|
fni = load(context, resume_file_path)
|
||||||
|
if not isinstance(fni, FrozenNodeIterator):
|
||||||
|
raise InvalidArgumentException("Invalid type.")
|
||||||
|
if check_bbd and fni.best_before and datetime.fromtimestamp(fni.best_before) < datetime.now():
|
||||||
|
raise InvalidArgumentException("\"Best before\" date exceeded.")
|
||||||
|
iterator.thaw(fni)
|
||||||
|
is_resuming = True
|
||||||
|
start_index = iterator.total_index
|
||||||
|
context.log("Resuming from {}.".format(resume_file_path))
|
||||||
|
except (InvalidArgumentException, LZMAError, json.decoder.JSONDecodeError) as exc:
|
||||||
|
context.error("Warning: Not resuming from {}: {}".format(resume_file_path, exc))
|
||||||
|
try:
|
||||||
|
yield is_resuming, start_index
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
os.makedirs(os.path.dirname(resume_file_path), exist_ok=True)
|
||||||
|
save(iterator.freeze(), resume_file_path)
|
||||||
|
context.log("\nSaved resume information to {}.".format(resume_file_path))
|
||||||
|
raise
|
||||||
|
if resume_file_exists:
|
||||||
|
os.unlink(resume_file_path)
|
||||||
|
context.log("Iteration complete, deleted resume information file {}.".format(resume_file_path))
|
||||||
+143
-95
@@ -9,7 +9,7 @@ from typing import Any, Dict, Iterator, List, Optional, Union
|
|||||||
from . import __version__
|
from . import __version__
|
||||||
from .exceptions import *
|
from .exceptions import *
|
||||||
from .instaloadercontext import InstaloaderContext
|
from .instaloadercontext import InstaloaderContext
|
||||||
|
from .nodeiterator import FrozenNodeIterator, NodeIterator
|
||||||
|
|
||||||
PostSidecarNode = namedtuple('PostSidecarNode', ['is_video', 'display_url', 'video_url'])
|
PostSidecarNode = namedtuple('PostSidecarNode', ['is_video', 'display_url', 'video_url'])
|
||||||
PostSidecarNode.__doc__ = "Item of a Sidecar Post."
|
PostSidecarNode.__doc__ = "Item of a Sidecar Post."
|
||||||
@@ -69,8 +69,11 @@ class Post:
|
|||||||
self._node = node
|
self._node = node
|
||||||
self._owner_profile = owner_profile
|
self._owner_profile = owner_profile
|
||||||
self._full_metadata_dict = None # type: Optional[Dict[str, Any]]
|
self._full_metadata_dict = None # type: Optional[Dict[str, Any]]
|
||||||
self._rhx_gis_str = None # type: Optional[str]
|
|
||||||
self._location = None # type: Optional[PostLocation]
|
self._location = None # type: Optional[PostLocation]
|
||||||
|
self._iphone_struct_ = None
|
||||||
|
if 'iphone_struct' in node:
|
||||||
|
# if loaded from JSON with load_structure_from_file()
|
||||||
|
self._iphone_struct_ = node['iphone_struct']
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def from_shortcode(cls, context: InstaloaderContext, shortcode: str):
|
def from_shortcode(cls, context: InstaloaderContext, shortcode: str):
|
||||||
@@ -111,6 +114,8 @@ class Post:
|
|||||||
node['owner'] = self.owner_profile._asdict()
|
node['owner'] = self.owner_profile._asdict()
|
||||||
if self._location:
|
if self._location:
|
||||||
node['location'] = self._location._asdict()
|
node['location'] = self._location._asdict()
|
||||||
|
if self._iphone_struct_:
|
||||||
|
node['iphone_struct'] = self._iphone_struct_
|
||||||
return node
|
return node
|
||||||
|
|
||||||
@property
|
@property
|
||||||
@@ -136,9 +141,11 @@ class Post:
|
|||||||
|
|
||||||
def _obtain_metadata(self):
|
def _obtain_metadata(self):
|
||||||
if not self._full_metadata_dict:
|
if not self._full_metadata_dict:
|
||||||
pic_json = self._context.get_json("p/{0}/".format(self.shortcode), params={})
|
pic_json = self._context.graphql_query(
|
||||||
self._full_metadata_dict = pic_json['entry_data']['PostPage'][0]['graphql']['shortcode_media']
|
'2b0673e0dc4580674a88d426fe00ea90',
|
||||||
self._rhx_gis_str = pic_json.get('rhx_gis')
|
{'shortcode': self.shortcode}
|
||||||
|
)
|
||||||
|
self._full_metadata_dict = pic_json['data']['shortcode_media']
|
||||||
if self._full_metadata_dict is None:
|
if self._full_metadata_dict is None:
|
||||||
# issue #449
|
# issue #449
|
||||||
self._context.error("Fetching Post metadata failed (issue #449). "
|
self._context.error("Fetching Post metadata failed (issue #449). "
|
||||||
@@ -156,9 +163,13 @@ class Post:
|
|||||||
return self._full_metadata_dict
|
return self._full_metadata_dict
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def _rhx_gis(self) -> Optional[str]:
|
def _iphone_struct(self) -> Dict[str, Any]:
|
||||||
self._obtain_metadata()
|
if not self._context.is_logged_in:
|
||||||
return self._rhx_gis_str
|
raise LoginRequiredException("--login required to access iPhone media info endpoint.")
|
||||||
|
if not self._iphone_struct_:
|
||||||
|
data = self._context.get_iphone_json(path='api/v1/media/{}/info/'.format(self.mediaid), params={})
|
||||||
|
self._iphone_struct_ = data['items'][0]
|
||||||
|
return self._iphone_struct_
|
||||||
|
|
||||||
def _field(self, *keys) -> Any:
|
def _field(self, *keys) -> Any:
|
||||||
"""Lookups given fields in _node, and if not found in _full_metadata. Raises KeyError if not found anywhere."""
|
"""Lookups given fields in _node, and if not found in _full_metadata. Raises KeyError if not found anywhere."""
|
||||||
@@ -225,6 +236,13 @@ class Post:
|
|||||||
@property
|
@property
|
||||||
def url(self) -> str:
|
def url(self) -> str:
|
||||||
"""URL of the picture / video thumbnail of the post"""
|
"""URL of the picture / video thumbnail of the post"""
|
||||||
|
if self.typename == "GraphImage" and self._context.is_logged_in:
|
||||||
|
try:
|
||||||
|
orig_url = self._iphone_struct['image_versions2']['candidates'][0]['url']
|
||||||
|
url = re.sub(r'&se=\d+(&?)', r'\1', orig_url)
|
||||||
|
return url
|
||||||
|
except (InstaloaderException, KeyError, IndexError) as err:
|
||||||
|
self._context.error('{} Unable to fetch high quality image version of {}.'.format(err, self))
|
||||||
return self._node["display_url"] if "display_url" in self._node else self._node["display_src"]
|
return self._node["display_url"] if "display_url" in self._node else self._node["display_src"]
|
||||||
|
|
||||||
@property
|
@property
|
||||||
@@ -239,10 +257,18 @@ class Post:
|
|||||||
if any(edge['node']['is_video'] for edge in edges):
|
if any(edge['node']['is_video'] for edge in edges):
|
||||||
# video_url is only present in full metadata, issue #558.
|
# video_url is only present in full metadata, issue #558.
|
||||||
edges = self._full_metadata['edge_sidecar_to_children']['edges']
|
edges = self._full_metadata['edge_sidecar_to_children']['edges']
|
||||||
for edge in edges:
|
for idx, edge in enumerate(edges):
|
||||||
node = edge['node']
|
node = edge['node']
|
||||||
is_video = node['is_video']
|
is_video = node['is_video']
|
||||||
yield PostSidecarNode(is_video=is_video, display_url=node['display_url'],
|
display_url = node['display_url']
|
||||||
|
if not is_video and self._context.is_logged_in:
|
||||||
|
try:
|
||||||
|
carousel_media = self._iphone_struct['carousel_media']
|
||||||
|
orig_url = carousel_media[idx]['image_versions2']['candidates'][0]['url']
|
||||||
|
display_url = re.sub(r'&se=\d+(&?)', r'\1', orig_url)
|
||||||
|
except (InstaloaderException, KeyError, IndexError) as err:
|
||||||
|
self._context.error('{} Unable to fetch high quality image version of {}.'.format(err, self))
|
||||||
|
yield PostSidecarNode(is_video=is_video, display_url=display_url,
|
||||||
video_url=node['video_url'] if is_video else None)
|
video_url=node['video_url'] if is_video else None)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
@@ -362,7 +388,7 @@ class Post:
|
|||||||
created_at_utc=datetime.utcfromtimestamp(node['created_at']),
|
created_at_utc=datetime.utcfromtimestamp(node['created_at']),
|
||||||
text=node['text'],
|
text=node['text'],
|
||||||
owner=Profile(self._context, node['owner']),
|
owner=Profile(self._context, node['owner']),
|
||||||
likes_count=node['edge_liked_by']['count'])
|
likes_count=node.get('edge_liked_by', {}).get('count', 0))
|
||||||
|
|
||||||
def _postcommentanswers(node):
|
def _postcommentanswers(node):
|
||||||
if 'edge_threaded_comments' not in node:
|
if 'edge_threaded_comments' not in node:
|
||||||
@@ -376,11 +402,14 @@ class Post:
|
|||||||
# If the answer's metadata already contains all comments, don't do GraphQL requests to obtain them
|
# If the answer's metadata already contains all comments, don't do GraphQL requests to obtain them
|
||||||
yield from (_postcommentanswer(comment['node']) for comment in answer_edges)
|
yield from (_postcommentanswer(comment['node']) for comment in answer_edges)
|
||||||
return
|
return
|
||||||
yield from (_postcommentanswer(answer_node) for answer_node in
|
yield from NodeIterator(
|
||||||
self._context.graphql_node_list("51fdd02b67508306ad4484ff574a0b62",
|
self._context,
|
||||||
{'comment_id': node['id']},
|
'51fdd02b67508306ad4484ff574a0b62',
|
||||||
'https://www.instagram.com/p/' + self.shortcode + '/',
|
lambda d: d['data']['comment']['edge_threaded_comments'],
|
||||||
lambda d: d['data']['comment']['edge_threaded_comments']))
|
_postcommentanswer,
|
||||||
|
{'comment_id': node['id']},
|
||||||
|
'https://www.instagram.com/p/{0}/'.format(self.shortcode),
|
||||||
|
)
|
||||||
|
|
||||||
def _postcomment(node):
|
def _postcomment(node):
|
||||||
return PostComment(*_postcommentanswer(node),
|
return PostComment(*_postcommentanswer(node),
|
||||||
@@ -388,29 +417,22 @@ class Post:
|
|||||||
if self.comments == 0:
|
if self.comments == 0:
|
||||||
# Avoid doing additional requests if there are no comments
|
# Avoid doing additional requests if there are no comments
|
||||||
return
|
return
|
||||||
try:
|
|
||||||
comment_edges = self._field('edge_media_to_parent_comment', 'edges')
|
comment_edges = self._field('edge_media_to_comment', 'edges')
|
||||||
answers_count = sum([edge['node']['edge_threaded_comments']['count'] for edge in comment_edges])
|
answers_count = sum([edge['node'].get('edge_threaded_comments', {}).get('count', 0) for edge in comment_edges])
|
||||||
threaded_comments_available = True
|
|
||||||
except KeyError:
|
|
||||||
comment_edges = self._field('edge_media_to_comment', 'edges')
|
|
||||||
answers_count = 0
|
|
||||||
threaded_comments_available = False
|
|
||||||
|
|
||||||
if self.comments == len(comment_edges) + answers_count:
|
if self.comments == len(comment_edges) + answers_count:
|
||||||
# If the Post's metadata already contains all parent comments, don't do GraphQL requests to obtain them
|
# If the Post's metadata already contains all parent comments, don't do GraphQL requests to obtain them
|
||||||
yield from (_postcomment(comment['node']) for comment in comment_edges)
|
yield from (_postcomment(comment['node']) for comment in comment_edges)
|
||||||
return
|
return
|
||||||
yield from (_postcomment(node) for node in
|
yield from NodeIterator(
|
||||||
self._context.graphql_node_list(
|
self._context,
|
||||||
"97b41c52301f77ce508f55e66d17620e" if threaded_comments_available
|
'97b41c52301f77ce508f55e66d17620e',
|
||||||
else "f0986789a5c5d17c2400faebf16efd0d",
|
lambda d: d['data']['shortcode_media']['edge_media_to_parent_comment'],
|
||||||
{'shortcode': self.shortcode},
|
_postcomment,
|
||||||
'https://www.instagram.com/p/' + self.shortcode + '/',
|
{'shortcode': self.shortcode},
|
||||||
lambda d:
|
'https://www.instagram.com/p/{0}/'.format(self.shortcode),
|
||||||
d['data']['shortcode_media'][
|
)
|
||||||
'edge_media_to_parent_comment' if threaded_comments_available else 'edge_media_to_comment'],
|
|
||||||
self._rhx_gis))
|
|
||||||
|
|
||||||
def get_likes(self) -> Iterator['Profile']:
|
def get_likes(self) -> Iterator['Profile']:
|
||||||
"""Iterate over all likes of the post. A :class:`Profile` instance of each likee is yielded."""
|
"""Iterate over all likes of the post. A :class:`Profile` instance of each likee is yielded."""
|
||||||
@@ -422,11 +444,14 @@ class Post:
|
|||||||
# If the Post's metadata already contains all likes, don't do GraphQL requests to obtain them
|
# If the Post's metadata already contains all likes, don't do GraphQL requests to obtain them
|
||||||
yield from (Profile(self._context, like['node']) for like in likes_edges)
|
yield from (Profile(self._context, like['node']) for like in likes_edges)
|
||||||
return
|
return
|
||||||
yield from (Profile(self._context, node) for node in
|
yield from NodeIterator(
|
||||||
self._context.graphql_node_list("1cb6ec562846122743b61e492c85999f", {'shortcode': self.shortcode},
|
self._context,
|
||||||
'https://www.instagram.com/p/' + self.shortcode + '/',
|
'1cb6ec562846122743b61e492c85999f',
|
||||||
lambda d: d['data']['shortcode_media']['edge_liked_by'],
|
lambda d: d['data']['shortcode_media']['edge_liked_by'],
|
||||||
self._rhx_gis))
|
lambda n: Profile(self._context, n),
|
||||||
|
{'shortcode': self.shortcode},
|
||||||
|
'https://www.instagram.com/p/{0}/'.format(self.shortcode),
|
||||||
|
)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def is_sponsored(self) -> bool:
|
def is_sponsored(self) -> bool:
|
||||||
@@ -507,7 +532,6 @@ class Profile:
|
|||||||
self._has_public_story = None # type: Optional[bool]
|
self._has_public_story = None # type: Optional[bool]
|
||||||
self._node = node
|
self._node = node
|
||||||
self._has_full_metadata = False
|
self._has_full_metadata = False
|
||||||
self._rhx_gis = None
|
|
||||||
self._iphone_struct_ = None
|
self._iphone_struct_ = None
|
||||||
if 'iphone_struct' in node:
|
if 'iphone_struct' in node:
|
||||||
# if loaded from JSON with load_structure_from_file()
|
# if loaded from JSON with load_structure_from_file()
|
||||||
@@ -569,10 +593,9 @@ class Profile:
|
|||||||
def _obtain_metadata(self):
|
def _obtain_metadata(self):
|
||||||
try:
|
try:
|
||||||
if not self._has_full_metadata:
|
if not self._has_full_metadata:
|
||||||
metadata = self._context.get_json('{}/'.format(self.username), params={})
|
metadata = self._context.get_json('{}/feed/'.format(self.username), params={})
|
||||||
self._node = metadata['entry_data']['ProfilePage'][0]['graphql']['user']
|
self._node = metadata['entry_data']['ProfilePage'][0]['graphql']['user']
|
||||||
self._has_full_metadata = True
|
self._has_full_metadata = True
|
||||||
self._rhx_gis = metadata.get('rhx_gis')
|
|
||||||
except (QueryReturnedNotFoundException, KeyError) as err:
|
except (QueryReturnedNotFoundException, KeyError) as err:
|
||||||
top_search_results = TopSearchResults(self._context, self.username)
|
top_search_results = TopSearchResults(self._context, self.username)
|
||||||
similar_profiles = [profile.username for profile in top_search_results.get_profiles()]
|
similar_profiles = [profile.username for profile in top_search_results.get_profiles()]
|
||||||
@@ -705,8 +728,7 @@ class Profile:
|
|||||||
'include_reel': False, 'include_suggested_users': False,
|
'include_reel': False, 'include_suggested_users': False,
|
||||||
'include_logged_out_extras': True,
|
'include_logged_out_extras': True,
|
||||||
'include_highlight_reels': False},
|
'include_highlight_reels': False},
|
||||||
'https://www.instagram.com/{}/'.format(self.username),
|
'https://www.instagram.com/{}/'.format(self.username))
|
||||||
self._rhx_gis)
|
|
||||||
self._has_public_story = data['data']['user']['has_public_story']
|
self._has_public_story = data['data']['user']['has_public_story']
|
||||||
assert self._has_public_story is not None
|
assert self._has_public_story is not None
|
||||||
return self._has_public_story
|
return self._has_public_story
|
||||||
@@ -757,86 +779,110 @@ class Profile:
|
|||||||
Use :attr:`profile_pic_url`."""
|
Use :attr:`profile_pic_url`."""
|
||||||
return self.profile_pic_url
|
return self.profile_pic_url
|
||||||
|
|
||||||
def get_posts(self) -> Iterator[Post]:
|
def get_posts(self) -> NodeIterator[Post]:
|
||||||
"""Retrieve all posts from a profile."""
|
"""Retrieve all posts from a profile.
|
||||||
self._obtain_metadata()
|
|
||||||
yield from (Post(self._context, node, self) for node in
|
|
||||||
self._context.graphql_node_list("472f257a40c653c64c666ce877d59d2b",
|
|
||||||
{'id': self.userid},
|
|
||||||
'https://www.instagram.com/{0}/'.format(self.username),
|
|
||||||
lambda d: d['data']['user']['edge_owner_to_timeline_media'],
|
|
||||||
self._rhx_gis,
|
|
||||||
self._metadata('edge_owner_to_timeline_media')))
|
|
||||||
|
|
||||||
def get_saved_posts(self) -> Iterator[Post]:
|
:rtype:NodeIterator[Post]"""
|
||||||
"""Get Posts that are marked as saved by the user."""
|
self._obtain_metadata()
|
||||||
|
return NodeIterator(
|
||||||
|
self._context,
|
||||||
|
'472f257a40c653c64c666ce877d59d2b',
|
||||||
|
lambda d: d['data']['user']['edge_owner_to_timeline_media'],
|
||||||
|
lambda n: Post(self._context, n, self),
|
||||||
|
{'id': self.userid},
|
||||||
|
'https://www.instagram.com/{0}/'.format(self.username),
|
||||||
|
self._metadata('edge_owner_to_timeline_media'),
|
||||||
|
)
|
||||||
|
|
||||||
|
def get_saved_posts(self) -> NodeIterator[Post]:
|
||||||
|
"""Get Posts that are marked as saved by the user.
|
||||||
|
|
||||||
|
:rtype:NodeIterator[Post]"""
|
||||||
|
|
||||||
if self.username != self._context.username:
|
if self.username != self._context.username:
|
||||||
raise LoginRequiredException("--login={} required to get that profile's saved posts.".format(self.username))
|
raise LoginRequiredException("--login={} required to get that profile's saved posts.".format(self.username))
|
||||||
|
|
||||||
self._obtain_metadata()
|
self._obtain_metadata()
|
||||||
yield from (Post(self._context, node) for node in
|
return NodeIterator(
|
||||||
self._context.graphql_node_list("f883d95537fbcd400f466f63d42bd8a1",
|
self._context,
|
||||||
{'id': self.userid},
|
'f883d95537fbcd400f466f63d42bd8a1',
|
||||||
'https://www.instagram.com/{0}/'.format(self.username),
|
lambda d: d['data']['user']['edge_saved_media'],
|
||||||
lambda d: d['data']['user']['edge_saved_media'],
|
lambda n: Post(self._context, n),
|
||||||
self._rhx_gis,
|
{'id': self.userid},
|
||||||
self._metadata('edge_saved_media')))
|
'https://www.instagram.com/{0}/'.format(self.username),
|
||||||
|
self._metadata('edge_saved_media'),
|
||||||
|
)
|
||||||
|
|
||||||
def get_tagged_posts(self) -> Iterator[Post]:
|
def get_tagged_posts(self) -> NodeIterator[Post]:
|
||||||
"""Retrieve all posts where a profile is tagged.
|
"""Retrieve all posts where a profile is tagged.
|
||||||
|
|
||||||
|
:rtype: NodeIterator[Post]
|
||||||
|
|
||||||
.. versionadded:: 4.0.7"""
|
.. versionadded:: 4.0.7"""
|
||||||
self._obtain_metadata()
|
self._obtain_metadata()
|
||||||
yield from (Post(self._context, node, self if int(node['owner']['id']) == self.userid else None) for node in
|
return NodeIterator(
|
||||||
self._context.graphql_node_list("e31a871f7301132ceaab56507a66bbb7",
|
self._context,
|
||||||
{'id': self.userid},
|
'e31a871f7301132ceaab56507a66bbb7',
|
||||||
'https://www.instagram.com/{0}/'.format(self.username),
|
lambda d: d['data']['user']['edge_user_to_photos_of_you'],
|
||||||
lambda d: d['data']['user']['edge_user_to_photos_of_you'],
|
lambda n: Post(self._context, n, self if int(n['owner']['id']) == self.userid else None),
|
||||||
self._rhx_gis))
|
{'id': self.userid},
|
||||||
|
'https://www.instagram.com/{0}/'.format(self.username),
|
||||||
|
)
|
||||||
|
|
||||||
def get_igtv_posts(self) -> Iterator[Post]:
|
def get_igtv_posts(self) -> NodeIterator[Post]:
|
||||||
"""Retrieve all IGTV posts.
|
"""Retrieve all IGTV posts.
|
||||||
|
|
||||||
|
:rtype: NodeIterator[Post]
|
||||||
|
|
||||||
.. versionadded:: 4.3"""
|
.. versionadded:: 4.3"""
|
||||||
self._obtain_metadata()
|
self._obtain_metadata()
|
||||||
yield from (Post(self._context, node, self) for node in
|
return NodeIterator(
|
||||||
self._context.graphql_node_list('bc78b344a68ed16dd5d7f264681c4c76',
|
self._context,
|
||||||
{'id': self.userid},
|
'bc78b344a68ed16dd5d7f264681c4c76',
|
||||||
'https://www.instagram.com/{0}/channel/'.format(self.username),
|
lambda d: d['data']['user']['edge_felix_video_timeline'],
|
||||||
lambda d: d['data']['user']['edge_felix_video_timeline'],
|
lambda n: Post(self._context, n, self),
|
||||||
self._rhx_gis,
|
{'id': self.userid},
|
||||||
self._metadata('edge_felix_video_timeline')))
|
'https://www.instagram.com/{0}/channel/'.format(self.username),
|
||||||
|
self._metadata('edge_felix_video_timeline'),
|
||||||
|
)
|
||||||
|
|
||||||
def get_followers(self) -> Iterator['Profile']:
|
def get_followers(self) -> NodeIterator['Profile']:
|
||||||
"""
|
"""
|
||||||
Retrieve list of followers of given profile.
|
Retrieve list of followers of given profile.
|
||||||
To use this, one needs to be logged in and private profiles has to be followed.
|
To use this, one needs to be logged in and private profiles has to be followed.
|
||||||
|
|
||||||
|
:rtype:NodeIterator[Profile]
|
||||||
"""
|
"""
|
||||||
if not self._context.is_logged_in:
|
if not self._context.is_logged_in:
|
||||||
raise LoginRequiredException("--login required to get a profile's followers.")
|
raise LoginRequiredException("--login required to get a profile's followers.")
|
||||||
self._obtain_metadata()
|
self._obtain_metadata()
|
||||||
yield from (Profile(self._context, node) for node in
|
return NodeIterator(
|
||||||
self._context.graphql_node_list("37479f2b8209594dde7facb0d904896a",
|
self._context,
|
||||||
{'id': str(self.userid)},
|
'37479f2b8209594dde7facb0d904896a',
|
||||||
'https://www.instagram.com/' + self.username + '/',
|
lambda d: d['data']['user']['edge_followed_by'],
|
||||||
lambda d: d['data']['user']['edge_followed_by'],
|
lambda n: Profile(self._context, n),
|
||||||
self._rhx_gis))
|
{'id': str(self.userid)},
|
||||||
|
'https://www.instagram.com/{0}/'.format(self.username),
|
||||||
|
)
|
||||||
|
|
||||||
def get_followees(self) -> Iterator['Profile']:
|
def get_followees(self) -> NodeIterator['Profile']:
|
||||||
"""
|
"""
|
||||||
Retrieve list of followees (followings) of given profile.
|
Retrieve list of followees (followings) of given profile.
|
||||||
To use this, one needs to be logged in and private profiles has to be followed.
|
To use this, one needs to be logged in and private profiles has to be followed.
|
||||||
|
|
||||||
|
:rtype:NodeIterator[Profile]
|
||||||
"""
|
"""
|
||||||
if not self._context.is_logged_in:
|
if not self._context.is_logged_in:
|
||||||
raise LoginRequiredException("--login required to get a profile's followees.")
|
raise LoginRequiredException("--login required to get a profile's followees.")
|
||||||
self._obtain_metadata()
|
self._obtain_metadata()
|
||||||
yield from (Profile(self._context, node) for node in
|
return NodeIterator(
|
||||||
self._context.graphql_node_list("58712303d941c6855d4e888c5f0cd22f",
|
self._context,
|
||||||
{'id': str(self.userid)},
|
'58712303d941c6855d4e888c5f0cd22f',
|
||||||
'https://www.instagram.com/' + self.username + '/',
|
lambda d: d['data']['user']['edge_follow'],
|
||||||
lambda d: d['data']['user']['edge_follow'],
|
lambda n: Profile(self._context, n),
|
||||||
self._rhx_gis))
|
{'id': str(self.userid)},
|
||||||
|
'https://www.instagram.com/{0}/'.format(self.username),
|
||||||
|
)
|
||||||
|
|
||||||
def get_similar_accounts(self) -> Iterator['Profile']:
|
def get_similar_accounts(self) -> Iterator['Profile']:
|
||||||
"""
|
"""
|
||||||
@@ -851,8 +897,8 @@ class Profile:
|
|||||||
yield from (Profile(self._context, edge["node"]) for edge in
|
yield from (Profile(self._context, edge["node"]) for edge in
|
||||||
self._context.graphql_query("ad99dd9d3646cc3c0dda65debcd266a7",
|
self._context.graphql_query("ad99dd9d3646cc3c0dda65debcd266a7",
|
||||||
{"user_id": str(self.userid), "include_chaining": True},
|
{"user_id": str(self.userid), "include_chaining": True},
|
||||||
"https://www.instagram.com/{0}/".format(self.username),
|
"https://www.instagram.com/{0}/"
|
||||||
self._rhx_gis)["data"]["user"]["edge_chaining"]["edges"])
|
.format(self.username))["data"]["user"]["edge_chaining"]["edges"])
|
||||||
|
|
||||||
|
|
||||||
class StoryItem:
|
class StoryItem:
|
||||||
@@ -1391,7 +1437,7 @@ class TopSearchResults:
|
|||||||
return self._searchstring
|
return self._searchstring
|
||||||
|
|
||||||
|
|
||||||
JsonExportable = Union[Post, Profile, StoryItem, Hashtag]
|
JsonExportable = Union[Post, Profile, StoryItem, Hashtag, FrozenNodeIterator]
|
||||||
|
|
||||||
|
|
||||||
def save_structure_to_file(structure: JsonExportable, filename: str) -> None:
|
def save_structure_to_file(structure: JsonExportable, filename: str) -> None:
|
||||||
@@ -1440,6 +1486,8 @@ def load_structure_from_file(context: InstaloaderContext, filename: str) -> Json
|
|||||||
return StoryItem(context, json_structure['node'])
|
return StoryItem(context, json_structure['node'])
|
||||||
elif node_type == "Hashtag":
|
elif node_type == "Hashtag":
|
||||||
return Hashtag(context, json_structure['node'])
|
return Hashtag(context, json_structure['node'])
|
||||||
|
elif node_type == "FrozenNodeIterator":
|
||||||
|
return FrozenNodeIterator(**json_structure['node'])
|
||||||
else:
|
else:
|
||||||
raise InvalidArgumentException("{}: Not an Instaloader JSON.".format(filename))
|
raise InvalidArgumentException("{}: Not an Instaloader JSON.".format(filename))
|
||||||
elif 'shortcode' in json_structure:
|
elif 'shortcode' in json_structure:
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ import shutil
|
|||||||
import tempfile
|
import tempfile
|
||||||
import unittest
|
import unittest
|
||||||
from itertools import islice
|
from itertools import islice
|
||||||
from typing import Dict, List
|
from typing import Optional
|
||||||
|
|
||||||
import instaloader
|
import instaloader
|
||||||
|
|
||||||
@@ -23,8 +23,7 @@ PRIVATE_PROFILE_ID = 1706625676
|
|||||||
EMPTY_PROFILE = "not_public"
|
EMPTY_PROFILE = "not_public"
|
||||||
EMPTY_PROFILE_ID = 1928659031
|
EMPTY_PROFILE_ID = 1928659031
|
||||||
|
|
||||||
# Preserve query timestamps (rate control) between tests to not get rate limited
|
ratecontroller = None # type: Optional[instaloader.RateController]
|
||||||
instaloadercontext_query_timestamps = dict() # type: Dict[str, List[float]]
|
|
||||||
|
|
||||||
|
|
||||||
class TestInstaloaderAnonymously(unittest.TestCase):
|
class TestInstaloaderAnonymously(unittest.TestCase):
|
||||||
@@ -37,13 +36,15 @@ class TestInstaloaderAnonymously(unittest.TestCase):
|
|||||||
download_comments=True,
|
download_comments=True,
|
||||||
save_metadata=True)
|
save_metadata=True)
|
||||||
self.L.context.raise_all_errors = True
|
self.L.context.raise_all_errors = True
|
||||||
# pylint:disable=protected-access
|
if ratecontroller is not None:
|
||||||
self.L.context._graphql_query_timestamps = instaloadercontext_query_timestamps.copy()
|
# pylint:disable=protected-access
|
||||||
|
ratecontroller._context = self.L.context
|
||||||
|
self.L.context._rate_controller = ratecontroller
|
||||||
|
|
||||||
def tearDown(self):
|
def tearDown(self):
|
||||||
# pylint:disable=global-statement,protected-access
|
# pylint:disable=global-statement,protected-access
|
||||||
global instaloadercontext_query_timestamps
|
global ratecontroller
|
||||||
instaloadercontext_query_timestamps = self.L.context._graphql_query_timestamps.copy()
|
ratecontroller = self.L.context._rate_controller
|
||||||
self.L.close()
|
self.L.close()
|
||||||
os.chdir('/')
|
os.chdir('/')
|
||||||
print("Removing {}".format(self.dir))
|
print("Removing {}".format(self.dir))
|
||||||
@@ -94,6 +95,10 @@ class TestInstaloaderAnonymously(unittest.TestCase):
|
|||||||
self.assertEqual(EMPTY_PROFILE.lower(),
|
self.assertEqual(EMPTY_PROFILE.lower(),
|
||||||
instaloader.Profile.from_id(self.L.context, EMPTY_PROFILE_ID).username)
|
instaloader.Profile.from_id(self.L.context, EMPTY_PROFILE_ID).username)
|
||||||
|
|
||||||
|
def test_get_username_by_name_empty(self):
|
||||||
|
self.assertEqual(EMPTY_PROFILE_ID,
|
||||||
|
instaloader.Profile.from_username(self.L.context, EMPTY_PROFILE).userid)
|
||||||
|
|
||||||
def test_post_from_mediaid(self):
|
def test_post_from_mediaid(self):
|
||||||
for post in instaloader.Profile.from_username(self.L.context, PUBLIC_PROFILE).get_posts():
|
for post in instaloader.Profile.from_username(self.L.context, PUBLIC_PROFILE).get_posts():
|
||||||
post2 = instaloader.Post.from_mediaid(self.L.context, post.mediaid)
|
post2 = instaloader.Post.from_mediaid(self.L.context, post.mediaid)
|
||||||
|
|||||||
Reference in New Issue
Block a user