__author__ = 'Constantin Serban'

import re
import urllib
from multiprocessing.dummy import Pool as ThreadPool
from sql_connect import main as gr
from optparse import OptionParser
import os


def get_links(items):
    links = []
    for item in items:
        matches = 0
        if item is not None:
            matches = re.findall(r"(http(s?):([/|.|\w|\s])*\.(?:jpg|gif|png))", item[0])
            match_no = len(matches)
            if matches:
                while match_no:
                    match_no -= 1
                    link = matches[match_no]
                    links.append(link)
    return links


def parallel_start(download_map):
    pool = ThreadPool(4)
    results = pool.map(download_files, download_map)
    pool.close()
    pool.join()


def download_files(download_map):
    location = download_map[1]
    link = download_map[0][0]
    if not os.path.exists(location):
        os.makedirs(location)
    f = open((location + "\\" + link[link.rfind('/') + 1:]), 'wb')
    f.write(urllib.urlopen(link).read())
    f.close()


def main():
    parser = OptionParser()
    parser.add_option("-s", "--save", action="store_true", dest="keep_saved", default=False,
                      help="Do not mark items as unsaved")
    parser.add_option("-d", "--download", action="store", dest="download_location", default=False,
                      help="Download location")
    (options, args) = parser.parse_args()
    keep_saved = options.keep_saved
    download_location = options.download_location
    items = gr(keep_saved)
    links = get_links(items)
    download_map = []
    for a in links:
        download_map.append((a, download_location))
    parallel_start(download_map)

if __name__ == "__main__":
    main()
@@ -858,75 +858,8 @@
 %5B0%5D%0A
-    if not os.path.exists(location):%0A        os.makedirs(location)%0A
     
@@ -1445,16 +1445,101 @@
 ocation%0A
+    if not os.path.exists(download_location):%0A        os.makedirs(download_location)%0A
     item

move download location check out of download pool