#!/usr/bin/env python3

import argparse
import re
import sys
import requests


def fetchDP(username):
    url = "https://www.instagram.com/{}/?__a=1".format(username)

    r = requests.get(url)

    if r.ok:
        data = r.json()
        return data['graphql']['user']['profile_pic_url_hd']

    else:
        print("\033[91m✘ Cannot find user ID \033[0m")
        sys.exit()


def main():
    parser = argparse.ArgumentParser(
        description="Download any users Instagram display picture/profile picture in full quality")

    parser.add_argument('username', action="store", help="username of the Instagram user")

    args = parser.parse_args()

    username = args.username

    file_url = fetchDP(username)
    fname = username + ".jpg"

    r = requests.get(file_url, stream=True)
    if r.ok:
        with open(fname, 'wb') as f:
            f.write(r.content)
            print("\033[92m✔ Downloaded:\033[0m {}".format(fname))
    else:
        print("Cannot make connection to download image")


if __name__ == "__main__":
    main()
