<s> import locale <EOL> import os <EOL> import sys <EOL> import simplejson as json <EOL> import binascii <EOL> import subprocess <EOL> import logging <EOL> import logging . handlers <EOL> import requests <EOL> import base64 <EOL> import base58 <EOL> import platform <EOL> import json <EOL> from twisted . web import server , resource , static <EOL> from twisted . internet import defer , threads , error , reactor <EOL> from txjsonrpc import jsonrpclib <EOL> from txjsonrpc . web import jsonrpc <EOL> from txjsonrpc . web . jsonrpc import Handler <EOL> from datetime import datetime <EOL> from decimal import Decimal <EOL> from appdirs import user_data_dir <EOL> from urllib2 import urlopen <EOL> from lbrynet import __version__ as lbrynet_version <EOL> from lbryum . version import ELECTRUM_VERSION as lbryum_version <EOL> from lbrynet . core . PaymentRateManager import PaymentRateManager <EOL> from lbrynet . core . server . BlobAvailabilityHandler import BlobAvailabilityHandlerFactory <EOL> from lbrynet . core . server . BlobRequestHandler import BlobRequestHandlerFactory <EOL> from lbrynet . core . server . ServerProtocol import ServerProtocolFactory <EOL> from lbrynet . lbrynet_console . ControlHandlers import get_time_behind_blockchain <EOL> from lbrynet . core . Error import UnknownNameError <EOL> from lbrynet . lbryfile . StreamDescriptor import LBRYFileStreamType <EOL> from lbrynet . lbryfile . client . LBRYFileDownloader import LBRYFileSaverFactory , LBRYFileOpenerFactory <EOL> from lbrynet . lbryfile . client . LBRYFileOptions import add_lbry_file_to_sd_identifier <EOL> from lbrynet . lbrynet_daemon . LBRYDownloader import GetStream , FetcherDaemon <EOL> from lbrynet . lbrynet_daemon . LBRYPublisher import Publisher <EOL> from lbrynet . core . utils import generate_id <EOL> from lbrynet . lbrynet_console . LBRYSettings import LBRYSettings <EOL> from lbrynet . conf import MIN_BLOB_DATA_PAYMENT_RATE , DEFAULT_MAX_SEARCH_RESULTS , KNOWN_DHT_NODES , DEFAULT_MAX_KEY_FEE <EOL> from lbrynet . conf import API_CONNECTION_STRING , API_PORT , API_ADDRESS , DEFAULT_TIMEOUT , UI_ADDRESS <EOL> from lbrynet . core . StreamDescriptor import StreamDescriptorIdentifier , download_sd_blob <EOL> from lbrynet . core . Session import LBRYSession <EOL> from lbrynet . core . PTCWallet import PTCWallet <EOL> from lbrynet . core . LBRYcrdWallet import LBRYcrdWallet , LBRYumWallet <EOL> from lbrynet . lbryfilemanager . LBRYFileManager import LBRYFileManager <EOL> from lbrynet . lbryfile . LBRYFileMetadataManager import DBLBRYFileMetadataManager , TempLBRYFileMetadataManager <EOL> if sys . platform != "<STR_LIT>" : <EOL> log_dir = os . path . join ( os . path . expanduser ( "<STR_LIT>" ) , "<STR_LIT>" ) <EOL> else : <EOL> log_dir = user_data_dir ( "<STR_LIT>" ) <EOL> if not os . path . isdir ( log_dir ) : <EOL> os . mkdir ( log_dir ) <EOL> LOG_FILENAME = os . path . join ( log_dir , '<STR_LIT>' ) <EOL> if os . path . isfile ( LOG_FILENAME ) : <EOL> f = open ( LOG_FILENAME , '<STR_LIT:r>' ) <EOL> PREVIOUS_LOG = len ( f . read ( ) ) <EOL> f . close ( ) <EOL> else : <EOL> PREVIOUS_LOG = <NUM_LIT:0> <EOL> log = logging . getLogger ( __name__ ) <EOL> handler = logging . handlers . RotatingFileHandler ( LOG_FILENAME , maxBytes = <NUM_LIT> , backupCount = <NUM_LIT:5> ) <EOL> log . addHandler ( handler ) <EOL> log . setLevel ( logging . INFO ) <EOL> STARTUP_STAGES = [ <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> ] <EOL> ALLOWED_DURING_STARTUP = [ '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT:version>' , '<STR_LIT>' ] <EOL> BAD_REQUEST = <NUM_LIT> <EOL> NOT_FOUND = <NUM_LIT> <EOL> OK_CODE = <NUM_LIT:200> <EOL> class LBRYDaemon ( jsonrpc . JSONRPC ) : <EOL> """<STR_LIT>""" <EOL> isLeaf = True <EOL> def __init__ ( self , ui_version_info , wallet_type = "<STR_LIT>" ) : <EOL> jsonrpc . JSONRPC . __init__ ( self ) <EOL> reactor . addSystemEventTrigger ( '<STR_LIT>' , '<STR_LIT>' , self . _shutdown ) <EOL> self . startup_status = STARTUP_STAGES [ <NUM_LIT:0> ] <EOL> self . startup_message = None <EOL> self . announced_startup = False <EOL> self . query_handlers = { } <EOL> self . ui_version = ui_version_info . replace ( '<STR_LIT:\n>' , '<STR_LIT>' ) <EOL> self . git_lbrynet_version = None <EOL> self . git_lbryum_version = None <EOL> self . wallet_type = wallet_type <EOL> self . session_settings = None <EOL> self . first_run = None <EOL> self . log_file = LOG_FILENAME <EOL> self . fetcher = None <EOL> self . current_db_revision = <NUM_LIT:1> <EOL> self . run_server = True <EOL> self . session = None <EOL> self . known_dht_nodes = KNOWN_DHT_NODES <EOL> self . platform_info = { <EOL> "<STR_LIT>" : platform . processor ( ) , <EOL> "<STR_LIT>" : platform . python_version ( ) , <EOL> "<STR_LIT>" : platform . platform ( ) , <EOL> "<STR_LIT>" : platform . release ( ) , <EOL> "<STR_LIT>" : platform . system ( ) , <EOL> "<STR_LIT>" : lbrynet_version , <EOL> "<STR_LIT>" : lbryum_version , <EOL> "<STR_LIT>" : self . ui_version , <EOL> } <EOL> try : <EOL> self . platform_info [ '<STR_LIT>' ] = json . load ( urlopen ( '<STR_LIT>' ) ) [ '<STR_LIT>' ] <EOL> except : <EOL> self . platform_info [ '<STR_LIT>' ] = "<STR_LIT>" <EOL> if os . name == "<STR_LIT>" : <EOL> from lbrynet . winhelpers . knownpaths import get_path , FOLDERID , UserHandle <EOL> self . download_directory = get_path ( FOLDERID . Downloads , UserHandle . current ) <EOL> self . db_dir = os . path . join ( get_path ( FOLDERID . RoamingAppData , UserHandle . current ) , "<STR_LIT>" ) <EOL> self . lbrycrdd_path = "<STR_LIT>" <EOL> if wallet_type == "<STR_LIT>" : <EOL> self . wallet_dir = os . path . join ( get_path ( FOLDERID . RoamingAppData , UserHandle . current ) , "<STR_LIT>" ) <EOL> else : <EOL> self . wallet_dir = os . path . join ( get_path ( FOLDERID . RoamingAppData , UserHandle . current ) , "<STR_LIT>" ) <EOL> elif sys . platform == "<STR_LIT>" : <EOL> self . download_directory = os . path . join ( os . path . expanduser ( "<STR_LIT>" ) , '<STR_LIT>' ) <EOL> self . db_dir = user_data_dir ( "<STR_LIT>" ) <EOL> self . lbrycrdd_path = "<STR_LIT>" <EOL> if wallet_type == "<STR_LIT>" : <EOL> self . wallet_dir = user_data_dir ( "<STR_LIT>" ) <EOL> else : <EOL> self . wallet_dir = user_data_dir ( "<STR_LIT>" ) <EOL> else : <EOL> self . download_directory = os . getcwd ( ) <EOL> self . db_dir = os . path . join ( os . path . expanduser ( "<STR_LIT>" ) , "<STR_LIT>" ) <EOL> self . lbrycrdd_path = "<STR_LIT>" <EOL> if wallet_type == "<STR_LIT>" : <EOL> self . wallet_dir = os . path . join ( os . path . expanduser ( "<STR_LIT>" ) , "<STR_LIT>" ) <EOL> else : <EOL> self . wallet_dir = os . path . join ( os . path . expanduser ( "<STR_LIT>" ) , "<STR_LIT>" ) <EOL> self . created_data_dir = False <EOL> if not os . path . exists ( self . db_dir ) : <EOL> os . mkdir ( self . db_dir ) <EOL> self . created_data_dir = True <EOL> self . blobfile_dir = os . path . join ( self . db_dir , "<STR_LIT>" ) <EOL> self . lbrycrd_conf = os . path . join ( self . wallet_dir , "<STR_LIT>" ) <EOL> self . autofetcher_conf = os . path . join ( self . wallet_dir , "<STR_LIT>" ) <EOL> self . daemon_conf = os . path . join ( self . db_dir , '<STR_LIT>' ) <EOL> self . wallet_conf = os . path . join ( self . wallet_dir , "<STR_LIT>" ) <EOL> self . wallet_user = None <EOL> self . wallet_password = None <EOL> self . sd_identifier = StreamDescriptorIdentifier ( ) <EOL> self . stream_info_manager = TempLBRYFileMetadataManager ( ) <EOL> self . settings = LBRYSettings ( self . db_dir ) <EOL> self . blob_request_payment_rate_manager = None <EOL> self . lbry_file_metadata_manager = None <EOL> self . lbry_file_manager = None <EOL> self . default_settings = { <EOL> '<STR_LIT>' : False , <EOL> '<STR_LIT>' : MIN_BLOB_DATA_PAYMENT_RATE , <EOL> '<STR_LIT>' : DEFAULT_MAX_KEY_FEE , <EOL> '<STR_LIT>' : self . download_directory , <EOL> '<STR_LIT>' : <NUM_LIT:0.0> , <EOL> '<STR_LIT>' : <NUM_LIT:0.0> , <EOL> '<STR_LIT>' : True , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : DEFAULT_MAX_SEARCH_RESULTS , <EOL> '<STR_LIT>' : wallet_type , <EOL> '<STR_LIT>' : True , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : True , <EOL> '<STR_LIT>' : True , <EOL> '<STR_LIT>' : False <EOL> } <EOL> def render ( self , request ) : <EOL> request . content . seek ( <NUM_LIT:0> , <NUM_LIT:0> ) <EOL> content = request . content . read ( ) <EOL> parsed = jsonrpclib . loads ( content ) <EOL> functionPath = parsed . get ( "<STR_LIT>" ) <EOL> args = parsed . get ( '<STR_LIT>' ) <EOL> id = parsed . get ( '<STR_LIT:id>' ) <EOL> version = parsed . get ( '<STR_LIT>' ) <EOL> if version : <EOL> version = int ( float ( version ) ) <EOL> elif id and not version : <EOL> version = jsonrpclib . VERSION_1 <EOL> else : <EOL> version = jsonrpclib . VERSION_PRE1 <EOL> if not self . announced_startup : <EOL> if functionPath not in ALLOWED_DURING_STARTUP : <EOL> return server . failure <EOL> try : <EOL> function = self . _getFunction ( functionPath ) <EOL> except jsonrpclib . Fault , f : <EOL> self . _cbRender ( f , request , id , version ) <EOL> else : <EOL> request . setHeader ( "<STR_LIT>" , "<STR_LIT:*>" ) <EOL> request . setHeader ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> if args == [ { } ] : <EOL> d = defer . maybeDeferred ( function ) <EOL> else : <EOL> d = defer . maybeDeferred ( function , * args ) <EOL> d . addErrback ( self . _ebRender , id ) <EOL> d . addCallback ( self . _cbRender , request , id , version ) <EOL> return server . NOT_DONE_YET <EOL> def _cbRender ( self , result , request , id , version ) : <EOL> if isinstance ( result , Handler ) : <EOL> result = result . result <EOL> if isinstance ( result , dict ) : <EOL> result = result [ '<STR_LIT:result>' ] <EOL> if version == jsonrpclib . VERSION_PRE1 : <EOL> if not isinstance ( result , jsonrpclib . Fault ) : <EOL> result = ( result , ) <EOL> try : <EOL> s = jsonrpclib . dumps ( result , version = version ) <EOL> except : <EOL> f = jsonrpclib . Fault ( self . FAILURE , "<STR_LIT>" ) <EOL> s = jsonrpclib . dumps ( f , version = version ) <EOL> request . setHeader ( "<STR_LIT>" , str ( len ( s ) ) ) <EOL> request . write ( s ) <EOL> request . finish ( ) <EOL> def _ebRender ( self , failure , id ) : <EOL> if isinstance ( failure . value , jsonrpclib . Fault ) : <EOL> return failure . value <EOL> log . error ( failure ) <EOL> return jsonrpclib . Fault ( self . FAILURE , "<STR_LIT:error>" ) <EOL> def setup ( self ) : <EOL> def _log_starting_vals ( ) : <EOL> def _get_lbry_files_json ( ) : <EOL> r = self . _get_lbry_files ( ) <EOL> return json . dumps ( r ) <EOL> def _get_lbryum_version ( ) : <EOL> r = urlopen ( "<STR_LIT>" ) . read ( ) . split ( '<STR_LIT:\n>' ) <EOL> version = next ( line . split ( "<STR_LIT:=>" ) [ <NUM_LIT:1> ] . split ( "<STR_LIT:#>" ) [ <NUM_LIT:0> ] . replace ( "<STR_LIT:U+0020>" , "<STR_LIT>" ) <EOL> for line in r if "<STR_LIT>" in line ) <EOL> version = version . replace ( "<STR_LIT:'>" , "<STR_LIT>" ) <EOL> log . info ( "<STR_LIT>" + str ( version ) + "<STR_LIT>" + str ( lbryum_version ) + "<STR_LIT>" + str ( <EOL> version > lbryum_version ) ) <EOL> return version <EOL> def _get_lbrynet_version ( ) : <EOL> r = urlopen ( "<STR_LIT>" ) . read ( ) . split ( '<STR_LIT:\n>' ) <EOL> vs = next ( i for i in r if '<STR_LIT>' in i ) . split ( "<STR_LIT:=>" ) [ <NUM_LIT:1> ] . replace ( "<STR_LIT:U+0020>" , "<STR_LIT>" ) <EOL> vt = tuple ( int ( x ) for x in vs [ <NUM_LIT:1> : - <NUM_LIT:1> ] . split ( '<STR_LIT:U+002C>' ) ) <EOL> vr = "<STR_LIT:.>" . join ( [ str ( x ) for x in vt ] ) <EOL> log . info ( "<STR_LIT>" + str ( vr ) + "<STR_LIT>" + str ( lbrynet_version ) + "<STR_LIT>" + str ( <EOL> vr > lbrynet_version ) ) <EOL> return vr <EOL> self . git_lbrynet_version = _get_lbrynet_version ( ) <EOL> self . git_lbryum_version = _get_lbryum_version ( ) <EOL> log . info ( "<STR_LIT>" + _get_lbry_files_json ( ) ) <EOL> log . info ( "<STR_LIT>" + str ( self . session . wallet . wallet_balance ) ) <EOL> return defer . succeed ( None ) <EOL> def _announce_startup ( ) : <EOL> def _announce ( ) : <EOL> self . announced_startup = True <EOL> self . startup_status = STARTUP_STAGES [ <NUM_LIT:5> ] <EOL> log . info ( "<STR_LIT:[>" + str ( datetime . now ( ) ) + "<STR_LIT>" ) <EOL> if self . first_run : <EOL> d = self . _upload_log ( name_prefix = "<STR_LIT>" ) <EOL> else : <EOL> d = self . _upload_log ( exclude_previous = True , name_prefix = "<STR_LIT:start>" ) <EOL> if float ( self . session . wallet . wallet_balance ) == <NUM_LIT:0.0> : <EOL> d . addCallback ( lambda _ : self . _check_first_run ( ) ) <EOL> d . addCallback ( self . _show_first_run_result ) <EOL> d . addCallback ( lambda _ : _announce ( ) ) <EOL> return d <EOL> log . info ( "<STR_LIT:[>" + str ( datetime . now ( ) ) + "<STR_LIT>" ) <EOL> d = defer . Deferred ( ) <EOL> d . addCallback ( lambda _ : self . _initial_setup ( ) ) <EOL> d . addCallback ( self . _set_daemon_settings ) <EOL> d . addCallback ( lambda _ : threads . deferToThread ( self . _setup_data_directory ) ) <EOL> d . addCallback ( lambda _ : self . _check_db_migration ( ) ) <EOL> d . addCallback ( lambda _ : self . _get_settings ( ) ) <EOL> d . addCallback ( lambda _ : self . _get_lbrycrdd_path ( ) ) <EOL> d . addCallback ( lambda _ : self . _get_session ( ) ) <EOL> d . addCallback ( lambda _ : add_lbry_file_to_sd_identifier ( self . sd_identifier ) ) <EOL> d . addCallback ( lambda _ : self . _setup_stream_identifier ( ) ) <EOL> d . addCallback ( lambda _ : self . _setup_lbry_file_manager ( ) ) <EOL> d . addCallback ( lambda _ : self . _setup_lbry_file_opener ( ) ) <EOL> d . addCallback ( lambda _ : self . _setup_query_handlers ( ) ) <EOL> d . addCallback ( lambda _ : self . _setup_server ( ) ) <EOL> d . addCallback ( lambda _ : self . _setup_fetcher ( ) ) <EOL> d . addCallback ( lambda _ : _log_starting_vals ( ) ) <EOL> d . addCallback ( lambda _ : _announce_startup ( ) ) <EOL> d . callback ( None ) <EOL> return defer . succeed ( None ) <EOL> def _initial_setup ( self ) : <EOL> def _log_platform ( ) : <EOL> log . info ( "<STR_LIT>" + json . dumps ( self . platform_info ) ) <EOL> return defer . succeed ( None ) <EOL> def _load_daemon_conf ( ) : <EOL> if os . path . isfile ( self . daemon_conf ) : <EOL> f = open ( self . daemon_conf , "<STR_LIT:r>" ) <EOL> loaded_settings = json . loads ( f . read ( ) ) <EOL> f . close ( ) <EOL> missing_settings = { } <EOL> for k in self . default_settings . keys ( ) : <EOL> if k not in loaded_settings . keys ( ) : <EOL> missing_settings [ k ] = self . default_settings [ k ] <EOL> if missing_settings != { } : <EOL> for k in missing_settings . keys ( ) : <EOL> log . info ( "<STR_LIT>" + k + "<STR_LIT>" + str ( missing_settings [ k ] ) ) <EOL> loaded_settings [ k ] = missing_settings [ k ] <EOL> f = open ( self . daemon_conf , "<STR_LIT:w>" ) <EOL> f . write ( json . dumps ( loaded_settings ) ) <EOL> f . close ( ) <EOL> rsettings = loaded_settings <EOL> else : <EOL> log . info ( "<STR_LIT>" + json . dumps ( self . default_settings ) + "<STR_LIT>" + str ( self . daemon_conf ) ) <EOL> f = open ( self . daemon_conf , "<STR_LIT:w>" ) <EOL> f . write ( json . dumps ( self . default_settings ) ) <EOL> f . close ( ) <EOL> rsettings = self . default_settings <EOL> for k in rsettings . keys ( ) : <EOL> self . __dict__ [ k ] = rsettings [ k ] <EOL> return rsettings <EOL> d = _log_platform ( ) <EOL> d . addCallback ( lambda _ : _load_daemon_conf ( ) ) <EOL> return d <EOL> def _set_daemon_settings ( self , settings ) : <EOL> self . session_settings = settings <EOL> return defer . succeed ( None ) <EOL> def _start_server ( self ) : <EOL> if self . peer_port is not None : <EOL> server_factory = ServerProtocolFactory ( self . session . rate_limiter , <EOL> self . query_handlers , <EOL> self . session . peer_manager ) <EOL> try : <EOL> self . lbry_server_port = reactor . listenTCP ( self . peer_port , server_factory ) <EOL> except error . CannotListenError as e : <EOL> import traceback <EOL> log . error ( "<STR_LIT>" , self . peer_port , traceback . format_exc ( ) ) <EOL> raise ValueError ( "<STR_LIT>" , str ( e ) ) <EOL> return defer . succeed ( True ) <EOL> def _stop_server ( self ) : <EOL> if self . lbry_server_port is not None : <EOL> self . lbry_server_port , p = None , self . lbry_server_port <EOL> return defer . maybeDeferred ( p . stopListening ) <EOL> else : <EOL> return defer . succeed ( True ) <EOL> def _setup_server ( self ) : <EOL> def restore_running_status ( running ) : <EOL> if running is True : <EOL> return self . _start_server ( ) <EOL> return defer . succeed ( True ) <EOL> self . startup_status = STARTUP_STAGES [ <NUM_LIT:4> ] <EOL> dl = self . settings . get_server_running_status ( ) <EOL> dl . addCallback ( restore_running_status ) <EOL> return dl <EOL> def _setup_query_handlers ( self ) : <EOL> handlers = [ <EOL> BlobAvailabilityHandlerFactory ( self . session . blob_manager ) , <EOL> self . session . wallet . get_wallet_info_query_handler_factory ( ) , <EOL> ] <EOL> def get_blob_request_handler_factory ( rate ) : <EOL> self . blob_request_payment_rate_manager = PaymentRateManager ( <EOL> self . session . base_payment_rate_manager , rate <EOL> ) <EOL> handlers . append ( BlobRequestHandlerFactory ( self . session . blob_manager , self . session . wallet , <EOL> self . blob_request_payment_rate_manager ) ) <EOL> d1 = self . settings . get_server_data_payment_rate ( ) <EOL> d1 . addCallback ( get_blob_request_handler_factory ) <EOL> dl = defer . DeferredList ( [ d1 ] ) <EOL> dl . addCallback ( lambda _ : self . _add_query_handlers ( handlers ) ) <EOL> return dl <EOL> def _add_query_handlers ( self , query_handlers ) : <EOL> def _set_query_handlers ( statuses ) : <EOL> from future_builtins import zip <EOL> for handler , ( success , status ) in zip ( query_handlers , statuses ) : <EOL> if success is True : <EOL> self . query_handlers [ handler ] = status <EOL> ds = [ ] <EOL> for handler in query_handlers : <EOL> ds . append ( self . settings . get_query_handler_status ( handler . get_primary_query_identifier ( ) ) ) <EOL> dl = defer . DeferredList ( ds ) <EOL> dl . addCallback ( _set_query_handlers ) <EOL> return dl <EOL> def _upload_log ( self , name_prefix = None , exclude_previous = False ) : <EOL> if name_prefix : <EOL> name_prefix = name_prefix + "<STR_LIT:->" + platform . system ( ) <EOL> else : <EOL> name_prefix = platform . system ( ) <EOL> if self . session_settings [ '<STR_LIT>' ] : <EOL> LOG_URL = "<STR_LIT>" <EOL> if exclude_previous : <EOL> f = open ( self . log_file , "<STR_LIT:r>" ) <EOL> f . seek ( PREVIOUS_LOG ) <EOL> log_contents = f . read ( ) <EOL> f . close ( ) <EOL> else : <EOL> f = open ( self . log_file , "<STR_LIT:r>" ) <EOL> log_contents = f . read ( ) <EOL> f . close ( ) <EOL> t = datetime . now ( ) <EOL> log_name = name_prefix + "<STR_LIT:->" + base58 . b58encode ( self . lbryid ) [ : <NUM_LIT:20> ] + "<STR_LIT:->" + str ( t . month ) + "<STR_LIT:->" + str ( t . day ) + "<STR_LIT:->" + str ( t . year ) + "<STR_LIT:->" + str ( t . hour ) + "<STR_LIT:->" + str ( t . minute ) <EOL> params = { '<STR_LIT:name>' : log_name , '<STR_LIT>' : log_contents } <EOL> requests . post ( LOG_URL , params ) <EOL> return defer . succeed ( None ) <EOL> def _shutdown ( self ) : <EOL> log . info ( "<STR_LIT>" ) <EOL> log . info ( "<STR_LIT>" + self . startup_status [ <NUM_LIT:0> ] ) <EOL> d = self . _upload_log ( name_prefix = "<STR_LIT>" , exclude_previous = False if self . first_run else True ) <EOL> d . addCallback ( lambda _ : self . _stop_server ( ) ) <EOL> d . addErrback ( lambda err : log . info ( "<STR_LIT>" + err . getTraceback ( ) ) ) <EOL> if self . session is not None : <EOL> d . addCallback ( lambda _ : self . session . shut_down ( ) ) <EOL> d . addErrback ( lambda err : log . info ( "<STR_LIT>" + err . getTraceback ( ) ) ) <EOL> return d <EOL> def _update_settings ( self , settings ) : <EOL> for k in settings . keys ( ) : <EOL> if k == '<STR_LIT>' : <EOL> if type ( settings [ '<STR_LIT>' ] ) is bool : <EOL> self . session_settings [ '<STR_LIT>' ] = settings [ '<STR_LIT>' ] <EOL> else : <EOL> return defer . fail ( ) <EOL> elif k == '<STR_LIT>' : <EOL> if type ( settings [ '<STR_LIT>' ] ) is float : <EOL> self . session_settings [ '<STR_LIT>' ] = settings [ '<STR_LIT>' ] <EOL> elif type ( settings [ '<STR_LIT>' ] ) is int : <EOL> self . session_settings [ '<STR_LIT>' ] = float ( settings [ '<STR_LIT>' ] ) <EOL> else : <EOL> return defer . fail ( ) <EOL> elif k == '<STR_LIT>' : <EOL> if type ( settings [ '<STR_LIT>' ] ) is float : <EOL> self . session_settings [ '<STR_LIT>' ] = settings [ '<STR_LIT>' ] <EOL> elif type ( settings [ '<STR_LIT>' ] ) is int : <EOL> self . session_settings [ '<STR_LIT>' ] = float ( settings [ '<STR_LIT>' ] ) <EOL> else : <EOL> return defer . fail ( ) <EOL> elif k == '<STR_LIT>' : <EOL> if type ( settings [ '<STR_LIT>' ] ) is unicode : <EOL> if os . path . isdir ( settings [ '<STR_LIT>' ] ) : <EOL> self . session_settings [ '<STR_LIT>' ] = settings [ '<STR_LIT>' ] <EOL> else : <EOL> pass <EOL> else : <EOL> return defer . fail ( ) <EOL> elif k == '<STR_LIT>' : <EOL> if type ( settings [ '<STR_LIT>' ] ) is float : <EOL> self . session_settings [ '<STR_LIT>' ] = settings [ '<STR_LIT>' ] <EOL> elif type ( settings [ '<STR_LIT>' ] ) is int : <EOL> self . session_settings [ '<STR_LIT>' ] = float ( settings [ '<STR_LIT>' ] ) <EOL> else : <EOL> return defer . fail ( ) <EOL> elif k == '<STR_LIT>' : <EOL> if type ( settings [ '<STR_LIT>' ] ) is float : <EOL> self . session_settings [ '<STR_LIT>' ] = settings [ '<STR_LIT>' ] <EOL> if type ( settings [ '<STR_LIT>' ] ) is int : <EOL> self . session_settings [ '<STR_LIT>' ] = float ( settings [ '<STR_LIT>' ] ) <EOL> else : <EOL> return defer . fail ( ) <EOL> elif k == '<STR_LIT>' : <EOL> if type ( settings [ '<STR_LIT>' ] ) is bool : <EOL> self . session_settings [ '<STR_LIT>' ] = settings [ '<STR_LIT>' ] <EOL> else : <EOL> return defer . fail ( ) <EOL> f = open ( self . daemon_conf , "<STR_LIT:w>" ) <EOL> f . write ( json . dumps ( self . session_settings ) ) <EOL> f . close ( ) <EOL> return defer . succeed ( True ) <EOL> def _setup_fetcher ( self ) : <EOL> self . fetcher = FetcherDaemon ( self . session , self . lbry_file_manager , self . lbry_file_metadata_manager , <EOL> self . session . wallet , self . sd_identifier , self . autofetcher_conf ) <EOL> return defer . succeed ( None ) <EOL> def _setup_data_directory ( self ) : <EOL> self . startup_status = STARTUP_STAGES [ <NUM_LIT:1> ] <EOL> log . info ( "<STR_LIT>" ) <EOL> if self . created_data_dir : <EOL> db_revision = open ( os . path . join ( self . db_dir , "<STR_LIT>" ) , mode = '<STR_LIT:w>' ) <EOL> db_revision . write ( str ( self . current_db_revision ) ) <EOL> db_revision . close ( ) <EOL> log . debug ( "<STR_LIT>" , str ( os . path . join ( self . db_dir , "<STR_LIT>" ) ) ) <EOL> if not os . path . exists ( self . blobfile_dir ) : <EOL> os . mkdir ( self . blobfile_dir ) <EOL> log . debug ( "<STR_LIT>" , str ( self . blobfile_dir ) ) <EOL> def _check_db_migration ( self ) : <EOL> old_revision = <NUM_LIT:0> <EOL> db_revision_file = os . path . join ( self . db_dir , "<STR_LIT>" ) <EOL> if os . path . exists ( db_revision_file ) : <EOL> old_revision = int ( open ( db_revision_file ) . read ( ) . strip ( ) ) <EOL> if old_revision < self . current_db_revision : <EOL> from lbrynet . db_migrator import dbmigrator <EOL> log . info ( "<STR_LIT>" ) <EOL> d = threads . deferToThread ( dbmigrator . migrate_db , self . db_dir , old_revision , self . current_db_revision ) <EOL> def print_success ( old_dirs ) : <EOL> success_string = "<STR_LIT>" <EOL> success_string += "<STR_LIT>" <EOL> success_string += "<STR_LIT>" <EOL> for i , old_dir in enumerate ( old_dirs ) : <EOL> success_string += old_dir <EOL> if i + <NUM_LIT:1> < len ( old_dir ) : <EOL> success_string += "<STR_LIT:U+002CU+0020>" <EOL> log . info ( success_string ) <EOL> d . addCallback ( print_success ) <EOL> return d <EOL> return defer . succeed ( True ) <EOL> def _get_settings ( self ) : <EOL> d = self . settings . start ( ) <EOL> d . addCallback ( lambda _ : self . settings . get_lbryid ( ) ) <EOL> d . addCallback ( self . _set_lbryid ) <EOL> d . addCallback ( lambda _ : self . _get_lbrycrdd_path ( ) ) <EOL> return d <EOL> def _set_lbryid ( self , lbryid ) : <EOL> if lbryid is None : <EOL> return self . _make_lbryid ( ) <EOL> else : <EOL> log . info ( "<STR_LIT>" + base58 . b58encode ( lbryid ) ) <EOL> self . lbryid = lbryid <EOL> def _make_lbryid ( self ) : <EOL> self . lbryid = generate_id ( ) <EOL> log . info ( "<STR_LIT>" + base58 . b58encode ( self . lbryid ) ) <EOL> d = self . settings . save_lbryid ( self . lbryid ) <EOL> return d <EOL> def _setup_lbry_file_manager ( self ) : <EOL> self . startup_status = STARTUP_STAGES [ <NUM_LIT:3> ] <EOL> self . lbry_file_metadata_manager = DBLBRYFileMetadataManager ( self . db_dir ) <EOL> d = self . lbry_file_metadata_manager . setup ( ) <EOL> def set_lbry_file_manager ( ) : <EOL> self . lbry_file_manager = LBRYFileManager ( self . session , <EOL> self . lbry_file_metadata_manager , <EOL> self . sd_identifier , <EOL> delete_data = True ) <EOL> return self . lbry_file_manager . setup ( ) <EOL> d . addCallback ( lambda _ : set_lbry_file_manager ( ) ) <EOL> return d <EOL> def _get_session ( self ) : <EOL> def get_default_data_rate ( ) : <EOL> d = self . settings . get_default_data_payment_rate ( ) <EOL> d . addCallback ( lambda rate : { "<STR_LIT>" : rate if rate is not None else <EOL> MIN_BLOB_DATA_PAYMENT_RATE } ) <EOL> return d <EOL> def get_wallet ( ) : <EOL> if self . wallet_type == "<STR_LIT>" : <EOL> log . info ( "<STR_LIT>" ) <EOL> lbrycrdd_path = None <EOL> if self . start_lbrycrdd is True : <EOL> lbrycrdd_path = self . lbrycrdd_path <EOL> if not lbrycrdd_path : <EOL> lbrycrdd_path = self . default_lbrycrdd_path <EOL> d = defer . succeed ( LBRYcrdWallet ( self . db_dir , wallet_dir = self . wallet_dir , wallet_conf = self . lbrycrd_conf , <EOL> lbrycrdd_path = lbrycrdd_path ) ) <EOL> elif self . wallet_type == "<STR_LIT>" : <EOL> log . info ( "<STR_LIT>" ) <EOL> d = defer . succeed ( LBRYumWallet ( self . db_dir ) ) <EOL> elif self . wallet_type == "<STR_LIT>" : <EOL> log . info ( "<STR_LIT>" ) <EOL> d = defer . succeed ( PTCWallet ( self . db_dir ) ) <EOL> else : <EOL> d = defer . fail ( ) <EOL> d . addCallback ( lambda wallet : { "<STR_LIT>" : wallet } ) <EOL> return d <EOL> d1 = get_default_data_rate ( ) <EOL> d2 = get_wallet ( ) <EOL> def combine_results ( results ) : <EOL> r = { } <EOL> for success , result in results : <EOL> if success is True : <EOL> r . update ( result ) <EOL> return r <EOL> def create_session ( results ) : <EOL> self . session = LBRYSession ( results [ '<STR_LIT>' ] , db_dir = self . db_dir , lbryid = self . lbryid , <EOL> blob_dir = self . blobfile_dir , dht_node_port = self . dht_node_port , <EOL> known_dht_nodes = self . known_dht_nodes , peer_port = self . peer_port , <EOL> use_upnp = self . use_upnp , wallet = results [ '<STR_LIT>' ] ) <EOL> self . startup_status = STARTUP_STAGES [ <NUM_LIT:2> ] <EOL> dl = defer . DeferredList ( [ d1 , d2 ] , fireOnOneErrback = True ) <EOL> dl . addCallback ( combine_results ) <EOL> dl . addCallback ( create_session ) <EOL> dl . addCallback ( lambda _ : self . session . setup ( ) ) <EOL> return dl <EOL> def _check_first_run ( self ) : <EOL> def _set_first_run_false ( ) : <EOL> log . info ( "<STR_LIT>" ) <EOL> self . first_run = False <EOL> return <NUM_LIT:0.0> <EOL> d = self . session . wallet . is_first_run ( ) <EOL> d . addCallback ( lambda is_first_run : self . _do_first_run ( ) if is_first_run or not self . requested_first_run_credits <EOL> else _set_first_run_false ( ) ) <EOL> return d <EOL> def _do_first_run ( self ) : <EOL> def send_request ( url , data ) : <EOL> log . info ( "<STR_LIT>" ) <EOL> r = requests . post ( url , json = data ) <EOL> if r . status_code == <NUM_LIT:200> : <EOL> self . requested_first_run_credits = True <EOL> self . session_settings [ '<STR_LIT>' ] = True <EOL> f = open ( self . daemon_conf , "<STR_LIT:w>" ) <EOL> f . write ( json . dumps ( self . session_settings ) ) <EOL> f . close ( ) <EOL> return r . json ( ) [ '<STR_LIT>' ] <EOL> return <NUM_LIT:0.0> <EOL> def log_error ( err ) : <EOL> log . warning ( "<STR_LIT>" , err . getErrorMessage ( ) ) <EOL> return <NUM_LIT:0.0> <EOL> def request_credits ( address ) : <EOL> url = "<STR_LIT>" <EOL> data = { "<STR_LIT:address>" : address } <EOL> d = threads . deferToThread ( send_request , url , data ) <EOL> d . addErrback ( log_error ) <EOL> return d <EOL> self . first_run = True <EOL> d = self . session . wallet . get_new_address ( ) <EOL> d . addCallback ( request_credits ) <EOL> return d <EOL> def _show_first_run_result ( self , credits_received ) : <EOL> if credits_received != <NUM_LIT:0.0> : <EOL> points_string = locale . format_string ( "<STR_LIT>" , ( round ( credits_received , <NUM_LIT:2> ) , ) , grouping = True ) <EOL> self . startup_message = "<STR_LIT>" % points_string <EOL> else : <EOL> self . startup_message = None <EOL> def _get_lbrycrdd_path ( self ) : <EOL> def get_lbrycrdd_path_conf_file ( ) : <EOL> lbrycrdd_path_conf_path = os . path . join ( os . path . expanduser ( "<STR_LIT>" ) , "<STR_LIT>" ) <EOL> if not os . path . exists ( lbrycrdd_path_conf_path ) : <EOL> return "<STR_LIT>" <EOL> lbrycrdd_path_conf = open ( lbrycrdd_path_conf_path ) <EOL> lines = lbrycrdd_path_conf . readlines ( ) <EOL> return lines <EOL> d = threads . deferToThread ( get_lbrycrdd_path_conf_file ) <EOL> def load_lbrycrdd_path ( conf ) : <EOL> for line in conf : <EOL> if len ( line . strip ( ) ) and line . strip ( ) [ <NUM_LIT:0> ] != "<STR_LIT:#>" : <EOL> self . lbrycrdd_path = line . strip ( ) <EOL> d . addCallback ( load_lbrycrdd_path ) <EOL> return d <EOL> def _setup_stream_identifier ( self ) : <EOL> file_saver_factory = LBRYFileSaverFactory ( self . session . peer_finder , self . session . rate_limiter , <EOL> self . session . blob_manager , self . stream_info_manager , <EOL> self . session . wallet , self . session_settings [ '<STR_LIT>' ] ) <EOL> self . sd_identifier . add_stream_downloader_factory ( LBRYFileStreamType , file_saver_factory ) <EOL> file_opener_factory = LBRYFileOpenerFactory ( self . session . peer_finder , self . session . rate_limiter , <EOL> self . session . blob_manager , self . stream_info_manager , <EOL> self . session . wallet ) <EOL> self . sd_identifier . add_stream_downloader_factory ( LBRYFileStreamType , file_opener_factory ) <EOL> return defer . succeed ( None ) <EOL> def _setup_lbry_file_opener ( self ) : <EOL> downloader_factory = LBRYFileOpenerFactory ( self . session . peer_finder , self . session . rate_limiter , <EOL> self . session . blob_manager , self . stream_info_manager , <EOL> self . session . wallet ) <EOL> self . sd_identifier . add_stream_downloader_factory ( LBRYFileStreamType , downloader_factory ) <EOL> return defer . succeed ( True ) <EOL> def _download_name ( self , name , timeout = DEFAULT_TIMEOUT , download_directory = None ) : <EOL> if not download_directory : <EOL> download_directory = self . session_settings [ '<STR_LIT>' ] <EOL> elif not os . path . isdir ( download_directory ) : <EOL> download_directory = self . session_settings [ '<STR_LIT>' ] <EOL> def _disp_file ( f ) : <EOL> file_path = os . path . join ( self . session_settings [ '<STR_LIT>' ] , f . file_name ) <EOL> log . info ( "<STR_LIT:[>" + str ( datetime . now ( ) ) + "<STR_LIT>" + str ( f . stream_hash ) + "<STR_LIT>" + file_path ) <EOL> return defer . succeed ( f ) <EOL> def _get_stream ( name ) : <EOL> def _disp ( stream ) : <EOL> stream_hash = stream [ '<STR_LIT>' ] <EOL> if isinstance ( stream_hash , dict ) : <EOL> stream_hash = stream_hash [ '<STR_LIT>' ] <EOL> log . info ( "<STR_LIT:[>" + str ( datetime . now ( ) ) + "<STR_LIT>" + stream_hash ) <EOL> return stream <EOL> d = self . session . wallet . get_stream_info_for_name ( name ) <EOL> stream = GetStream ( self . sd_identifier , self . session , self . session . wallet , self . lbry_file_manager , <EOL> max_key_fee = self . max_key_fee , data_rate = self . data_rate , timeout = timeout , <EOL> download_directory = download_directory ) <EOL> d . addCallback ( _disp ) <EOL> d . addCallback ( lambda stream_info : stream . start ( stream_info ) ) <EOL> d . addCallback ( lambda _ : self . _path_from_name ( name ) ) <EOL> return d <EOL> d = self . _check_history ( name ) <EOL> d . addCallback ( lambda lbry_file : _get_stream ( name ) if not lbry_file else _disp_file ( lbry_file ) ) <EOL> d . addCallback ( lambda _ : self . _path_from_name ( name ) ) <EOL> d . addErrback ( lambda err : defer . fail ( NOT_FOUND ) ) <EOL> return d <EOL> def _resolve_name ( self , name ) : <EOL> d = defer . Deferred ( ) <EOL> d . addCallback ( lambda _ : self . session . wallet . get_stream_info_for_name ( name ) ) <EOL> d . addErrback ( lambda _ : defer . fail ( UnknownNameError ) ) <EOL> return d <EOL> def _resolve_name_wc ( self , name ) : <EOL> d = defer . Deferred ( ) <EOL> d . addCallback ( lambda _ : self . session . wallet . get_stream_info_for_name ( name ) ) <EOL> d . addErrback ( lambda _ : defer . fail ( UnknownNameError ) ) <EOL> d . callback ( None ) <EOL> return d <EOL> def _check_history ( self , name ) : <EOL> def _get_lbry_file ( path ) : <EOL> f = open ( path , '<STR_LIT:r>' ) <EOL> l = json . loads ( f . read ( ) ) <EOL> f . close ( ) <EOL> file_name = l [ '<STR_LIT>' ] . decode ( '<STR_LIT>' ) <EOL> for lbry_file in self . lbry_file_manager . lbry_files : <EOL> if lbry_file . stream_name == file_name : <EOL> if sys . platform == "<STR_LIT>" : <EOL> if os . path . isfile ( os . path . join ( self . session_settings [ '<STR_LIT>' ] , lbry_file . stream_name ) ) : <EOL> return lbry_file <EOL> else : <EOL> return False <EOL> else : <EOL> return lbry_file <EOL> else : <EOL> return False <EOL> def _check ( info ) : <EOL> stream_hash = info [ '<STR_LIT>' ] <EOL> if isinstance ( stream_hash , dict ) : <EOL> stream_hash = stream_hash [ '<STR_LIT>' ] <EOL> path = os . path . join ( self . blobfile_dir , stream_hash ) <EOL> if os . path . isfile ( path ) : <EOL> log . info ( "<STR_LIT:[>" + str ( datetime . now ( ) ) + "<STR_LIT>" + stream_hash ) <EOL> return defer . succeed ( _get_lbry_file ( path ) ) <EOL> else : <EOL> log . info ( "<STR_LIT:[>" + str ( datetime . now ( ) ) + "<STR_LIT>" ) <EOL> return defer . succeed ( False ) <EOL> d = self . _resolve_name ( name ) <EOL> d . addCallback ( _check ) <EOL> d . callback ( None ) <EOL> return d <EOL> def _delete_lbry_file ( self , lbry_file ) : <EOL> d = self . lbry_file_manager . delete_lbry_file ( lbry_file ) <EOL> def finish_deletion ( lbry_file ) : <EOL> d = lbry_file . delete_data ( ) <EOL> d . addCallback ( lambda _ : _delete_stream_data ( lbry_file ) ) <EOL> return d <EOL> def _delete_stream_data ( lbry_file ) : <EOL> s_h = lbry_file . stream_hash <EOL> d = self . lbry_file_manager . get_count_for_stream_hash ( s_h ) <EOL> d . addCallback ( lambda c : self . stream_info_manager . delete_stream ( s_h ) if c == <NUM_LIT:0> else True ) <EOL> d . addCallback ( lambda _ : os . remove ( os . path . join ( self . session_settings [ '<STR_LIT>' ] , lbry_file . file_name ) ) if <EOL> os . path . isfile ( os . path . join ( self . session_settings [ '<STR_LIT>' ] , lbry_file . file_name ) ) else defer . succeed ( None ) ) <EOL> return d <EOL> d . addCallback ( lambda _ : finish_deletion ( lbry_file ) ) <EOL> return d <EOL> def _path_from_name ( self , name ) : <EOL> d = self . _check_history ( name ) <EOL> d . addCallback ( lambda lbry_file : { '<STR_LIT>' : lbry_file . stream_hash , <EOL> '<STR_LIT:path>' : os . path . join ( self . session_settings [ '<STR_LIT>' ] , lbry_file . file_name ) } <EOL> if lbry_file else defer . fail ( UnknownNameError ) ) <EOL> return d <EOL> def _path_from_lbry_file ( self , lbry_file ) : <EOL> if lbry_file : <EOL> r = { '<STR_LIT>' : lbry_file . stream_hash , <EOL> '<STR_LIT:path>' : os . path . join ( self . session_settings [ '<STR_LIT>' ] , lbry_file . file_name ) } <EOL> return defer . succeed ( r ) <EOL> else : <EOL> return defer . fail ( UnknownNameError ) <EOL> def _get_est_cost ( self , name ) : <EOL> def _check_est ( d , name ) : <EOL> if type ( d . result ) is float : <EOL> log . info ( "<STR_LIT:[>" + str ( datetime . now ( ) ) + "<STR_LIT>" + name + "<STR_LIT>" + str ( d . result ) + "<STR_LIT>" ) <EOL> else : <EOL> log . info ( "<STR_LIT:[>" + str ( datetime . now ( ) ) + "<STR_LIT>" + name + "<STR_LIT>" ) <EOL> d . cancel ( ) <EOL> return defer . succeed ( None ) <EOL> def _add_key_fee ( data_cost ) : <EOL> d = self . session . wallet . get_stream_info_for_name ( name ) <EOL> d . addCallback ( lambda info : data_cost + info [ '<STR_LIT>' ] if '<STR_LIT>' in info . keys ( ) else data_cost ) <EOL> return d <EOL> d = self . session . wallet . get_stream_info_for_name ( name ) <EOL> d . addCallback ( lambda info : info [ '<STR_LIT>' ] if isinstance ( info [ '<STR_LIT>' ] , str ) <EOL> else info [ '<STR_LIT>' ] [ '<STR_LIT>' ] ) <EOL> d . addCallback ( lambda sd_hash : download_sd_blob ( self . session , sd_hash , <EOL> self . blob_request_payment_rate_manager ) ) <EOL> d . addCallback ( self . sd_identifier . get_metadata_for_sd_blob ) <EOL> d . addCallback ( lambda metadata : metadata . validator . info_to_show ( ) ) <EOL> d . addCallback ( lambda info : int ( dict ( info ) [ '<STR_LIT>' ] ) / <NUM_LIT> * self . data_rate ) <EOL> d . addCallback ( _add_key_fee ) <EOL> d . addErrback ( lambda _ : _add_key_fee ( <NUM_LIT:0.0> ) ) <EOL> reactor . callLater ( self . search_timeout , _check_est , d , name ) <EOL> return d <EOL> def _get_lbry_files ( self ) : <EOL> r = [ ] <EOL> for f in self . lbry_file_manager . lbry_files : <EOL> if f . key : <EOL> t = { '<STR_LIT>' : f . completed , '<STR_LIT>' : f . file_name , '<STR_LIT:key>' : binascii . b2a_hex ( f . key ) , <EOL> '<STR_LIT>' : f . points_paid , '<STR_LIT>' : f . stopped , '<STR_LIT>' : f . stream_hash , <EOL> '<STR_LIT>' : f . stream_name , '<STR_LIT>' : f . suggested_file_name , <EOL> '<STR_LIT>' : f . upload_allowed } <EOL> else : <EOL> t = { '<STR_LIT>' : f . completed , '<STR_LIT>' : f . file_name , '<STR_LIT:key>' : None , <EOL> '<STR_LIT>' : f . points_paid , <EOL> '<STR_LIT>' : f . stopped , '<STR_LIT>' : f . stream_hash , '<STR_LIT>' : f . stream_name , <EOL> '<STR_LIT>' : f . suggested_file_name , '<STR_LIT>' : f . upload_allowed } <EOL> r . append ( t ) <EOL> return r <EOL> def _render_response ( self , result , code ) : <EOL> return defer . succeed ( { '<STR_LIT:result>' : result , '<STR_LIT:code>' : code } ) <EOL> def jsonrpc_is_running ( self ) : <EOL> """<STR_LIT>""" <EOL> log . info ( "<STR_LIT:[>" + str ( datetime . now ( ) ) + "<STR_LIT>" + str ( self . announced_startup ) ) <EOL> if self . announced_startup : <EOL> return self . _render_response ( True , OK_CODE ) <EOL> else : <EOL> return self . _render_response ( False , OK_CODE ) <EOL> def jsonrpc_daemon_status ( self ) : <EOL> """<STR_LIT>""" <EOL> r = { '<STR_LIT:code>' : self . startup_status [ <NUM_LIT:0> ] , '<STR_LIT:message>' : self . startup_status [ <NUM_LIT:1> ] , '<STR_LIT>' : None , '<STR_LIT>' : None } <EOL> if self . startup_status [ <NUM_LIT:0> ] == '<STR_LIT>' : <EOL> r [ '<STR_LIT>' ] = self . session . wallet . is_lagging <EOL> if r [ '<STR_LIT>' ] == True : <EOL> r [ '<STR_LIT:message>' ] = "<STR_LIT>" <EOL> else : <EOL> r [ '<STR_LIT:message>' ] = r [ '<STR_LIT:message>' ] % ( str ( self . session . wallet . blocks_behind_alert ) + "<STR_LIT>" ) <EOL> r [ '<STR_LIT>' ] = self . session . wallet . catchup_progress <EOL> log . info ( "<STR_LIT:[>" + str ( datetime . now ( ) ) + "<STR_LIT>" + str ( r ) ) <EOL> return self . _render_response ( r , OK_CODE ) <EOL> def jsonrpc_is_first_run ( self ) : <EOL> """<STR_LIT>""" <EOL> log . info ( "<STR_LIT:[>" + str ( datetime . now ( ) ) + "<STR_LIT>" ) <EOL> try : <EOL> d = self . session . wallet . is_first_run ( ) <EOL> except : <EOL> d = defer . fail ( None ) <EOL> d . addCallbacks ( lambda r : self . _render_response ( r , OK_CODE ) , lambda _ : self . _render_response ( None , OK_CODE ) ) <EOL> return d <EOL> def jsonrpc_get_start_notice ( self ) : <EOL> """<STR_LIT>""" <EOL> log . info ( "<STR_LIT:[>" + str ( datetime . now ( ) ) + "<STR_LIT>" ) <EOL> if self . first_run and not self . session . wallet . wallet_balance : <EOL> return self . _render_response ( self . startup_message , OK_CODE ) <EOL> elif self . first_run : <EOL> return self . _render_response ( None , OK_CODE ) <EOL> else : <EOL> self . _render_response ( self . startup_message , OK_CODE ) <EOL> def jsonrpc_version ( self ) : <EOL> """<STR_LIT>""" <EOL> msg = { <EOL> '<STR_LIT>' : self . platform_info [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : self . platform_info [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : self . platform_info [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : lbrynet_version , <EOL> '<STR_LIT>' : lbryum_version , <EOL> '<STR_LIT>' : self . ui_version , <EOL> '<STR_LIT>' : self . git_lbrynet_version , <EOL> '<STR_LIT>' : self . git_lbryum_version <EOL> } <EOL> log . info ( "<STR_LIT:[>" + str ( datetime . now ( ) ) + "<STR_LIT>" + json . dumps ( msg ) ) <EOL> return self . _render_response ( msg , OK_CODE ) <EOL> def jsonrpc_get_settings ( self ) : <EOL> """<STR_LIT>""" <EOL> log . info ( "<STR_LIT:[>" + str ( datetime . now ( ) ) + "<STR_LIT>" ) <EOL> return self . _render_response ( self . session_settings , OK_CODE ) <EOL> def jsonrpc_set_settings ( self , p ) : <EOL> """<STR_LIT>""" <EOL> def _log_settings_change ( params ) : <EOL> log . info ( "<STR_LIT:[>" + str ( datetime . now ( ) ) + "<STR_LIT>" + str ( params ) ) <EOL> d = self . _update_settings ( p ) <EOL> d . addCallback ( lambda _ : _log_settings_change ( p ) ) <EOL> d . addCallback ( lambda _ : self . _render_response ( self . session_settings , OK_CODE ) ) <EOL> return d <EOL> def jsonrpc_help ( self , p = None ) : <EOL> """<STR_LIT>""" <EOL> if not p : <EOL> return self . _render_response ( self . _listFunctions ( ) , OK_CODE ) <EOL> elif '<STR_LIT>' in p . keys ( ) : <EOL> return self . _render_response ( ALLOWED_DURING_STARTUP , OK_CODE ) <EOL> elif '<STR_LIT>' in p . keys ( ) : <EOL> func_path = p [ '<STR_LIT>' ] <EOL> function = self . _getFunction ( func_path ) <EOL> return self . _render_response ( function . __doc__ , OK_CODE ) <EOL> else : <EOL> return self . _render_response ( self . jsonrpc_help . __doc__ , OK_CODE ) <EOL> def jsonrpc_start_fetcher ( self ) : <EOL> """<STR_LIT>""" <EOL> self . fetcher . start ( ) <EOL> log . info ( '<STR_LIT:[>' + str ( datetime . now ( ) ) + '<STR_LIT>' ) <EOL> return self . _render_response ( "<STR_LIT>" , OK_CODE ) <EOL> def jsonrpc_stop_fetcher ( self ) : <EOL> """<STR_LIT>""" <EOL> self . fetcher . stop ( ) <EOL> log . info ( '<STR_LIT:[>' + str ( datetime . now ( ) ) + '<STR_LIT>' ) <EOL> return self . _render_response ( "<STR_LIT>" , OK_CODE ) <EOL> def jsonrpc_fetcher_status ( self ) : <EOL> """<STR_LIT>""" <EOL> log . info ( "<STR_LIT:[>" + str ( datetime . now ( ) ) + "<STR_LIT>" ) <EOL> return self . _render_response ( self . fetcher . check_if_running ( ) , OK_CODE ) <EOL> def jsonrpc_get_balance ( self ) : <EOL> """<STR_LIT>""" <EOL> log . info ( "<STR_LIT:[>" + str ( datetime . now ( ) ) + "<STR_LIT>" ) <EOL> return self . _render_response ( float ( self . session . wallet . wallet_balance ) , OK_CODE ) <EOL> def jsonrpc_stop ( self ) : <EOL> """<STR_LIT>""" <EOL> def _disp_shutdown ( ) : <EOL> log . info ( "<STR_LIT>" ) <EOL> d = self . _shutdown ( ) <EOL> d . addCallback ( lambda _ : _disp_shutdown ( ) ) <EOL> d . addCallback ( lambda _ : reactor . callLater ( <NUM_LIT:1.0> , reactor . stop ) ) <EOL> return self . _render_response ( "<STR_LIT>" , OK_CODE ) <EOL> def jsonrpc_get_lbry_files ( self ) : <EOL> """<STR_LIT>""" <EOL> r = self . _get_lbry_files ( ) <EOL> log . info ( "<STR_LIT:[>" + str ( datetime . now ( ) ) + "<STR_LIT>" ) <EOL> return self . _render_response ( r , OK_CODE ) <EOL> def jsonrpc_resolve_name ( self , p ) : <EOL> """<STR_LIT>""" <EOL> if '<STR_LIT:name>' in p . keys ( ) : <EOL> name = p [ '<STR_LIT:name>' ] <EOL> else : <EOL> return self . _render_response ( None , BAD_REQUEST ) <EOL> def _disp ( info ) : <EOL> stream_hash = info [ '<STR_LIT>' ] <EOL> if isinstance ( stream_hash , dict ) : <EOL> stream_hash = stream_hash [ '<STR_LIT>' ] <EOL> log . info ( "<STR_LIT:[>" + str ( datetime . now ( ) ) + "<STR_LIT>" + stream_hash ) <EOL> return self . _render_response ( info , OK_CODE ) <EOL> d = self . _resolve_name ( name ) <EOL> d . addCallbacks ( _disp , lambda _ : server . failure ) <EOL> d . callback ( None ) <EOL> return d <EOL> def jsonrpc_get ( self , p ) : <EOL> """<STR_LIT>""" <EOL> if '<STR_LIT>' not in p . keys ( ) : <EOL> timeout = DEFAULT_TIMEOUT <EOL> else : <EOL> timeout = p [ '<STR_LIT>' ] <EOL> if '<STR_LIT>' not in p . keys ( ) : <EOL> download_directory = self . session_settings [ '<STR_LIT>' ] <EOL> else : <EOL> download_directory = p [ '<STR_LIT>' ] <EOL> if '<STR_LIT:name>' in p . keys ( ) : <EOL> name = p [ '<STR_LIT:name>' ] <EOL> d = self . _download_name ( name = name , timeout = timeout , download_directory = download_directory ) <EOL> d . addCallbacks ( lambda message : self . _render_response ( message , OK_CODE ) , <EOL> lambda err : self . _render_response ( '<STR_LIT:error>' , NOT_FOUND ) ) <EOL> else : <EOL> d = self . _render_response ( '<STR_LIT:error>' , BAD_REQUEST ) <EOL> return d <EOL> def jsonrpc_search_nametrie ( self , p ) : <EOL> """<STR_LIT>""" <EOL> if '<STR_LIT>' in p . keys ( ) : <EOL> search = p [ '<STR_LIT>' ] <EOL> else : <EOL> return self . _render_response ( None , BAD_REQUEST ) <EOL> def _clean ( n ) : <EOL> t = [ ] <EOL> for i in n : <EOL> if i [ <NUM_LIT:0> ] : <EOL> if i [ <NUM_LIT:1> ] [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] and i [ <NUM_LIT:1> ] [ <NUM_LIT:1> ] [ <NUM_LIT:0> ] and i [ <NUM_LIT:1> ] [ <NUM_LIT:2> ] [ <NUM_LIT:0> ] : <EOL> i [ <NUM_LIT:1> ] [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] [ '<STR_LIT:value>' ] = str ( i [ <NUM_LIT:1> ] [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] [ '<STR_LIT:value>' ] ) <EOL> t . append ( [ i [ <NUM_LIT:1> ] [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] , i [ <NUM_LIT:1> ] [ <NUM_LIT:1> ] [ <NUM_LIT:1> ] , i [ <NUM_LIT:1> ] [ <NUM_LIT:2> ] [ <NUM_LIT:1> ] ] ) <EOL> return t <EOL> def resolve_claims ( claims ) : <EOL> ds = [ ] <EOL> for claim in claims : <EOL> d1 = defer . succeed ( claim ) <EOL> d2 = self . _resolve_name_wc ( claim [ '<STR_LIT:name>' ] ) <EOL> d3 = self . _get_est_cost ( claim [ '<STR_LIT:name>' ] ) <EOL> dl = defer . DeferredList ( [ d1 , d2 , d3 ] , consumeErrors = True ) <EOL> ds . append ( dl ) <EOL> return defer . DeferredList ( ds ) <EOL> def _disp ( results ) : <EOL> log . info ( '<STR_LIT:[>' + str ( datetime . now ( ) ) + '<STR_LIT>' + str ( len ( results ) ) + '<STR_LIT>' ) <EOL> consolidated_results = [ ] <EOL> for r in results : <EOL> t = { } <EOL> t . update ( r [ <NUM_LIT:0> ] ) <EOL> if '<STR_LIT:name>' in r [ <NUM_LIT:1> ] . keys ( ) : <EOL> r [ <NUM_LIT:1> ] [ '<STR_LIT>' ] = r [ <NUM_LIT:1> ] [ '<STR_LIT:name>' ] <EOL> del r [ <NUM_LIT:1> ] [ '<STR_LIT:name>' ] <EOL> t . update ( r [ <NUM_LIT:1> ] ) <EOL> t [ '<STR_LIT>' ] = r [ <NUM_LIT:2> ] <EOL> consolidated_results . append ( t ) <EOL> return consolidated_results <EOL> log . info ( '<STR_LIT:[>' + str ( datetime . now ( ) ) + '<STR_LIT>' + search ) <EOL> d = self . session . wallet . get_nametrie ( ) <EOL> d . addCallback ( lambda trie : [ claim for claim in trie if claim [ '<STR_LIT:name>' ] . startswith ( search ) and '<STR_LIT>' in claim ] ) <EOL> d . addCallback ( lambda claims : claims [ : self . max_search_results ] ) <EOL> d . addCallback ( resolve_claims ) <EOL> d . addCallback ( _clean ) <EOL> d . addCallback ( _disp ) <EOL> d . addCallback ( lambda results : self . _render_response ( results , OK_CODE ) ) <EOL> return d <EOL> def jsonrpc_delete_lbry_file ( self , p ) : <EOL> """<STR_LIT>""" <EOL> def _disp ( file_name ) : <EOL> log . info ( "<STR_LIT:[>" + str ( datetime . now ( ) ) + "<STR_LIT>" + file_name ) <EOL> return self . _render_response ( "<STR_LIT>" + file_name , OK_CODE ) <EOL> if "<STR_LIT>" in p . keys ( ) : <EOL> lbry_files = [ self . _delete_lbry_file ( f ) for f in self . lbry_file_manager . lbry_files <EOL> if p [ '<STR_LIT>' ] == f . file_name ] <EOL> d = defer . DeferredList ( lbry_files ) <EOL> d . addCallback ( lambda _ : _disp ( p [ '<STR_LIT>' ] ) ) <EOL> return d <EOL> def jsonrpc_publish ( self , p ) : <EOL> """<STR_LIT>""" <EOL> metadata_fields = [ "<STR_LIT:name>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT:title>" , <EOL> "<STR_LIT:description>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" ] <EOL> for k in metadata_fields : <EOL> if k not in p . keys ( ) : <EOL> p [ k ] = None <EOL> pub = Publisher ( self . session , self . lbry_file_manager , self . session . wallet ) <EOL> d = pub . start ( p [ '<STR_LIT:name>' ] , <EOL> p [ '<STR_LIT>' ] , <EOL> p [ '<STR_LIT>' ] , <EOL> title = p [ '<STR_LIT:title>' ] , <EOL> description = p [ '<STR_LIT:description>' ] , <EOL> thumbnail = p [ '<STR_LIT>' ] , <EOL> key_fee = p [ '<STR_LIT>' ] , <EOL> key_fee_address = p [ '<STR_LIT>' ] , <EOL> content_license = p [ '<STR_LIT>' ] , <EOL> author = p [ '<STR_LIT>' ] , <EOL> sources = p [ '<STR_LIT>' ] ) <EOL> d . addCallbacks ( lambda msg : self . _render_response ( msg , OK_CODE ) , <EOL> lambda err : self . _render_response ( err . getTraceback ( ) , BAD_REQUEST ) ) <EOL> return d <EOL> def jsonrpc_abandon_name ( self , p ) : <EOL> """<STR_LIT>""" <EOL> if '<STR_LIT>' in p . keys ( ) : <EOL> txid = p [ '<STR_LIT>' ] <EOL> else : <EOL> return server . failure <EOL> def _disp ( x ) : <EOL> log . info ( "<STR_LIT:[>" + str ( datetime . now ( ) ) + "<STR_LIT>" + str ( x ) ) <EOL> return self . _render_response ( x , OK_CODE ) <EOL> d = defer . Deferred ( ) <EOL> d . addCallback ( lambda _ : self . session . wallet . abandon_name ( txid ) ) <EOL> d . addCallback ( _disp ) <EOL> d . callback ( None ) <EOL> return d <EOL> def jsonrpc_get_name_claims ( self ) : <EOL> """<STR_LIT>""" <EOL> def _clean ( claims ) : <EOL> for c in claims : <EOL> for k in c . keys ( ) : <EOL> if isinstance ( c [ k ] , Decimal ) : <EOL> c [ k ] = float ( c [ k ] ) <EOL> return defer . succeed ( claims ) <EOL> d = self . session . wallet . get_name_claims ( ) <EOL> d . addCallback ( _clean ) <EOL> d . addCallback ( lambda claims : self . _render_response ( claims , OK_CODE ) ) <EOL> return d <EOL> def jsonrpc_get_time_behind_blockchain ( self ) : <EOL> """<STR_LIT>""" <EOL> def _get_time_behind ( ) : <EOL> try : <EOL> local_height = self . session . wallet . network . get_local_height ( ) <EOL> remote_height = self . session . wallet . network . get_server_height ( ) <EOL> return defer . succeed ( remote_height - local_height ) <EOL> except : <EOL> return defer . fail ( ) <EOL> d = _get_time_behind ( ) <EOL> d . addCallback ( lambda r : self . _render_response ( r , OK_CODE ) ) <EOL> return d <EOL> def jsonrpc_get_new_address ( self ) : <EOL> """<STR_LIT>""" <EOL> def _disp ( address ) : <EOL> log . info ( "<STR_LIT:[>" + str ( datetime . now ( ) ) + "<STR_LIT>" + address ) <EOL> return defer . succeed ( address ) <EOL> d = self . session . wallet . get_new_address ( ) <EOL> d . addCallback ( _disp ) <EOL> d . addCallback ( lambda address : self . _render_response ( address , OK_CODE ) ) <EOL> return d <EOL> def jsonrpc_toggle_fetcher_verbose ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . fetcher . verbose : <EOL> self . fetcher . verbose = False <EOL> else : <EOL> self . fetcher . verbose = True <EOL> return self . _render_response ( self . fetcher . verbose , OK_CODE ) <EOL> def jsonrpc_check_for_new_version ( self ) : <EOL> """<STR_LIT>""" <EOL> def _check_version ( ) : <EOL> if ( lbrynet_version >= self . git_lbrynet_version ) and ( lbryum_version >= self . git_lbryum_version ) : <EOL> log . info ( "<STR_LIT:[>" + str ( datetime . now ( ) ) + "<STR_LIT>" ) <EOL> return self . _render_response ( False , OK_CODE ) <EOL> else : <EOL> log . info ( "<STR_LIT:[>" + str ( datetime . now ( ) ) + "<STR_LIT>" ) <EOL> return self . _render_response ( True , OK_CODE ) <EOL> return _check_version ( ) <EOL> def jsonrpc_upload_log ( self , p = None ) : <EOL> if p : <EOL> if '<STR_LIT>' in p . keys ( ) : <EOL> prefix = p [ '<STR_LIT>' ] + '<STR_LIT>' <EOL> else : <EOL> prefix = None <EOL> if '<STR_LIT>' in p . keys : <EOL> exclude_previous = p [ '<STR_LIT>' ] <EOL> else : <EOL> exclude_previous = True <EOL> else : <EOL> prefix = "<STR_LIT>" <EOL> exclude_previous = True <EOL> d = self . _upload_log ( name_prefix = prefix , exclude_previous = exclude_previous ) <EOL> d . addCallback ( lambda _ : self . _render_response ( True , OK_CODE ) ) <EOL> return d <EOL> class LBRYDaemonCommandHandler ( object ) : <EOL> def __init__ ( self , command ) : <EOL> self . _api = jsonrpc . Proxy ( API_CONNECTION_STRING ) <EOL> self . command = command <EOL> def run ( self , params = None ) : <EOL> if params : <EOL> d = self . _api . callRemote ( self . command , params ) <EOL> else : <EOL> d = self . _api . callRemote ( self . command ) <EOL> return d <EOL> class LBRYindex ( resource . Resource ) : <EOL> def __init__ ( self , ui_dir ) : <EOL> resource . Resource . __init__ ( self ) <EOL> self . ui_dir = ui_dir <EOL> isLeaf = False <EOL> def _delayed_render ( self , request , results ) : <EOL> request . write ( str ( results ) ) <EOL> request . finish ( ) <EOL> def getChild ( self , name , request ) : <EOL> if name == '<STR_LIT>' : <EOL> return self <EOL> return resource . Resource . getChild ( self , name , request ) <EOL> def render_GET ( self , request ) : <EOL> return static . File ( os . path . join ( self . ui_dir , "<STR_LIT>" ) ) . render_GET ( request ) <EOL> class LBRYFileRender ( resource . Resource ) : <EOL> isLeaf = False <EOL> def render_GET ( self , request ) : <EOL> if '<STR_LIT:name>' in request . args . keys ( ) : <EOL> api = jsonrpc . Proxy ( API_CONNECTION_STRING ) <EOL> if request . args [ '<STR_LIT:name>' ] [ <NUM_LIT:0> ] != '<STR_LIT>' : <EOL> d = api . callRemote ( "<STR_LIT>" , { '<STR_LIT:name>' : request . args [ '<STR_LIT:name>' ] [ <NUM_LIT:0> ] } ) <EOL> d . addCallback ( lambda results : static . File ( results [ '<STR_LIT:path>' ] ) . render_GET ( request ) ) <EOL> else : <EOL> request . redirect ( UI_ADDRESS ) <EOL> request . finish ( ) <EOL> return server . NOT_DONE_YET <EOL> else : <EOL> return server . failure </s>
