Exemplo n.º 1
0
Arquivo: han.py Projeto: MSylvia/KSP
def assemble_sidecar(book, requested_guid, annotations_list):
	if not annotations_list:
		return None

	if requested_guid:
		guid = mobi.read_guid(book.file_path)
		if guid is None:
			logging.error("not a MOBI book? %s", book)
			return None

		rguid = requested_guid
		if ':' in rguid:
			_, _, rguid = rguid.partition(':')
		try:
			rguid = binascii.unhexlify(bytes(rguid, 'ascii'))
		except:
			logging.exception("failed to parse guid from %s, expected %s", requested_guid, guid)
			return None
		if guid != rguid:
			logging.error("requested guid %s does not match book guid %s", requested_guid, guid)
			return None

	sidecar_json = {
			'md5':'',
			'payload': {
				'acr': requested_guid or '',
				'guid': requested_guid or '',
				'key': book.asin,
				'type': 'EBOK',
				'records': [ _record(s) for s in annotations_list ]
			}
		}
	sidecar_json = json.dumps(sidecar_json, ensure_ascii=True, separators=(',', ':'))
	return ('application/json', bytes(sidecar_json, 'ascii'))
Exemplo n.º 2
0
Arquivo: mbp.py Projeto: nomada2/KSP
def assemble_sidecar(book, requested_guid, annotations_list):
	if not annotations_list:
		return None

	guid = mobi.read_guid(book.file_path)
	if guid is None:
		logging.error("not a MOBI book? %s", book)
		return None

	if requested_guid:
		rguid = requested_guid
		if ':' in rguid:
			_, _, rguid = rguid.partition(':')
		try:
			rguid = binascii.unhexlify(bytes(rguid, 'ascii'))
		except:
			logging.exception("failed to parse guid from %s, expected %s", requested_guid, guid)
			return None
		if guid != rguid:
			logging.error("requested guid %s does not match book guid %s", requested_guid, guid)
			return None

	# the annotations_list's first item is an optional last_read block
	# the rest of the entries are sorted by timestamp
	last_read = None if hasattr(annotations_list[0], 'kind') else annotations_list.pop(0)

	logging.debug("filtering annotations for guid %s", guid)
	# filter out entries whose guid does not match -- the clients will ignore them anyway
	annotations_list = [ a for a in annotations_list if a.state and a.state[28:32] == guid ]
	if not last_read and not annotations_list:
		return None

	sidecar_bytes = _build_sidecar(book, guid, last_read, annotations_list)
	return ('application/x-mobipocket-sidecar', sidecar_bytes)
Exemplo n.º 3
0
Arquivo: han.py Projeto: nomada2/KSP
def assemble_sidecar(book, requested_guid, annotations_list):
    if not annotations_list:
        return None

    if requested_guid:
        guid = mobi.read_guid(book.file_path)
        if guid is None:
            logging.error("not a MOBI book? %s", book)
            return None

        rguid = requested_guid
        if ':' in rguid:
            _, _, rguid = rguid.partition(':')
        try:
            rguid = binascii.unhexlify(bytes(rguid, 'ascii'))
        except:
            logging.exception("failed to parse guid from %s, expected %s",
                              requested_guid, guid)
            return None
        if guid != rguid:
            logging.error("requested guid %s does not match book guid %s",
                          requested_guid, guid)
            return None

    sidecar_json = {
        'md5': '',
        'payload': {
            'acr': requested_guid or '',
            'guid': requested_guid or '',
            'key': book.asin,
            'type': 'EBOK',
            'records': [_record(s) for s in annotations_list]
        }
    }
    sidecar_json = json.dumps(sidecar_json,
                              ensure_ascii=True,
                              separators=(',', ':'))
    return ('application/json', bytes(sidecar_json, 'ascii'))