2019-03-24 21:55:04 +01:00
|
|
|
import os.path
|
2019-03-30 01:26:10 +01:00
|
|
|
import json
|
|
|
|
from string import ascii_letters
|
|
|
|
from typing import List, Tuple, Iterator, TypeVar, Generic
|
2019-03-15 06:33:41 +01:00
|
|
|
from decimal import Decimal
|
|
|
|
from binascii import hexlify, unhexlify
|
2019-03-14 00:47:13 +01:00
|
|
|
|
2019-03-22 07:18:34 +01:00
|
|
|
from google.protobuf.json_format import MessageToDict
|
2019-03-20 06:46:23 +01:00
|
|
|
from google.protobuf.message import DecodeError
|
2019-03-24 21:55:04 +01:00
|
|
|
from hachoir.parser import createParser as binary_file_parser
|
|
|
|
from hachoir.metadata import extractMetadata as binary_file_metadata
|
|
|
|
from hachoir.core.log import log as hachoir_log
|
2018-09-17 22:13:30 +02:00
|
|
|
|
2019-03-20 06:46:23 +01:00
|
|
|
from torba.client.hash import Base58
|
2019-03-14 00:47:13 +01:00
|
|
|
from torba.client.constants import COIN
|
2018-09-17 22:13:30 +02:00
|
|
|
|
2019-03-15 06:33:41 +01:00
|
|
|
from lbrynet.schema import compat
|
2019-03-20 06:46:23 +01:00
|
|
|
from lbrynet.schema.base import Signable
|
2019-03-24 21:55:04 +01:00
|
|
|
from lbrynet.schema.mime_types import guess_media_type
|
2019-03-28 01:32:43 +01:00
|
|
|
from lbrynet.schema.types.v2.claim_pb2 import (
|
|
|
|
Claim as ClaimMessage,
|
|
|
|
Fee as FeeMessage,
|
|
|
|
Location as LocationMessage,
|
|
|
|
Language as LanguageMessage
|
|
|
|
)
|
2019-03-24 21:55:04 +01:00
|
|
|
|
|
|
|
|
|
|
|
hachoir_log.use_print = False
|
2018-09-17 22:13:30 +02:00
|
|
|
|
|
|
|
|
2019-03-20 06:46:23 +01:00
|
|
|
class Claim(Signable):
|
2018-09-17 22:13:30 +02:00
|
|
|
|
2019-03-20 06:46:23 +01:00
|
|
|
__slots__ = 'version',
|
|
|
|
message_class = ClaimMessage
|
2019-03-15 06:33:41 +01:00
|
|
|
|
|
|
|
def __init__(self, claim_message=None):
|
2019-03-20 06:46:23 +01:00
|
|
|
super().__init__(claim_message)
|
|
|
|
self.version = 2
|
2019-03-14 00:47:13 +01:00
|
|
|
|
2019-03-15 06:33:41 +01:00
|
|
|
@property
|
|
|
|
def is_stream(self):
|
2019-03-20 06:46:23 +01:00
|
|
|
return self.message.WhichOneof('type') == 'stream'
|
2019-03-14 00:47:13 +01:00
|
|
|
|
2019-03-15 06:33:41 +01:00
|
|
|
@property
|
|
|
|
def is_channel(self):
|
2019-03-20 06:46:23 +01:00
|
|
|
return self.message.WhichOneof('type') == 'channel'
|
2019-03-18 05:59:13 +01:00
|
|
|
|
2019-03-15 06:33:41 +01:00
|
|
|
@property
|
|
|
|
def stream_message(self):
|
|
|
|
if self.is_undetermined:
|
2019-03-20 06:46:23 +01:00
|
|
|
self.message.stream.SetInParent()
|
2019-03-15 06:33:41 +01:00
|
|
|
if not self.is_stream:
|
|
|
|
raise ValueError('Claim is not a stream.')
|
2019-03-20 06:46:23 +01:00
|
|
|
return self.message.stream
|
2019-03-15 06:33:41 +01:00
|
|
|
|
|
|
|
@property
|
|
|
|
def stream(self) -> 'Stream':
|
|
|
|
return Stream(self)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def channel_message(self):
|
|
|
|
if self.is_undetermined:
|
2019-03-20 06:46:23 +01:00
|
|
|
self.message.channel.SetInParent()
|
2019-03-15 06:33:41 +01:00
|
|
|
if not self.is_channel:
|
|
|
|
raise ValueError('Claim is not a channel.')
|
2019-03-20 06:46:23 +01:00
|
|
|
return self.message.channel
|
2019-03-15 06:33:41 +01:00
|
|
|
|
|
|
|
@property
|
|
|
|
def channel(self) -> 'Channel':
|
|
|
|
return Channel(self)
|
|
|
|
|
2019-03-24 21:55:04 +01:00
|
|
|
def to_dict(self):
|
|
|
|
return MessageToDict(self.message, preserving_proto_field_name=True)
|
|
|
|
|
2019-03-14 00:47:13 +01:00
|
|
|
@classmethod
|
2019-03-15 06:33:41 +01:00
|
|
|
def from_bytes(cls, data: bytes) -> 'Claim':
|
2019-03-20 06:46:23 +01:00
|
|
|
try:
|
|
|
|
return super().from_bytes(data)
|
|
|
|
except DecodeError:
|
|
|
|
claim = cls()
|
|
|
|
if data[0] == ord('{'):
|
|
|
|
claim.version = 0
|
|
|
|
compat.from_old_json_schema(claim, data)
|
|
|
|
elif data[0] not in (0, 1):
|
|
|
|
claim.version = 1
|
|
|
|
compat.from_types_v1(claim, data)
|
|
|
|
else:
|
|
|
|
raise
|
|
|
|
return claim
|
2019-03-15 06:33:41 +01:00
|
|
|
|
|
|
|
|
2019-03-30 01:26:10 +01:00
|
|
|
I = TypeVar('I')
|
|
|
|
|
|
|
|
|
|
|
|
class BaseMessageList(Generic[I]):
|
|
|
|
|
|
|
|
__slots__ = 'message',
|
|
|
|
|
|
|
|
item_class = None
|
|
|
|
|
|
|
|
def __init__(self, message):
|
|
|
|
self.message = message
|
|
|
|
|
|
|
|
def add(self) -> I:
|
|
|
|
return self.item_class(self.message.add())
|
|
|
|
|
|
|
|
def extend(self, values: List[str]):
|
|
|
|
for value in values:
|
|
|
|
self.append(value)
|
|
|
|
|
|
|
|
def append(self, value: str):
|
|
|
|
raise NotImplemented
|
|
|
|
|
|
|
|
def __len__(self):
|
|
|
|
return len(self.message)
|
|
|
|
|
|
|
|
def __iter__(self) -> Iterator[I]:
|
|
|
|
for lang in self.message:
|
|
|
|
yield self.item_class(lang)
|
|
|
|
|
|
|
|
def __getitem__(self, item) -> I:
|
|
|
|
return self.item_class(self.message[item])
|
|
|
|
|
|
|
|
|
2019-03-25 17:30:30 +01:00
|
|
|
class Dimmensional:
|
2019-03-15 06:33:41 +01:00
|
|
|
|
2019-03-25 17:30:30 +01:00
|
|
|
__slots__ = ()
|
2019-03-15 06:33:41 +01:00
|
|
|
|
|
|
|
@property
|
|
|
|
def width(self) -> int:
|
2019-03-25 17:30:30 +01:00
|
|
|
return self.message.width
|
2019-03-15 06:33:41 +01:00
|
|
|
|
|
|
|
@width.setter
|
|
|
|
def width(self, width: int):
|
2019-03-25 17:30:30 +01:00
|
|
|
self.message.width = width
|
2019-03-15 06:33:41 +01:00
|
|
|
|
|
|
|
@property
|
|
|
|
def height(self) -> int:
|
2019-03-25 17:30:30 +01:00
|
|
|
return self.message.height
|
2019-03-15 06:33:41 +01:00
|
|
|
|
|
|
|
@height.setter
|
|
|
|
def height(self, height: int):
|
2019-03-25 17:30:30 +01:00
|
|
|
self.message.height = height
|
2019-03-15 06:33:41 +01:00
|
|
|
|
|
|
|
@property
|
|
|
|
def dimensions(self) -> Tuple[int, int]:
|
|
|
|
return self.width, self.height
|
|
|
|
|
|
|
|
@dimensions.setter
|
|
|
|
def dimensions(self, dimensions: Tuple[int, int]):
|
2019-03-25 17:30:30 +01:00
|
|
|
self.message.width, self.message.height = dimensions
|
|
|
|
|
|
|
|
|
|
|
|
class Playable:
|
|
|
|
|
|
|
|
__slots__ = ()
|
|
|
|
|
|
|
|
@property
|
|
|
|
def duration(self) -> int:
|
|
|
|
return self.message.duration
|
|
|
|
|
|
|
|
@duration.setter
|
|
|
|
def duration(self, duration: int):
|
|
|
|
self.message.duration = duration
|
|
|
|
|
|
|
|
def set_duration_from_path(self, file_path):
|
|
|
|
try:
|
|
|
|
file_metadata = binary_file_metadata(binary_file_parser(file_path))
|
|
|
|
self.duration = file_metadata.getValues('duration')[0].seconds
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class Image(Dimmensional):
|
|
|
|
|
|
|
|
__slots__ = 'message',
|
|
|
|
|
|
|
|
def __init__(self, image_message):
|
|
|
|
self.message = image_message
|
|
|
|
|
|
|
|
|
|
|
|
class Video(Dimmensional, Playable):
|
|
|
|
|
|
|
|
__slots__ = 'message',
|
|
|
|
|
|
|
|
def __init__(self, video_message):
|
|
|
|
self.message = video_message
|
|
|
|
|
|
|
|
|
|
|
|
class Audio(Playable):
|
|
|
|
|
|
|
|
__slots__ = 'message',
|
|
|
|
|
|
|
|
def __init__(self, audio_message):
|
|
|
|
self.message = audio_message
|
2019-03-15 06:33:41 +01:00
|
|
|
|
|
|
|
|
|
|
|
class File:
|
|
|
|
|
|
|
|
__slots__ = '_file',
|
|
|
|
|
|
|
|
def __init__(self, file_message):
|
|
|
|
self._file = file_message
|
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self) -> str:
|
|
|
|
return self._file.name
|
|
|
|
|
|
|
|
@name.setter
|
|
|
|
def name(self, name: str):
|
|
|
|
self._file.name = name
|
|
|
|
|
|
|
|
@property
|
|
|
|
def size(self) -> int:
|
|
|
|
return self._file.size
|
|
|
|
|
|
|
|
@size.setter
|
|
|
|
def size(self, size: int):
|
|
|
|
self._file.size = size
|
|
|
|
|
|
|
|
|
|
|
|
class Fee:
|
|
|
|
|
|
|
|
__slots__ = '_fee',
|
|
|
|
|
|
|
|
def __init__(self, fee_message):
|
|
|
|
self._fee = fee_message
|
|
|
|
|
|
|
|
@property
|
|
|
|
def currency(self) -> str:
|
|
|
|
return FeeMessage.Currency.Name(self._fee.currency)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def address(self) -> str:
|
2019-03-20 06:46:23 +01:00
|
|
|
return Base58.encode(self._fee.address)
|
2019-03-15 06:33:41 +01:00
|
|
|
|
|
|
|
@address.setter
|
|
|
|
def address(self, address: str):
|
2019-03-20 06:46:23 +01:00
|
|
|
self._fee.address = Base58.decode(address)
|
2019-03-15 06:33:41 +01:00
|
|
|
|
|
|
|
@property
|
|
|
|
def address_bytes(self) -> bytes:
|
|
|
|
return self._fee.address
|
|
|
|
|
|
|
|
@address_bytes.setter
|
|
|
|
def address_bytes(self, address: bytes):
|
|
|
|
self._fee.address = address
|
|
|
|
|
2019-03-15 06:41:22 +01:00
|
|
|
@property
|
|
|
|
def amount(self) -> Decimal:
|
|
|
|
if self.currency == 'LBC':
|
|
|
|
return self.lbc
|
2019-03-30 01:26:10 +01:00
|
|
|
if self.currency == 'BTC':
|
|
|
|
return self.btc
|
2019-03-15 06:41:22 +01:00
|
|
|
if self.currency == 'USD':
|
|
|
|
return self.usd
|
|
|
|
|
2019-03-15 14:05:14 +01:00
|
|
|
DEWIES = Decimal(COIN)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def lbc(self) -> Decimal:
|
|
|
|
if self._fee.currency != FeeMessage.LBC:
|
|
|
|
raise ValueError('LBC can only be returned for LBC fees.')
|
|
|
|
return Decimal(self._fee.amount / self.DEWIES)
|
|
|
|
|
|
|
|
@lbc.setter
|
|
|
|
def lbc(self, amount: Decimal):
|
|
|
|
self.dewies = int(amount * self.DEWIES)
|
|
|
|
|
2019-03-15 06:33:41 +01:00
|
|
|
@property
|
|
|
|
def dewies(self) -> int:
|
|
|
|
if self._fee.currency != FeeMessage.LBC:
|
|
|
|
raise ValueError('Dewies can only be returned for LBC fees.')
|
|
|
|
return self._fee.amount
|
|
|
|
|
|
|
|
@dewies.setter
|
|
|
|
def dewies(self, amount: int):
|
|
|
|
self._fee.amount = amount
|
|
|
|
self._fee.currency = FeeMessage.LBC
|
|
|
|
|
2019-03-30 01:26:10 +01:00
|
|
|
SATOSHIES = Decimal(COIN)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def btc(self) -> Decimal:
|
|
|
|
if self._fee.currency != FeeMessage.BTC:
|
|
|
|
raise ValueError('BTC can only be returned for BTC fees.')
|
|
|
|
return Decimal(self._fee.amount / self.SATOSHIES)
|
|
|
|
|
|
|
|
@btc.setter
|
|
|
|
def btc(self, amount: Decimal):
|
|
|
|
self.satoshis = int(amount * self.SATOSHIES)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def satoshis(self) -> int:
|
|
|
|
if self._fee.currency != FeeMessage.BTC:
|
|
|
|
raise ValueError('Satoshies can only be returned for BTC fees.')
|
|
|
|
return self._fee.amount
|
|
|
|
|
|
|
|
@satoshis.setter
|
|
|
|
def satoshis(self, amount: int):
|
|
|
|
self._fee.amount = amount
|
|
|
|
self._fee.currency = FeeMessage.BTC
|
|
|
|
|
2019-03-15 14:05:14 +01:00
|
|
|
PENNIES = Decimal(100.0)
|
2019-03-15 06:33:41 +01:00
|
|
|
|
|
|
|
@property
|
|
|
|
def usd(self) -> Decimal:
|
|
|
|
if self._fee.currency != FeeMessage.USD:
|
|
|
|
raise ValueError('USD can only be returned for USD fees.')
|
2019-03-15 14:05:14 +01:00
|
|
|
return Decimal(self._fee.amount / self.PENNIES)
|
2019-03-15 06:33:41 +01:00
|
|
|
|
|
|
|
@usd.setter
|
|
|
|
def usd(self, amount: Decimal):
|
2019-03-15 14:05:14 +01:00
|
|
|
self.pennies = int(amount * self.PENNIES)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def pennies(self) -> int:
|
|
|
|
if self._fee.currency != FeeMessage.USD:
|
|
|
|
raise ValueError('Pennies can only be returned for USD fees.')
|
|
|
|
return self._fee.amount
|
|
|
|
|
|
|
|
@pennies.setter
|
|
|
|
def pennies(self, amount: int):
|
|
|
|
self._fee.amount = amount
|
2019-03-15 06:33:41 +01:00
|
|
|
self._fee.currency = FeeMessage.USD
|
|
|
|
|
|
|
|
|
2019-03-30 01:26:10 +01:00
|
|
|
class Language:
|
|
|
|
|
|
|
|
__slots__ = 'message',
|
|
|
|
|
|
|
|
def __init__(self, message):
|
|
|
|
self.message = message
|
|
|
|
|
|
|
|
@property
|
|
|
|
def langtag(self) -> str:
|
|
|
|
langtag = []
|
|
|
|
if self.language:
|
|
|
|
langtag.append(self.language)
|
|
|
|
if self.script:
|
|
|
|
langtag.append(self.script)
|
|
|
|
if self.region:
|
|
|
|
langtag.append(self.region)
|
|
|
|
return '-'.join(langtag)
|
|
|
|
|
|
|
|
@langtag.setter
|
|
|
|
def langtag(self, langtag: str):
|
|
|
|
parts = langtag.split('-')
|
|
|
|
self.language = parts.pop(0)
|
|
|
|
if parts and len(parts[0]) == 4:
|
|
|
|
self.script = parts.pop(0)
|
|
|
|
if parts and len(parts[0]) == 2:
|
|
|
|
self.region = parts.pop(0)
|
|
|
|
assert not parts, f"Failed to parse language tag: {langtag}"
|
|
|
|
|
|
|
|
@property
|
|
|
|
def language(self) -> str:
|
|
|
|
if self.message.language:
|
|
|
|
return LanguageMessage.Language.Name(self.message.language)
|
|
|
|
|
|
|
|
@language.setter
|
|
|
|
def language(self, language: str):
|
|
|
|
self.message.language = LanguageMessage.Language.Value(language)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def script(self) -> str:
|
|
|
|
if self.message.script:
|
|
|
|
return LanguageMessage.Script.Name(self.message.script)
|
|
|
|
|
|
|
|
@script.setter
|
|
|
|
def script(self, script: str):
|
|
|
|
self.message.script = LanguageMessage.Script.Value(script)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def region(self) -> str:
|
|
|
|
if self.message.region:
|
|
|
|
return LocationMessage.Country.Name(self.message.region)
|
|
|
|
|
|
|
|
@region.setter
|
|
|
|
def region(self, region: str):
|
|
|
|
self.message.region = LocationMessage.Country.Value(region)
|
|
|
|
|
|
|
|
|
|
|
|
class LanguageList(BaseMessageList[Language]):
|
|
|
|
__slots__ = ()
|
|
|
|
item_class = Language
|
|
|
|
|
|
|
|
def append(self, value: str):
|
|
|
|
self.add().langtag = value
|
|
|
|
|
|
|
|
|
|
|
|
class Location:
|
|
|
|
|
|
|
|
__slots__ = 'message',
|
|
|
|
|
|
|
|
def __init__(self, message):
|
|
|
|
self.message = message
|
|
|
|
|
|
|
|
def from_value(self, value):
|
|
|
|
if isinstance(value, str) and value.startswith('{'):
|
|
|
|
value = json.loads(value)
|
|
|
|
|
|
|
|
if isinstance(value, dict):
|
|
|
|
for key, val in value.items():
|
|
|
|
setattr(self, key, val)
|
|
|
|
|
|
|
|
elif isinstance(value, str):
|
|
|
|
parts = value.split(':')
|
|
|
|
if len(parts) > 2 or (parts[0] and parts[0][0] in ascii_letters):
|
|
|
|
country = parts and parts.pop(0)
|
|
|
|
if country:
|
|
|
|
self.country = country
|
|
|
|
state = parts and parts.pop(0)
|
|
|
|
if state:
|
|
|
|
self.state = state
|
|
|
|
city = parts and parts.pop(0)
|
|
|
|
if city:
|
|
|
|
self.city = city
|
|
|
|
code = parts and parts.pop(0)
|
|
|
|
if code:
|
|
|
|
self.code = code
|
|
|
|
latitude = parts and parts.pop(0)
|
|
|
|
if latitude:
|
|
|
|
self.latitude = latitude
|
|
|
|
longitude = parts and parts.pop(0)
|
|
|
|
if longitude:
|
|
|
|
self.longitude = longitude
|
|
|
|
|
|
|
|
else:
|
|
|
|
raise ValueError(f'Could not parse country value: {value}')
|
|
|
|
|
|
|
|
@property
|
|
|
|
def country(self) -> str:
|
|
|
|
if self.message.country:
|
|
|
|
return LocationMessage.Country.Name(self.message.country)
|
|
|
|
|
|
|
|
@country.setter
|
|
|
|
def country(self, country: str):
|
|
|
|
self.message.country = LocationMessage.Country.Value(country)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def state(self) -> str:
|
|
|
|
return self.message.state
|
|
|
|
|
|
|
|
@state.setter
|
|
|
|
def state(self, state: str):
|
|
|
|
self.message.state = state
|
|
|
|
|
|
|
|
@property
|
|
|
|
def city(self) -> str:
|
|
|
|
return self.message.city
|
|
|
|
|
|
|
|
@city.setter
|
|
|
|
def city(self, city: str):
|
|
|
|
self.message.city = city
|
|
|
|
|
|
|
|
@property
|
|
|
|
def code(self) -> str:
|
|
|
|
return self.message.code
|
|
|
|
|
|
|
|
@code.setter
|
|
|
|
def code(self, code: str):
|
|
|
|
self.message.code = code
|
|
|
|
|
|
|
|
GPS_PRECISION = Decimal('10000000')
|
|
|
|
|
|
|
|
@property
|
|
|
|
def latitude(self) -> str:
|
|
|
|
if self.message.latitude:
|
|
|
|
return str(Decimal(self.message.latitude) / self.GPS_PRECISION)
|
|
|
|
|
|
|
|
@latitude.setter
|
|
|
|
def latitude(self, latitude: str):
|
|
|
|
latitude = Decimal(latitude)
|
|
|
|
assert -90 <= latitude <= 90, "Latitude must be between -90 and 90 degrees."
|
|
|
|
self.message.latitude = int(latitude * self.GPS_PRECISION)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def longitude(self) -> str:
|
|
|
|
if self.message.longitude:
|
|
|
|
return str(Decimal(self.message.longitude) / self.GPS_PRECISION)
|
|
|
|
|
|
|
|
@longitude.setter
|
|
|
|
def longitude(self, longitude: str):
|
|
|
|
longitude = Decimal(longitude)
|
|
|
|
assert -180 <= longitude <= 180, "Longitude must be between -180 and 180 degrees."
|
|
|
|
self.message.longitude = int(longitude * self.GPS_PRECISION)
|
|
|
|
|
|
|
|
|
|
|
|
class LocationList(BaseMessageList[Location]):
|
|
|
|
__slots__ = ()
|
|
|
|
item_class = Location
|
|
|
|
|
|
|
|
def append(self, value):
|
|
|
|
self.add().from_value(value)
|
|
|
|
|
|
|
|
|
2019-03-24 21:55:04 +01:00
|
|
|
class BaseClaimSubType:
|
2019-03-18 05:59:13 +01:00
|
|
|
|
2019-03-24 21:55:04 +01:00
|
|
|
__slots__ = 'claim', 'message'
|
2019-03-18 05:59:13 +01:00
|
|
|
|
2019-03-24 21:55:04 +01:00
|
|
|
def __init__(self, claim: Claim):
|
|
|
|
self.claim = claim or Claim()
|
2019-03-22 07:18:34 +01:00
|
|
|
|
2019-03-18 05:59:13 +01:00
|
|
|
@property
|
2019-03-24 21:55:04 +01:00
|
|
|
def title(self) -> str:
|
|
|
|
return self.message.title
|
|
|
|
|
|
|
|
@title.setter
|
|
|
|
def title(self, title: str):
|
|
|
|
self.message.title = title
|
2019-03-18 05:59:13 +01:00
|
|
|
|
|
|
|
@property
|
2019-03-24 21:55:04 +01:00
|
|
|
def description(self) -> str:
|
|
|
|
return self.message.description
|
|
|
|
|
|
|
|
@description.setter
|
|
|
|
def description(self, description: str):
|
|
|
|
self.message.description = description
|
2019-03-18 05:59:13 +01:00
|
|
|
|
|
|
|
@property
|
2019-03-24 21:55:04 +01:00
|
|
|
def thumbnail_url(self) -> str:
|
|
|
|
return self.message.thumbnail_url
|
2019-03-18 05:59:13 +01:00
|
|
|
|
2019-03-24 21:55:04 +01:00
|
|
|
@thumbnail_url.setter
|
|
|
|
def thumbnail_url(self, thumbnail_url: str):
|
|
|
|
self.message.thumbnail_url = thumbnail_url
|
2019-03-18 05:59:13 +01:00
|
|
|
|
|
|
|
@property
|
2019-03-30 01:26:10 +01:00
|
|
|
def tags(self) -> List:
|
|
|
|
return self.message.tags
|
2019-03-28 01:32:43 +01:00
|
|
|
|
|
|
|
@property
|
2019-03-30 01:26:10 +01:00
|
|
|
def languages(self) -> LanguageList:
|
|
|
|
return LanguageList(self.message.languages)
|
2019-03-28 01:32:43 +01:00
|
|
|
|
|
|
|
@property
|
2019-03-30 01:26:10 +01:00
|
|
|
def langtags(self) -> List[str]:
|
|
|
|
return [l.langtag for l in self.languages]
|
2019-03-28 01:32:43 +01:00
|
|
|
|
|
|
|
@property
|
2019-03-30 01:26:10 +01:00
|
|
|
def locations(self) -> LocationList:
|
|
|
|
return LocationList(self.message.locations)
|
2019-03-18 05:59:13 +01:00
|
|
|
|
2019-03-24 21:55:04 +01:00
|
|
|
def to_dict(self):
|
|
|
|
return MessageToDict(self.message, preserving_proto_field_name=True)
|
|
|
|
|
2019-03-30 01:26:10 +01:00
|
|
|
def update(self, **kwargs):
|
|
|
|
for l in ('tags', 'languages', 'locations'):
|
|
|
|
if kwargs.pop(f'clear_{l}', False):
|
|
|
|
self.message.ClearField('tags')
|
|
|
|
items = kwargs.pop(l, None)
|
|
|
|
if items is not None:
|
|
|
|
if isinstance(items, str):
|
|
|
|
getattr(self, l).append(items)
|
|
|
|
elif isinstance(items, list):
|
|
|
|
getattr(self, l).extend(items)
|
|
|
|
else:
|
|
|
|
raise ValueError(f"Unknown {l} value: {items}")
|
2019-03-24 21:55:04 +01:00
|
|
|
|
|
|
|
for key, value in kwargs.items():
|
|
|
|
setattr(self, key, value)
|
|
|
|
|
|
|
|
|
|
|
|
class Channel(BaseClaimSubType):
|
|
|
|
|
|
|
|
__slots__ = ()
|
|
|
|
|
|
|
|
def __init__(self, claim: Claim = None):
|
|
|
|
super().__init__(claim)
|
|
|
|
self.message = self.claim.channel_message
|
2019-03-18 05:59:13 +01:00
|
|
|
|
|
|
|
@property
|
2019-03-24 21:55:04 +01:00
|
|
|
def public_key(self) -> str:
|
|
|
|
return hexlify(self.message.public_key).decode()
|
2019-03-18 05:59:13 +01:00
|
|
|
|
2019-03-24 21:55:04 +01:00
|
|
|
@public_key.setter
|
|
|
|
def public_key(self, sd_public_key: str):
|
|
|
|
self.message.public_key = unhexlify(sd_public_key.encode())
|
2019-03-18 05:59:13 +01:00
|
|
|
|
|
|
|
@property
|
2019-03-24 21:55:04 +01:00
|
|
|
def public_key_bytes(self) -> bytes:
|
|
|
|
return self.message.public_key
|
2019-03-18 05:59:13 +01:00
|
|
|
|
2019-03-24 21:55:04 +01:00
|
|
|
@public_key_bytes.setter
|
|
|
|
def public_key_bytes(self, public_key: bytes):
|
|
|
|
self.message.public_key = public_key
|
2019-03-18 05:59:13 +01:00
|
|
|
|
|
|
|
@property
|
|
|
|
def contact_email(self) -> str:
|
2019-03-24 21:55:04 +01:00
|
|
|
return self.message.contact_email
|
2019-03-18 05:59:13 +01:00
|
|
|
|
|
|
|
@contact_email.setter
|
|
|
|
def contact_email(self, contact_email: str):
|
2019-03-24 21:55:04 +01:00
|
|
|
self.message.contact_email = contact_email
|
2019-03-18 05:59:13 +01:00
|
|
|
|
|
|
|
@property
|
|
|
|
def homepage_url(self) -> str:
|
2019-03-24 21:55:04 +01:00
|
|
|
return self.message.homepage_url
|
2019-03-18 05:59:13 +01:00
|
|
|
|
|
|
|
@homepage_url.setter
|
|
|
|
def homepage_url(self, homepage_url: str):
|
2019-03-24 21:55:04 +01:00
|
|
|
self.message.homepage_url = homepage_url
|
2019-03-18 05:59:13 +01:00
|
|
|
|
|
|
|
@property
|
|
|
|
def cover_url(self) -> str:
|
2019-03-24 21:55:04 +01:00
|
|
|
return self.message.cover_url
|
2019-03-18 05:59:13 +01:00
|
|
|
|
|
|
|
@cover_url.setter
|
|
|
|
def cover_url(self, cover_url: str):
|
2019-03-24 21:55:04 +01:00
|
|
|
self.message.cover_url = cover_url
|
2019-03-18 05:59:13 +01:00
|
|
|
|
|
|
|
|
2019-03-24 21:55:04 +01:00
|
|
|
class Stream(BaseClaimSubType):
|
2019-03-15 06:33:41 +01:00
|
|
|
|
2019-03-24 21:55:04 +01:00
|
|
|
__slots__ = ()
|
2019-03-15 06:33:41 +01:00
|
|
|
|
|
|
|
def __init__(self, claim: Claim = None):
|
2019-03-24 21:55:04 +01:00
|
|
|
super().__init__(claim)
|
|
|
|
self.message = self.claim.stream_message
|
|
|
|
|
|
|
|
def update(
|
2019-03-25 17:30:30 +01:00
|
|
|
self, file_path=None, stream_type=None,
|
2019-03-24 21:55:04 +01:00
|
|
|
fee_currency=None, fee_amount=None, fee_address=None,
|
|
|
|
**kwargs):
|
|
|
|
|
2019-03-25 17:30:30 +01:00
|
|
|
duration_was_not_set = True
|
|
|
|
sub_types = ('image', 'video', 'audio')
|
|
|
|
for key in list(kwargs.keys()):
|
|
|
|
for sub_type in sub_types:
|
|
|
|
if key.startswith(f'{sub_type}_'):
|
|
|
|
stream_type = sub_type
|
|
|
|
sub_obj = getattr(self, sub_type)
|
|
|
|
sub_obj_attr = key[len(f'{sub_type}_'):]
|
|
|
|
setattr(sub_obj, sub_obj_attr, kwargs.pop(key))
|
|
|
|
if sub_obj_attr == 'duration':
|
|
|
|
duration_was_not_set = False
|
|
|
|
break
|
|
|
|
|
|
|
|
if stream_type is not None:
|
|
|
|
if stream_type not in sub_types:
|
|
|
|
raise Exception(
|
|
|
|
f"stream_type of '{stream_type}' is not valid, must be one of: {sub_types}"
|
|
|
|
)
|
|
|
|
|
|
|
|
sub_obj = getattr(self, stream_type)
|
|
|
|
if duration_was_not_set and file_path and isinstance(sub_obj, Playable):
|
|
|
|
sub_obj.set_duration_from_path(file_path)
|
2019-03-24 21:55:04 +01:00
|
|
|
|
2019-03-25 17:30:30 +01:00
|
|
|
super().update(**kwargs)
|
2019-03-24 21:55:04 +01:00
|
|
|
|
|
|
|
if file_path is not None:
|
|
|
|
self.media_type = guess_media_type(file_path)
|
|
|
|
if not os.path.isfile(file_path):
|
|
|
|
raise Exception(f"File does not exist: {file_path}")
|
|
|
|
self.file.size = os.path.getsize(file_path)
|
|
|
|
if self.file.size == 0:
|
|
|
|
raise Exception(f"Cannot publish empty file: {file_path}")
|
|
|
|
|
|
|
|
if fee_amount and fee_currency:
|
|
|
|
if fee_address:
|
|
|
|
self.fee.address = fee_address
|
|
|
|
if fee_currency.lower() == 'lbc':
|
|
|
|
self.fee.lbc = Decimal(fee_amount)
|
2019-03-30 01:26:10 +01:00
|
|
|
elif fee_currency.lower() == 'btc':
|
|
|
|
self.fee.btc = Decimal(fee_amount)
|
2019-03-24 21:55:04 +01:00
|
|
|
elif fee_currency.lower() == 'usd':
|
|
|
|
self.fee.usd = Decimal(fee_amount)
|
|
|
|
else:
|
|
|
|
raise Exception(f'Unknown currency type: {fee_currency}')
|
2019-03-15 06:33:41 +01:00
|
|
|
|
|
|
|
@property
|
2019-03-28 01:32:43 +01:00
|
|
|
def sd_hash(self) -> str:
|
|
|
|
return hexlify(self.message.sd_hash).decode()
|
2019-03-15 06:33:41 +01:00
|
|
|
|
2019-03-28 01:32:43 +01:00
|
|
|
@sd_hash.setter
|
|
|
|
def sd_hash(self, sd_hash: str):
|
|
|
|
self.message.sd_hash = unhexlify(sd_hash.encode())
|
2019-03-15 06:33:41 +01:00
|
|
|
|
|
|
|
@property
|
2019-03-28 01:32:43 +01:00
|
|
|
def sd_hash_bytes(self) -> bytes:
|
|
|
|
return self.message.sd_hash
|
2019-03-15 06:33:41 +01:00
|
|
|
|
2019-03-28 01:32:43 +01:00
|
|
|
@sd_hash_bytes.setter
|
|
|
|
def sd_hash_bytes(self, sd_hash: bytes):
|
|
|
|
self.message.sd_hash = sd_hash
|
2019-03-15 06:33:41 +01:00
|
|
|
|
|
|
|
@property
|
|
|
|
def author(self) -> str:
|
2019-03-24 21:55:04 +01:00
|
|
|
return self.message.author
|
2019-03-15 06:33:41 +01:00
|
|
|
|
|
|
|
@author.setter
|
|
|
|
def author(self, author: str):
|
2019-03-24 21:55:04 +01:00
|
|
|
self.message.author = author
|
2019-03-15 06:33:41 +01:00
|
|
|
|
|
|
|
@property
|
|
|
|
def license(self) -> str:
|
2019-03-24 21:55:04 +01:00
|
|
|
return self.message.license
|
2019-03-15 06:33:41 +01:00
|
|
|
|
|
|
|
@license.setter
|
|
|
|
def license(self, license: str):
|
2019-03-24 21:55:04 +01:00
|
|
|
self.message.license = license
|
2019-03-15 06:33:41 +01:00
|
|
|
|
|
|
|
@property
|
|
|
|
def license_url(self) -> str:
|
2019-03-24 21:55:04 +01:00
|
|
|
return self.message.license_url
|
2019-03-15 06:33:41 +01:00
|
|
|
|
|
|
|
@license_url.setter
|
|
|
|
def license_url(self, license_url: str):
|
2019-03-24 21:55:04 +01:00
|
|
|
self.message.license_url = license_url
|
2019-03-15 06:33:41 +01:00
|
|
|
|
|
|
|
@property
|
|
|
|
def release_time(self) -> int:
|
2019-03-24 21:55:04 +01:00
|
|
|
return self.message.release_time
|
2019-03-15 06:33:41 +01:00
|
|
|
|
|
|
|
@release_time.setter
|
|
|
|
def release_time(self, release_time: int):
|
2019-03-24 21:55:04 +01:00
|
|
|
self.message.release_time = release_time
|
2019-03-25 17:30:30 +01:00
|
|
|
|
|
|
|
@property
|
|
|
|
def media_type(self) -> str:
|
|
|
|
return self.message.media_type
|
|
|
|
|
|
|
|
@media_type.setter
|
|
|
|
def media_type(self, media_type: str):
|
|
|
|
self.message.media_type = media_type
|
|
|
|
|
|
|
|
@property
|
|
|
|
def fee(self) -> Fee:
|
|
|
|
return Fee(self.message.fee)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def has_fee(self) -> bool:
|
|
|
|
return self.message.HasField('fee')
|
|
|
|
|
|
|
|
@property
|
|
|
|
def file(self) -> File:
|
|
|
|
return File(self.message.file)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def image(self) -> Image:
|
|
|
|
return Image(self.message.image)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def video(self) -> Video:
|
|
|
|
return Video(self.message.video)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def audio(self) -> Audio:
|
|
|
|
return Audio(self.message.audio)
|