From e21d34124dac3e5f384ca5d233eafb977c2f48ee Mon Sep 17 00:00:00 2001 From: Alexander Graf <17130992+aandergr@users.noreply.github.com> Date: Sun, 21 Jun 2020 19:38:16 +0200 Subject: [PATCH] Add codesnippet for almost chronological order Such as for downloading hashtag feeds, as discussed in #666 and contributed by @e2tovar. Also change comment color to grey in codesnippets in documentation. --- docs/_static/instaloader.css | 4 ++- docs/_static/instaloader.scss | 6 +++- docs/codesnippets.rst | 13 ++++++-- docs/codesnippets/121_since_until.py | 6 ++-- .../666_historical_hashtag_data.py | 30 +++++++++++++++++++ 5 files changed, 51 insertions(+), 8 deletions(-) create mode 100644 docs/codesnippets/666_historical_hashtag_data.py diff --git a/docs/_static/instaloader.css b/docs/_static/instaloader.css index 4c92ea1..4d1ec72 100644 --- a/docs/_static/instaloader.css +++ b/docs/_static/instaloader.css @@ -17,7 +17,9 @@ code { .highlight pre { padding: 0.7em; color: #fff; } - .highlight .c1, .highlight .k, .highlight .kn, .highlight .ow { + .highlight .c1 { + color: #666; } + .highlight .k, .highlight .kn, .highlight .ow { color: #008d06; } .highlight .nb, .highlight .ne, .highlight .nf, .highlight .vm { color: #f48400; } diff --git a/docs/_static/instaloader.scss b/docs/_static/instaloader.scss index 4680585..b8953f5 100644 --- a/docs/_static/instaloader.scss +++ b/docs/_static/instaloader.scss @@ -36,7 +36,11 @@ code { color: #fff; } - .c1, .k, .kn, .ow { + .c1 { + color: #666; + } + + .k, .kn, .ow { color: $color_instaloader_main } diff --git a/docs/codesnippets.rst b/docs/codesnippets.rst index 58430a6..39fba14 100644 --- a/docs/codesnippets.rst +++ b/docs/codesnippets.rst @@ -28,8 +28,9 @@ Download Posts in a Specific Period ----------------------------------- To only download Instagram pictures (and metadata) that are within a specific -period, you can play around with :func:`~itertools.dropwhile` and -:func:`~itertools.takewhile` from :mod:`itertools` like in this snippet. +period, you can simply use :func:`~itertools.dropwhile` and +:func:`~itertools.takewhile` from :mod:`itertools` on a generator that returns +Posts in **exact chronological order**, such as :meth:`Profile.get_posts`. .. literalinclude:: codesnippets/121_since_until.py @@ -37,6 +38,14 @@ See also :class:`Post`, :meth:`Instaloader.download_post`. 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 ------------------------------------ diff --git a/docs/codesnippets/121_since_until.py b/docs/codesnippets/121_since_until.py index 355b392..4df2c27 100644 --- a/docs/codesnippets/121_since_until.py +++ b/docs/codesnippets/121_since_until.py @@ -5,13 +5,11 @@ import instaloader L = instaloader.Instaloader() -posts = instaloader.Hashtag.from_name(L.context, 'urbanphotography').get_posts() -# or -# posts = instaloader.Profile.from_username(L.context, PROFILE).get_posts() +posts = instaloader.Profile.from_username(L.context, "instagram").get_posts() SINCE = datetime(2015, 5, 1) UNTIL = datetime(2015, 3, 1) for post in takewhile(lambda p: p.date > UNTIL, dropwhile(lambda p: p.date > SINCE, posts)): print(post.date) - L.download_post(post, '#urbanphotography') + L.download_post(post, "instagram") diff --git a/docs/codesnippets/666_historical_hashtag_data.py b/docs/codesnippets/666_historical_hashtag_data.py new file mode 100644 index 0000000..8889937 --- /dev/null +++ b/docs/codesnippets/666_historical_hashtag_data.py @@ -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)