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-31 05:46:14 +02:00
|
|
|
from decimal import Decimal, ROUND_UP
|
2019-03-15 06:33:41 +01:00
|
|
|
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
|
|
|
|
|
|
|
|
2019-04-15 21:25:09 +02:00
|
|
|
class Source:
|
2019-03-15 06:33:41 +01:00
|
|
|
|
2019-04-15 21:25:09 +02:00
|
|
|
__slots__ = 'message',
|
2019-03-15 06:33:41 +01:00
|
|
|
|
|
|
|
def __init__(self, file_message):
|
2019-04-15 21:25:09 +02:00
|
|
|
self.message = file_message
|
2019-03-15 06:33:41 +01:00
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self) -> str:
|
2019-04-15 21:25:09 +02:00
|
|
|
return self.message.name
|
2019-03-15 06:33:41 +01:00
|
|
|
|
|
|
|
@name.setter
|
|
|
|
def name(self, name: str):
|
2019-04-15 21:25:09 +02:00
|
|
|
self.message.name = name
|
2019-03-15 06:33:41 +01:00
|
|
|
|
|
|
|
@property
|
|
|
|
def size(self) -> int:
|
2019-04-15 21:25:09 +02:00
|
|
|
return self.message.size
|
2019-03-15 06:33:41 +01:00
|
|
|
|
|
|
|
@size.setter
|
|
|
|
def size(self, size: int):
|
2019-04-15 21:25:09 +02:00
|
|
|
self.message.size = size
|
|
|
|
|
|
|
|
@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 sd_hash(self) -> str:
|
|
|
|
return hexlify(self.message.sd_hash).decode()
|
|
|
|
|
|
|
|
@sd_hash.setter
|
|
|
|
def sd_hash(self, sd_hash: str):
|
|
|
|
self.message.sd_hash = unhexlify(sd_hash.encode())
|
|
|
|
|
|
|
|
@property
|
|
|
|
def sd_hash_bytes(self) -> bytes:
|
|
|
|
return self.message.sd_hash
|
|
|
|
|
|
|
|
@sd_hash_bytes.setter
|
|
|
|
def sd_hash_bytes(self, sd_hash: bytes):
|
|
|
|
self.message.sd_hash = sd_hash
|
|
|
|
|
|
|
|
@property
|
|
|
|
def url(self) -> str:
|
|
|
|
return self.message.url
|
|
|
|
|
|
|
|
@url.setter
|
|
|
|
def url(self, url: str):
|
|
|
|
self.message.url = url
|
2019-03-15 06:33:41 +01:00
|
|
|
|
|
|
|
|
|
|
|
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-31 05:46:14 +02:00
|
|
|
PENNIES = Decimal('100.0')
|
|
|
|
PENNY = Decimal('0.01')
|
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-31 05:46:14 +02:00
|
|
|
self.pennies = int(amount.quantize(self.PENNY, ROUND_UP) * self.PENNIES)
|
2019-03-15 14:05:14 +01:00
|
|
|
|
|
|
|
@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-04-20 07:12:43 +02:00
|
|
|
class ClaimReference:
|
|
|
|
|
|
|
|
__slots__ = 'message',
|
|
|
|
|
|
|
|
def __init__(self, message):
|
|
|
|
self.message = message
|
|
|
|
|
|
|
|
@property
|
|
|
|
def claim_id(self) -> str:
|
|
|
|
return hexlify(self.claim_hash[::-1]).decode()
|
|
|
|
|
|
|
|
@claim_id.setter
|
|
|
|
def claim_id(self, claim_id: str):
|
|
|
|
self.claim_hash = unhexlify(claim_id)[::-1]
|
|
|
|
|
|
|
|
@property
|
|
|
|
def claim_hash(self) -> bytes:
|
|
|
|
return self.message.claim_hash
|
|
|
|
|
|
|
|
@claim_hash.setter
|
|
|
|
def claim_hash(self, claim_hash: bytes):
|
|
|
|
self.message.claim_hash = claim_hash
|
|
|
|
|
|
|
|
|
|
|
|
class ClaimList(BaseMessageList[ClaimReference]):
|
|
|
|
|
|
|
|
__slots__ = ()
|
|
|
|
item_class = ClaimReference
|
|
|
|
|
|
|
|
def append(self, value):
|
|
|
|
self.add().claim_id = value
|
|
|
|
|
|
|
|
@property
|
|
|
|
def claim_ids(self) -> List[str]:
|
|
|
|
return [c.claim_id for c in self]
|
|
|
|
|
|
|
|
|
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-04-20 07:12:43 +02:00
|
|
|
object_fields = 'thumbnail',
|
|
|
|
repeat_fields = 'tags', 'languages', 'locations'
|
|
|
|
|
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-04-20 07:12:43 +02:00
|
|
|
def to_dict(self):
|
|
|
|
claim = self.claim.to_dict()
|
|
|
|
if 'languages' in claim:
|
|
|
|
claim['languages'] = self.langtags
|
|
|
|
return claim
|
|
|
|
|
|
|
|
def update(self, **kwargs):
|
|
|
|
for key in list(kwargs):
|
|
|
|
for field in self.object_fields:
|
|
|
|
if key.startswith(f'{field}_'):
|
|
|
|
attr = getattr(self, field)
|
|
|
|
setattr(attr, key[len(f'{field}_'):], kwargs.pop(key))
|
|
|
|
continue
|
|
|
|
|
|
|
|
for l in self.repeat_fields:
|
|
|
|
field = getattr(self, l)
|
|
|
|
if kwargs.pop(f'clear_{l}', False):
|
|
|
|
del field[:]
|
|
|
|
items = kwargs.pop(l, None)
|
|
|
|
if items is not None:
|
|
|
|
if isinstance(items, str):
|
|
|
|
field.append(items)
|
|
|
|
elif isinstance(items, list):
|
|
|
|
field.extend(items)
|
|
|
|
else:
|
|
|
|
raise ValueError(f"Unknown {l} value: {items}")
|
|
|
|
|
|
|
|
for key, value in kwargs.items():
|
|
|
|
setattr(self, key, value)
|
|
|
|
|
2019-03-18 05:59:13 +01:00
|
|
|
@property
|
2019-03-24 21:55:04 +01:00
|
|
|
def title(self) -> str:
|
2019-04-15 21:25:09 +02:00
|
|
|
return self.claim.message.title
|
2019-03-24 21:55:04 +01:00
|
|
|
|
|
|
|
@title.setter
|
|
|
|
def title(self, title: str):
|
2019-04-15 21:25:09 +02:00
|
|
|
self.claim.message.title = title
|
2019-03-18 05:59:13 +01:00
|
|
|
|
|
|
|
@property
|
2019-03-24 21:55:04 +01:00
|
|
|
def description(self) -> str:
|
2019-04-15 21:25:09 +02:00
|
|
|
return self.claim.message.description
|
2019-03-24 21:55:04 +01:00
|
|
|
|
|
|
|
@description.setter
|
|
|
|
def description(self, description: str):
|
2019-04-15 21:25:09 +02:00
|
|
|
self.claim.message.description = description
|
2019-03-18 05:59:13 +01:00
|
|
|
|
|
|
|
@property
|
2019-04-15 21:25:09 +02:00
|
|
|
def thumbnail(self) -> Source:
|
|
|
|
return Source(self.claim.message.thumbnail)
|
2019-03-18 05:59:13 +01:00
|
|
|
|
|
|
|
@property
|
2019-03-30 01:26:10 +01:00
|
|
|
def tags(self) -> List:
|
2019-04-15 21:25:09 +02:00
|
|
|
return self.claim.message.tags
|
2019-03-28 01:32:43 +01:00
|
|
|
|
|
|
|
@property
|
2019-03-30 01:26:10 +01:00
|
|
|
def languages(self) -> LanguageList:
|
2019-04-15 21:25:09 +02:00
|
|
|
return LanguageList(self.claim.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:
|
2019-04-15 21:25:09 +02:00
|
|
|
return LocationList(self.claim.message.locations)
|
2019-03-18 05:59:13 +01:00
|
|
|
|
2019-03-24 21:55:04 +01:00
|
|
|
|
|
|
|
class Channel(BaseClaimSubType):
|
|
|
|
|
|
|
|
__slots__ = ()
|
|
|
|
|
2019-04-20 07:12:43 +02:00
|
|
|
object_fields = BaseClaimSubType.object_fields + ('cover',)
|
|
|
|
repeat_fields = BaseClaimSubType.repeat_fields + ('featured',)
|
|
|
|
|
2019-03-24 21:55:04 +01:00
|
|
|
def __init__(self, claim: Claim = None):
|
|
|
|
super().__init__(claim)
|
|
|
|
self.message = self.claim.channel_message
|
2019-03-18 05:59:13 +01:00
|
|
|
|
2019-04-20 07:12:43 +02:00
|
|
|
def to_dict(self):
|
|
|
|
claim = super().to_dict()
|
|
|
|
claim.update(claim.pop('channel'))
|
|
|
|
claim['public_key'] = self.public_key
|
|
|
|
return claim
|
|
|
|
|
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
|
2019-04-20 07:12:43 +02:00
|
|
|
def email(self) -> str:
|
|
|
|
return self.message.email
|
2019-03-18 05:59:13 +01:00
|
|
|
|
2019-04-20 07:12:43 +02:00
|
|
|
@email.setter
|
|
|
|
def email(self, email: str):
|
|
|
|
self.message.email = email
|
2019-03-18 05:59:13 +01:00
|
|
|
|
|
|
|
@property
|
2019-04-20 07:12:43 +02:00
|
|
|
def website_url(self) -> str:
|
|
|
|
return self.message.website_url
|
2019-03-18 05:59:13 +01:00
|
|
|
|
2019-04-20 07:12:43 +02:00
|
|
|
@website_url.setter
|
|
|
|
def website_url(self, website_url: str):
|
|
|
|
self.message.website_url = website_url
|
2019-03-18 05:59:13 +01:00
|
|
|
|
|
|
|
@property
|
2019-04-15 21:25:09 +02:00
|
|
|
def cover(self) -> Source:
|
|
|
|
return Source(self.message.cover)
|
2019-03-18 05:59:13 +01:00
|
|
|
|
2019-04-20 07:12:43 +02:00
|
|
|
@property
|
|
|
|
def featured(self) -> ClaimList:
|
|
|
|
return ClaimList(self.message.featured)
|
|
|
|
|
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
|
|
|
|
2019-04-20 07:12:43 +02:00
|
|
|
object_fields = BaseClaimSubType.object_fields + ('source',)
|
|
|
|
|
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
|
|
|
|
|
2019-04-20 07:12:43 +02:00
|
|
|
def to_dict(self):
|
|
|
|
claim = super().to_dict()
|
|
|
|
claim.update(claim.pop('stream'))
|
|
|
|
fee = claim.get('fee', {})
|
|
|
|
if 'address' in fee:
|
|
|
|
fee['address'] = self.fee.address
|
|
|
|
if 'amount' in fee:
|
|
|
|
fee['amount'] = self.fee.amount
|
|
|
|
return claim
|
|
|
|
|
2019-03-24 21:55:04 +01:00
|
|
|
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-04-20 07:12:43 +02:00
|
|
|
if 'sd_hash' in kwargs:
|
|
|
|
self.source.sd_hash = kwargs.pop('sd_hash')
|
|
|
|
|
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:
|
2019-04-20 07:12:43 +02:00
|
|
|
self.source.media_type = guess_media_type(file_path)
|
2019-03-24 21:55:04 +01:00
|
|
|
if not os.path.isfile(file_path):
|
|
|
|
raise Exception(f"File does not exist: {file_path}")
|
2019-04-20 07:12:43 +02:00
|
|
|
self.source.size = os.path.getsize(file_path)
|
|
|
|
if self.source.size == 0:
|
2019-03-24 21:55:04 +01:00
|
|
|
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
|
|
|
|
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 fee(self) -> Fee:
|
|
|
|
return Fee(self.message.fee)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def has_fee(self) -> bool:
|
|
|
|
return self.message.HasField('fee')
|
|
|
|
|
|
|
|
@property
|
2019-04-15 21:25:09 +02:00
|
|
|
def source(self) -> Source:
|
|
|
|
return Source(self.message.source)
|
2019-03-25 17:30:30 +01:00
|
|
|
|
|
|
|
@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)
|