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


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

flickrids = dict()

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")

workqueue = flickrids.items()
workqueue[0] = ("plop", "plop123456789")
print workqueue
queueLock = threading.Lock()
stdoutLock = threading.Lock()

def check_upload():
	while(True):
		queueLock.acquire()
		if(len(workqueue) == 0):
			queueLock.release()
			break
		(key, value) = workqueue.pop()		
		stdoutLock.acquire()
		print value
		stdoutLock.release()
		queueLock.release()
		photo = flickr_api.Photo(id=value)
		try:
			photo.getInfo()
		except Exception:
			stdoutLock.acquire()
			print "ERROR", key, value
			stdoutLock.release()
			continue

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

for thread in threads:
        thread.start()

for thread in threads:
        thread.join()

