#!/usr/bin/env python
# -*- coding: utf-8 -*-

import getopt
import os
import re
import sys
import urllib2

from BeautifulSoup import BeautifulSoup
from urlparse import urlsplit

headers = {'User-Agent':'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.7) Gecko/2009021910 Firefox/3.0.7'}

def download(url, path, flag):
    try:
        board = re.findall(r'\/[a-z0-9]{1,4}\/', url)[0]
        request = urllib2.Request(url, None, headers)
        response = urllib2.urlopen(request)
        page_content = response.read()
        # page_content = urllib2.urlopen(url).read()
        soup = BeautifulSoup(page_content)
        tags = soup.findAll('a')

        # Old HTML img tag:
        # <a href="//i.4cdn.org/<board>/src/<filename>.<extension>">

        # New HTML img tag:
        # <a href="//i.4cdn.org/<board>/<filename>.<extension>"

        patterns = [
            "//i.4cdn.org"+board+"[0-9]+.jpg",
            "//i.4cdn.org"+board+"[0-9]+.png",
            "//i.4cdn.org"+board+"[0-9]+.gif",
            "//i.4cdn.org"+board+"[0-9]+.webm"
        ]

        images = []
        original_names = []

        for tag in tags:
            href = tag.get('href')
            string = tag.string
            for pattern in patterns:
                if re.match(pattern, href) and string is not None:
                    images.append(href)
                    original_names.append(string)

        n = len(images)
        print "There are {} images in {}".format(n, url)

        for i in range(0, n):
            image = urllib2.urlopen("https:" + images[i]).read()
            if flag:
                filename = original_names[i]
            else:
                filename = re.findall(r'[0-9]+.[a-z]{3,4}',
                                      urlsplit(images[i])[2])[0]
            complete_path = os.path.join(path, filename)
            output = open(complete_path, 'wb')
            output.write(image)
            output.close()

            print "Downloaded image {} of {}".format(i + 1, n)
        print "\nDownload complete!"

    except Exception as inst:
        print "Error\n" + str(inst)


def main(argv):
    flag = False
    try:
        opts, args = getopt.getopt(argv, "hi:o:k")
    except getopt.GetoptError:
        print './4chan-dl -i URL -o path [-k]'
        sys.exit(2)

    for opt, arg in opts:
        if opt == '-h':
            print './4chan-dl -i URL -o path'
            sys.exit()
        elif opt == '-i':
            url = arg
        elif opt == '-o':
            path = arg
        elif opt == '-k':
            flag = True

    download(url, path, flag)

if __name__ == "__main__":
    main(sys.argv[1:])
