Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
languagepy
titleon_message
def on_message(channel, method, header, body):
    """When message is received do the following steps:
    1. download the file
    2. launch extractor function"""

    global logger, extractorName
    inputfile=None
    fileid=0

    try:
        # parse body back from json
        jbody=json.loads(body)
        host=jbody['host']
        fileid=jbody['id']
        intermediatefileid=jbody['intermediateId']
        if not (host.endswith('/')):
            host += '/'
        
         # printtell whateverybody we are doing starting to process the file
        logger.debug("[%s] started processing", fileid)
status_update(channel, header, fileid, "Started processing file")

        # download file
        inputfile = download_file(channel, header, host, secretKey, fileid, intermediatefileid)
        # call actual extractor function
        process_file(channel, header, host, secretKey, fileid, intermediatefileid, inputfile)
 
        # notify rabbitMQ we are done processsing message
        channel.basic_ack(method.delivery_tag)
    except subprocess.CalledProcessError as e:
        msg = str.format("Error processing [exit code=%d]\n%s", e.returncode, e.output)
        logger.exception("[%s] %s", fileid, msg)
        status_update(channel, header, fileid, msg)
    except:
        logger.exception("[%s] error processing", fileid)
        status_update(channel, header, fileid, "Error processing")
    finally:
        status_update(channel, header, fileid, "Done")
        if inputfile is not None:
            try:
                os.remove(inputfile)
            except OSError:
                pass
            except UnboundLocalError:
                pass

...

Code Block
languagepy
titleprocess_file
def process_file(channel, header, host, key, fileid, intermediatefileid, inputfile):
    """Count the number of words in text file"""
    status_update(channel, header, fileid, "Counting words in file.")
    # call actual program
    result = subprocess.check_output(['wc', inputfile], stderr=subprocess.STDOUT)
    (lines, words, characters, filename) = result.split()

    # store results as metadata
    metadata={}
    metadata['lines']=lines
    metadata['words']=words
    metadata['characters']=characters
    headers={'Content-Type': 'application/json'}
    r = requests.post('%sapi/files/%s/metadata?key=%s' % (host, fileid, key),
                      headers=headers,
                      data=json.dumps(metadata));
    r.raise_for_status()

...

The following code will process an incoming video and upload a preview of the video

Code Block
languagepy
titlecreate_image_thumbnail
def create_image_thumbnail(inputfile, ext, size, host, fileid, *args):
	global logger

	(fd, thumbnailfile)=tempfile.mkstemp(suffix='.' + ext)
	try:
		# convert image to right size
		subprocess.check_output(['convert',  inputfile, '-resize', size, thumbnailfile], stderr=subprocess.STDOUT)

		if(os.path.getsize(thumbnailfile) == 0):
			raise Exception("File is empty.")

		# upload preview image
		url=host + 'api/fileThumbnail?key=' + playserverKey
		r = requests.post(url, files={"File" : open(thumbnailfile, 'rb')})
		r.raise_for_status()
		thumbnailid = r.json()['id']

		# associate uploaded thumbnail with original file
		url=host + 'api/files/' + fileid + '/thumbnails/' + thumbnailid + '?key=' + playserverKey
		r = requests.post(url);
		r.raise_for_status()

		logger.debug("[%s] created thumbnail of type %s", fileid, ext)
	finally:
		try:
			os.remove(thumbnailfile)
		except:
			pass