Este es un módulo simple que recuperan los valores de las monedas de criptografía instantáneas en BTC y USD de Coin360.com y las API de CoinMarketCap
Primero instale el paquete
pip install cryptocurrencies_scraper
from cryptocurrencies_scraper . CurrencyService import Manager
manager = Manager () # Create a new instance of the Manager
manager . update () # Ask the manager to update all currencies values
manager . all_currencies () # returns a dict that contain the currency name and symbol as key and currency information as a list
manager . get_curency ( 'btc' ) # return the list of currency values retrived for bitcoin using symbol
manager . get_curency ( 'bitcoin' ) # return the list of currency values retrived for bitcoin using name
Un raspador debe extender la CurrentPriceInterface
. Aquí hay un esquema básico para un nuevo raspador
class MySourceForCurrencies ( CurrentPriceInterface ):
instance = None
URL = 'https://api.mysource.com/v1/ticker/?limit=3000'
NAME = "mysource.com"
@ classmethod
def get_instance ( cls ) -> 'MySourceForCurrencies' :
if cls . instance is None :
cls . instance = MySourceForCurrencies ()
return cls . instance
def __init__ ( self ):
super (). __init__ ()
def update_currency_list ( self ):
self . process_data ( requests . get ( self . URL ). json ())
def process_data ( self , json_data : json ) -> None :
# Manipulate the the json to be Currency "parsable"
c = Currency . parse_json ( json_data , self . NAME )
#When parsed, add it to the index
NameIndexes . get_instance (). add_to_index ( c )
return None
Para que su raspador esté trabajando, debe agregarlo al administrador
from cryptocurrencies_scraper . CurrencyService import Manager
manager = Manager ()
manager . add_source ( MySourceForCurrencies . get_instance ())
# you can also remove a source
manager . remove_source ( MySourceForCurrencies . NAME )
Luego, en cada manager.update()
llame a su raspador se llamará y se agregará monedas al índice
Esta clase se usa internamente para indexar todas las monedas. Cuando se agrega una nueva moneda, se agrupará por el símbolo y el nombre (símbolo superior, nombre minúscula)
El único método útil es NameIndexes.get_instance().add_to_index(currency)
. En cada actualización, se elimina completamente de todos los valores antiguos.
Currency
Este es el objeto que representa una moneda aquí es la estructura
{
"symbol" : " BTC " ,
"name" : " Bitcoin " ,
"valueUSD" : 5513 ,
"valueBTC" : 1 ,
"lastUpdate" : 1566531513813 ,
"source" : " coin360.com " ,
"changes" : {
"7d" : -1 ,
"24h" : -5 ,
"1h" : -10
}
}