Last active
January 1, 2022 03:16
-
-
Save dschep/28bf9848d76d4790df1330a246cc90cc to your computer and use it in GitHub Desktop.
Instagram Top 9 - example output: https://www.instagram.com/p/B6vVp93Bb5F/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
An Instagram Top 9 generator that doesn't require connecting your account | |
to an untrustworthy 3rd party Instagram app. | |
Install the requirements with `pip3 install igramscraper Pillow click` | |
then run `python3 top9.py` | |
When done, you will have a YOURUSERNAME-top9.jpg in your working directory. | |
""" | |
from datetime import datetime | |
from igramscraper.instagram import Instagram | |
from PIL import Image | |
import click | |
import requests | |
instagram = Instagram() | |
@click.command() | |
@click.option( | |
"--user", | |
prompt="Your Instagram account (without @)", | |
help="The instagram username to create a top 9 for", | |
) | |
@click.option( | |
"--login-user", | |
help="The instagram username to login with. Doesn't log in if omitted.", | |
) | |
@click.option( | |
"--tfa", | |
help="Use two factor auth during login", | |
) | |
def top9(user, tfa=False, login_user=None): | |
if login_user: | |
password = click.prompt("Your Instagram password") | |
instagram.with_credentials(login_user, password) | |
instagram.login(two_step_verificator=tfa) | |
now = datetime.utcnow() | |
if now.month > 7: | |
this_year = now.year | |
else: | |
this_year = now.year - 1 | |
posts = None | |
count = 0 | |
prev = None | |
while ( | |
posts is None | |
or datetime.fromtimestamp(posts[-1].created_time).year >= this_year | |
): | |
count += 50 | |
posts = instagram.get_medias(user, count) | |
last = datetime.fromtimestamp(posts[-1].created_time) | |
if prev == last: | |
break | |
prev = last | |
this_year_photos = [ | |
post | |
for post in posts | |
if datetime.fromtimestamp(post.created_time).year == this_year | |
and post.type in (post.TYPE_IMAGE, post.TYPE_SIDECAR) | |
] | |
top9 = sorted(this_year_photos, key=lambda post: -post.likes_count)[:9] | |
img = Image.new("RGB", (1080, 1080)) | |
for i, post in enumerate(top9): | |
tile = Image.open(requests.get(post.image_high_resolution_url, stream=True).raw) | |
if tile.size[0] > tile.size[1]: | |
tile = tile.crop( | |
( | |
(tile.size[0] - tile.size[1]) / 2, | |
0, | |
(tile.size[0] - tile.size[1]) / 2 + tile.size[1], | |
tile.size[1], | |
) | |
) | |
elif tile.size[0] < tile.size[1]: | |
tile = tile.crop( | |
( | |
0, | |
(tile.size[1] - tile.size[0]) / 2, | |
tile.size[0], | |
(tile.size[1] - tile.size[0]) / 2 + tile.size[0], | |
) | |
) | |
tile = tile.resize((360, 360), Image.ANTIALIAS) | |
print(f"{post.likes_count} likes - {post.link}") | |
img.paste(tile, (i % 3 * 360, i // 3 * 360)) | |
img.save(f"{user}-top9.jpg") | |
if __name__ == "__main__": | |
top9() |
raenb0
commented
Jan 3, 2020
via email
Hmm not sure what I'm doing wrong. it seems to install fine (but I have to
modify "pip3" to "pip"
But then when I run python3 top9.p nothing appears to happen (no error msg)
I am using ConEmu to run python scripts
not sure if I need to change the working directory or sth?
[image: image.png]
…On Wed, Jan 1, 2020 at 4:15 PM Daniel Schep ***@***.***> wrote:
@ahjota <https://github.com/ahjota>, yeah, probably not the best idea to
put your password in your shell history anyway :) Updated the gist! [image:
]
Also -- removing the post.type filter lets you pull multi-image posts
(sidecars) and videos as well. :)
and post.type == post.TYPE_IMAGE
ah, didn't think of mutlimage, but I figured video probably wouldn't be
that useful tho.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<https://gist.github.com/28bf9848d76d4790df1330a246cc90cc?email_source=notifications&email_token=AOFEWTG73BWKR5C5DMQB7DLQ3UBVVA5CNFSM4KBWA4T2YY3PNVWWK3TUL52HS4DFVNDWS43UINXW23LFNZ2KUY3PNVWWK3TUL5UWJTQAF6XMO#gistcomment-3124935>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AOFEWTCRPG6CBMXPXFHMZGDQ3UBVVANCNFSM4KBWA4TQ>
.
![:shipit: :shipit:](https://github.githubassets.com/images/icons/emoji/shipit.png)
Huh, that's odd. I don't think there's any issue with conemu. If you're using python3 top9.py
maybe try the pip install as python3 -m pip install igramscraper Pillow click
. But it's still very odd that it does nothing. I'd expect at least an error 🤔
Yeah still nothing. Don't worry about it! I am probably doing something
weird on my end.
…On Fri, Jan 3, 2020 at 6:32 PM Daniel Schep ***@***.***> wrote:
Huh, that's odd. I don't think there's any issue with conemu. If you're
using python3 top9.py maybe try the pip install as python3 -m pip install
igramscraper Pillow click. But it's still very odd that it does nothing.
I'd expect at least an error 🤔
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<https://gist.github.com/28bf9848d76d4790df1330a246cc90cc?email_source=notifications&email_token=AOFEWTADZ542IBUIAZYPNCLQ37DKPA5CNFSM4KBWA4T2YY3PNVWWK3TUL52HS4DFVNDWS43UINXW23LFNZ2KUY3PNVWWK3TUL5UWJTQAF63EY#gistcomment-3126860>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AOFEWTAGNK3WY4MTA5ECXWDQ37DKPANCNFSM4KBWA4TQ>
.
Currently broken due to https://github.com/realsirjoe/instagram-scraper/issues/158
Oops, actually still works fine without login, but bc of the change from @ahota, you couldn't actually not login. I've changed it so that password is only by prompt, and you specify the desire to login by using the --login-user
flag.
Just doing a 2021 check in to say: Still works!
Is it weird that I used this to test my terminal was working on an old computer? probably. sorry.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment