#!/usr/bin/python
import re
import flickr_api
import threading

pattern = """(<a href=['"]http://mon-adresse-de-gallery/main.php\?g2_itemId=([0-9]+)['"]><img .+?alt=["'](.*?)['"].*?</a>)"""

blogexp = open('oltcuisine.wordpress.2015-06-04-with-gallery-links-fixed.xml', 'r')
picfile = open('ops.log', 'r')

flickridfile = open('flickrids.txt', 'r')


picmap = dict()
flickrids = dict()

for line in picfile:
	toks = line.split(" ")
	picmap[' '.join(toks[:-1])] = toks[-1:][0].rstrip()
picfile.close()

for line in flickridfile:
	elem = line.rstrip().split(" ")
	flickrids[elem[0]] = elem[1]

flickridfile.close()

flickr_api.set_auth_handler("python-flickr-api/flickr_token.txt")
flickridfile = open('flickrids.txt', 'a', 1)


extracted = []
for line in blogexp:
	matches = re.findall(pattern, line)
	for m in matches:
		extracted.append(picmap[m[0]])

workqueue = sorted(set(extracted))

lockQueue = threading.Lock()
lockFile = threading.Lock()
lockStdout = threading.Lock()

def upload_files(threadid):
	while(True):
		lockQueue.acquire()
		if(len(workqueue) == 0):
			lockQueue.release()
			break;
		item = workqueue.pop()
		lockQueue.release()
		
		#we don't put a lock here because the current file cannot be put by another thread
		if item not in flickrids:
			try:
				photo = flickr_api.upload(photo_file=item)
			except Exception:
				lockStdout.acquire()
        		        print threadid, ": ", item, "EXCEPTION"
		                lockStdout.release()
				continue
			photoid = photo.id
			lockFile.acquire()
			flickridfile.write(item + " " + photoid+"\n")
			lockFile.release()
			flickrids[item] = photoid
			lockStdout.acquire()
			print threadid, ": ", item, photoid
			lockStdout.release()
		else:
			photoid = flickrids[item]


threads = []
for i in range(15):
	t = threading.Thread(target=upload_files, args=(i,))
	threads.append(t)

for thread in threads:
	thread.start()

for thread in threads:
	thread.join()

blogexp.close()
flickridfile.close()

