Skip to content

Instantly share code, notes, and snippets.

@ahjota
Forked from dschep/top9.py
Last active January 1, 2020 21:29
Show Gist options
  • Save ahjota/df3833e8352f26f3a1507e8851c41f43 to your computer and use it in GitHub Desktop.
Save ahjota/df3833e8352f26f3a1507e8851c41f43 to your computer and use it in GitHub Desktop.
Instagram Top 9
"""
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 IGUSERNAME-top9.jpg in your working directory.
Forked from dschep/top9.py -- https://git.io/JeAMA
"""
from datetime import datetime
from igramscraper.instagram import Instagram
from PIL import Image
import click
import requests
import os
instagram = Instagram()
@click.command()
@click.option(
"--user",
prompt="Your IG account (without @)",
help="Your Instagram account.",
)
@click.option(
"--account",
help="The IG account to create a top 9 for; defaults to --user.",
)
@click.option(
"--password",
prompt="Your IG password",
hide_input=True,
help="The password for your IG account.",
)
@click.option(
"--tfa",
help="Use two factor auth during login.",
)
def top9(user, account=None, password=None, tfa=False):
if password is not None:
instagram.with_credentials(user, password)
instagram.login(two_step_verificator=tfa)
print(f"Logged in as @{user}")
now = datetime.utcnow()
if now.month > 7:
this_year = now.year
else:
this_year = now.year - 1
if account is None:
account = user
print(f"Retrieving @{account}'s posts for {this_year}")
posts = None
count = 0
while (
posts is None
or (datetime.fromtimestamp(posts[-1].created_time).year >= this_year
and len(posts) == count)
):
count += 50
posts = instagram.get_medias(account, count)
this_year_photos = [
post
for post in posts
if datetime.fromtimestamp(post.created_time).year == this_year
# and post.type == post.TYPE_IMAGE
# and post.type == post.TYPE_VIDEO
]
print("Sorting photos")
top9 = sorted(this_year_photos, key=lambda post: -post.likes_count)[:9]
print(f"Compiling @{account}'s top9")
img = Image.new("RGB", (3240, 3240))
for i, post in enumerate(top9):
# print(f"{post}")
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((1080, 1080), Image.ANTIALIAS)
print(f"{post.likes_count} likes - {datetime.fromtimestamp(post.created_time)} - {post.link}")
# print(f"{post.caption}")
img.paste(tile, (i % 3 * 1080, i // 3 * 1080))
img.save(f"{account}-top9.jpg")
if __name__ == "__main__":
top9()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment