#!/usr/bin/env python3

import os
import re
import sys

from platform import system as operatingSystem
from subprocess import call
from urllib.request import Request, urlopen


def getWebPage(url):
    print("\ngetting: "+url)
    h = Request(url)
    h.add_header('User-Agent', 'SPD/1.0')
    webpage = urlopen(h).read()
    return str(webpage)  # convert from bytes to string


def getSubmittedPage(userName):
    return getWebPage('https://www.reddit.com/user/' +
                      userName +
                      '/submitted/')


def downloadImage(link):
    print('downloading: ' + link)
    if operatingSystem() == 'Windows':
        # manually installed wget
        # --no-check-certificate is used because GnuWin wget fails to verify 
        #   all certificates for some reason
        call(['C:\\Program Files (x86)\\GnuWin32\\bin\\wget.exe', '-b',
              '-N', '-o', 'NUL', '--no-check-certificate', link])
    else:
        # open wget in the background
        call(['wget', '-b', '-N', '-o', '/dev/null', link])


def downloadImageGallery(link):
    webpage = getWebPage(link)
    link = link.replace('http:', 'https:')
    if re.search(r"gfycat\.com/", link):
        link = link.replace('gfycat', 'giant.gfycat') + '.gif'
        downloadImage(link)
    elif re.search(r"imgur\.com/", link):
        for image in getAllImageURLs(webpage):
            downloadImage(image)
    pass


def isGallery(link):
    if re.match(r"https://(?:imgur\.com/|gfycat\.com/)", link):
        return True
    return False


def getAllImageURLs(webpage):
    urlList = re.findall(
        r'src="//(i\.imgur\.com/[a-zA-Z0-9]{7}\.(?:[a-z]{3,4})(?:\?[0-9]+?)?)"',
        webpage)
    return urlList


def getAllImages(webpage):
    for link in re.findall(
            '<a class=\"title may-blank ?\" href=\"(https?://' +
            '(?:gfycat\\.com/[a-zA-Z]+|' +
            'imgur\\.com/(?:[a-zA-Z0-9]{7}|[a-zA-Z0-9]{5})|' +
            'imgur\\.com/a/[a-zA-Z0-9]{5}|' +
            'imgur\\.com/gallery/[a-zA-Z0-9]{5}|' +
            'i\\.imgur\\.com/(?:[a-zA-Z0-9]{7}|' +
            '[a-zA-Z0-9]{5})\\.(?:[a-z]{3,4})(?:\?[0-9]+?)?))',
            webpage):
        link = link.replace('http:', 'https:')
        link = link.replace('.gifv', '.gif')  # fix handling of gifv links
        if isGallery(link):
            downloadImageGallery(link)
        else:
            downloadImage(link)


def pageGetNextPage(webpage, userName):
    nextPage = re.findall(
        "(https?://www\\.reddit\\.com/user/" +
        userName +
        "/submitted/\\?count=[0-9]{2,4}&amp;after=t[0-9]_[a-z0-9]{6})",
        webpage)
    if not nextPage == []:
        return getWebPage(nextPage[0].replace('amp;', ''))
    else:
        return None

userName = sys.argv[1]
if len(sys.argv) > 2:
    basePath = os.path.expanduser(sys.argv[2]) + '/'
else:
    basePath = os.path.expanduser("~/Pictures/SPD/")


if not os.path.exists(basePath + userName):
    os.makedirs(basePath + userName)
os.chdir(basePath + userName)

userSubmitted = getSubmittedPage(userName)

getAllImages(userSubmitted)

while True:
    userSubmitted = pageGetNextPage(userSubmitted, userName)
    if userSubmitted is None:
        break

    getAllImages(userSubmitted)

@@ -1594,17 +1594,16 @@
         
-r
 'src=%22//
@@ -1609,20 +1609,25 @@
 /(i%5C
+%5C
 .imgur%5C
+%5C
 .com/
+(?:
 %5Ba-z
@@ -1636,16 +1636,46 @@
 Z0-9%5D%7B7%7D
+%7C' +%0A        '%5Ba-zA-Z0-9%5D%7B5%7D)%5C
 %5C.(?:%5Ba-
@@ -1678,32 +1678,33 @@
 :%5Ba-z%5D%7B3,4%7D)(?:%5C
+%5C
 ?%5B0-9%5D+?)?)%22',%0A 
@@ -1696,17 +1696,16 @@
 %5B0-9%5D+?)
-?
 )%22',%0A   

improve image regex