#!/usr/bin/env python

"""
Image scraper utilizing Reddit Python API, PRAW.
Requires PRAW library installed, or pip installed to get it.
Choose a subreddit, it identifies pictures in that sub
and downloads to specified directory.
"""

__author__ = 'lance - github.com/lalanza808'

################################################################################
# Library Import


# Builtin libs
from urllib import urlretrieve
from os import path,system,getenv
from time import strftime, sleep
import ConfigParser
import argparse

# 3rd party libs
try:
	import praw
except ImportError:
	print("\nPython library PRAW not installed.\n\nTry:\n\nsudo pip install praw")
	exit()


################################################################################
# Variable Declaration

# Open config
configfile = '{}/.config/rharvest.conf'.format(getenv('HOME'))
configparse = ConfigParser.ConfigParser()
configparse.readfp(open(configfile))
savedir = configparse.get('rharvest', 'savedir')
maxthreads = configparse.get('rharvest', 'maxthreads')
useragent = configparse.get('rharvest', 'useragent')
curtime = strftime(str(configparse.get('rharvest', 'timeformat')))
counter = 1
args = ''

parser = argparse.ArgumentParser(description='Command line Reddit image scraper. Supply the subreddit(s) separated by commas (no space), supply the number of images.')
parser.add_argument('--sub', '-s', type=str, dest='subreddit', help='The SubReddit name(s), separated by commas. Eg: minimalwallpaper,gifs,funny', required=True)
parser.add_argument('--count', '-c', type=int, dest='count', help='The amount of images you wish to download', required=True)
parser.add_argument('--dest', '-d', type=str, dest='dest', help='Save location. Set manually here or it defaults to value in config file', required=False)
args = parser.parse_args()

if args.dest == None:
	savedir = configparse.get('rharvest', 'savedir')
else:
	savedir = args.dest

def main():
	CreateFolders(args.subreddit)

def CreateFolders(subs):
	subs = subs.split(',')
	for sub in subs:
		if not path.exists("{}/{}/{}".format(savedir, sub, curtime)):
			print("Creating new directory : {}/{}/{}".format(savedir, sub, str(curtime)))
			sleep(2)
			system("mkdir -p {}/{}/{}".format(savedir, sub, curtime))
		DownloadImages(sub)

def DownloadImages(sub):
	global counter
	ua = praw.Reddit(useragent)
	subscrape = ua.get_subreddit(sub).get_hot(limit=(args.count + args.count))
	for image in subscrape:
		extension = image.url[-4:]
		if extension == '.jpg' or extension == '.png' or extension == '.gif':
			img = path.basename(image.url)
			dlas = "{}/{}/{}/{}.{}".format(savedir, sub, curtime, str(counter), img)
			urlretrieve(image.url, dlas)
			print "Downloading {} as {}".format(img, dlas)
			counter += 1

if __name__ == "__main__":
	main()
