Example #1
0
def main(options, args):
    if not 1 == len(args):
        print "No filename or path to upload supplied:"
        parser.print_help()
        exit(1)

    flickr = authenticate_to_flickr()
    
    # Upload a single file
    if not options.recursive:
        if already_uploaded(flickr, args[0]):
            print "Already uploaded, skipping"
            exit(1)
        else:
            print "Uploading"
            upload_file(flickr, args[0])
            exit(0)

    # Use worker threads to upload a directory tree full of files
    queue = Queue()
    queue.done = False
    queue.count = itertools.count()

    def do_work():
       upload_from_queue(flickr, queue) 
    
    # Spawn workers
    workers = [Thread(target=do_work) for _ in range(options.workers)]
    for worker in workers:
        worker.start()

    # Use a watcher thread to print out status about tue queue
    def watch():
        print_status(queue)

    watcher = Thread(target=watch)
    watcher.daemon = True
    watcher.start()

    # Enqueue work for the workers
    for path in files_to_upload(flickr, args[0]):
        queue.put(path)
    queue.done = True

    # Wait for everything to finish
    queue.join()

    print "\n\n DONE"
    def start(self):

        print 'Start Speech Download Service...'

        language = self._language
        download_dir = self.setup_download_dir()

        # Create a queue to communicate with the worker threads
        queue = Queue()

        with open(self._text_file_path, 'r') as f:

            progress = Progress(len(f.readlines()))
            f.seek(0)
            queue.count = 0
            queue.progress = progress

            for text in f:
                text = text.rstrip('\n')
                encoded_args = urlencode({
                    'hl': language,
                    'src': text,
                    'key': config.API_KEY
                })

                url = config.SPEECH_URL + encoded_args
                download_path = os.path.join(download_dir, text + config.DOWNLOAD_FILE_TYPE)

                # queue accepts only one object
                queue.put(
                    (url, download_path)
                )

        # Create 8 worker threads
        for _ in range(self.THREAD_POOL_SIZE):
            thread = SpeechDownloadThread(queue)
            # Setting daemon to true will let the main thread exit
            # even though the workers are blocking
            thread.daemon = True
            thread.start()

        # Block until all tasks are done
        queue.join()

        print 'Done, Downloaded %d Speeches' % queue.count