lbry-sdk/lbrynet/schema/compat.py

89 lines
3.6 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-03-15 06:33:41 +01:00
from lbrynet.schema.types.v1.legacy_claim_pb2 import Claim as OldClaimMessage
2019-03-18 05:59:13 +01:00
from lbrynet.schema.types.v1.certificate_pb2 import KeyType
2019-03-15 06:33:41 +01:00
from lbrynet.schema.types.v1.fee_pb2 import Fee as FeeMessage
def from_old_json_schema(claim, payload: bytes):
value = json.loads(payload)
stream = claim.stream
stream.media_type = value.get('content_type', value.get('content-type', 'application/octet-stream'))
stream.title = value.get('title', '')
stream.description = value.get('description', '')
stream.thumbnail_url = value.get('thumbnail', '')
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
stream.sd_hash = value['sources']['lbry_sd_hash']
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-03-15 06:33:41 +01:00
if "fee" in value:
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 == 1:
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
stream.thumbnail_url = old.stream.metadata.thumbnail
if old.stream.metadata.HasField('language'):
stream.languages.add().message.language = old.stream.metadata.language
2019-03-15 06:33:41 +01:00
stream.media_type = old.stream.source.contentType
stream.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
elif old.claimType == 2:
channel = claim.channel
channel.public_key_bytes = old.certificate.publicKey
else:
2019-03-29 01:57:03 +01:00
raise DecodeError('claimType must be 1 for Streams and 2 for Channel')
2019-03-15 06:33:41 +01:00
return claim