lbry-sdk/lbry/schema/compat.py

94 lines
3.7 KiB
Python
Raw Normal View History

2019-03-15 06:33:41 +01:00
import json
from decimal import Decimal
2019-03-29 01:57:03 +01:00
from google.protobuf.message import DecodeError
2019-06-21 02:55:47 +02:00
from lbry.schema.types.v1.legacy_claim_pb2 import Claim as OldClaimMessage
from lbry.schema.types.v1.certificate_pb2 import KeyType
from lbry.schema.types.v1.fee_pb2 import Fee as FeeMessage
2019-03-15 06:33:41 +01:00
def from_old_json_schema(claim, payload: bytes):
2019-04-20 08:11:19 +02:00
try:
value = json.loads(payload)
except:
raise DecodeError('Could not parse JSON.')
2019-03-15 06:33:41 +01:00
stream = claim.stream
2019-04-15 21:25:09 +02:00
stream.source.sd_hash = value['sources']['lbry_sd_hash']
2019-05-21 00:18:27 +02:00
stream.source.media_type = (
value.get('content_type', value.get('content-type')) or
'application/octet-stream'
)
2019-03-15 06:33:41 +01:00
stream.title = value.get('title', '')
stream.description = value.get('description', '')
2019-04-15 21:25:09 +02:00
if value.get('thumbnail', ''):
stream.thumbnail.url = value.get('thumbnail', '')
2019-03-15 06:33:41 +01:00
stream.author = value.get('author', '')
stream.license = value.get('license', '')
stream.license_url = value.get('license_url', '')
language = value.get('language', '')
if language:
if language.lower() == 'english':
language = 'en'
try:
stream.languages.append(language)
except:
pass
2019-03-15 06:33:41 +01:00
if value.get('nsfw', False):
2019-03-20 06:46:23 +01:00
stream.tags.append('mature')
2019-07-07 01:58:02 +02:00
if "fee" in value and isinstance(value['fee'], dict):
2019-03-15 06:33:41 +01:00
fee = value["fee"]
currency = list(fee.keys())[0]
if currency == 'LBC':
stream.fee.lbc = Decimal(fee[currency]['amount'])
elif currency == 'USD':
stream.fee.usd = Decimal(fee[currency]['amount'])
elif currency == 'BTC':
stream.fee.btc = Decimal(fee[currency]['amount'])
2019-03-15 06:33:41 +01:00
else:
2019-03-29 01:57:03 +01:00
raise DecodeError(f'Unknown currency: {currency}')
2019-03-15 06:33:41 +01:00
stream.fee.address = fee[currency]['address']
return claim
def from_types_v1(claim, payload: bytes):
old = OldClaimMessage()
old.ParseFromString(payload)
if old.claimType == 2:
channel = claim.channel
channel.public_key_bytes = old.certificate.publicKey
else:
2019-03-15 06:33:41 +01:00
stream = claim.stream
stream.title = old.stream.metadata.title
stream.description = old.stream.metadata.description
stream.author = old.stream.metadata.author
stream.license = old.stream.metadata.license
stream.license_url = old.stream.metadata.licenseUrl
2019-04-15 21:25:09 +02:00
stream.thumbnail.url = old.stream.metadata.thumbnail
if old.stream.metadata.HasField('language'):
stream.languages.add().message.language = old.stream.metadata.language
2019-04-15 21:25:09 +02:00
stream.source.media_type = old.stream.source.contentType
stream.source.sd_hash_bytes = old.stream.source.source
2019-03-15 06:33:41 +01:00
if old.stream.metadata.nsfw:
2019-03-20 06:46:23 +01:00
stream.tags.append('mature')
2019-03-15 06:33:41 +01:00
if old.stream.metadata.HasField('fee'):
fee = old.stream.metadata.fee
stream.fee.address_bytes = fee.address
currency = FeeMessage.Currency.Name(fee.currency)
if currency == 'LBC':
stream.fee.lbc = Decimal(fee.amount)
elif currency == 'USD':
stream.fee.usd = Decimal(fee.amount)
elif currency == 'BTC':
stream.fee.btc = Decimal(fee.amount)
2019-03-15 06:33:41 +01:00
else:
2019-03-29 01:57:03 +01:00
raise DecodeError(f'Unsupported currency: {currency}')
2019-03-18 05:59:13 +01:00
if old.HasField('publisherSignature'):
sig = old.publisherSignature
claim.signature = sig.signature
claim.signature_type = KeyType.Name(sig.signatureType)
2019-04-04 05:15:16 +02:00
claim.signing_channel_hash = sig.certificateId[::-1]
2019-03-18 05:59:13 +01:00
old.ClearField("publisherSignature")
claim.unsigned_payload = old.SerializeToString()
2019-03-15 06:33:41 +01:00
return claim