Coin360.com 및 CoinmarketCap API에서 BTC 및 USD의 즉각적인 암호화 통화 값을 검색하는 간단한 모듈입니다.
먼저 패키지를 설치하십시오
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
스크레이퍼는 CurrentPriceInterface
를 확장해야합니다. 다음은 새 스크레이퍼의 기본 체계입니다
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
스크레이퍼가 작동하려면 관리자에게 추가해야합니다.
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 )
그런 다음 각 manager.update()
에서 스크레이퍼를 호출하고 통화가 인덱스에 추가됩니다.
이 클래스는 내부적으로 모든 통화를 색인화하는 데 사용됩니다. 새 통화가 추가되면 기호와 이름으로 그룹화됩니다 (기호 위, 이름 소문자).
유일한 유용한 방법은 NameIndexes.get_instance().add_to_index(currency)
입니다. 각 업데이트마다 모든 오래된 값에서 완전하게 지워집니다.
Currency
이것은 여기서 하나의 통화를 나타내는 객체입니다.
{
"symbol" : " BTC " ,
"name" : " Bitcoin " ,
"valueUSD" : 5513 ,
"valueBTC" : 1 ,
"lastUpdate" : 1566531513813 ,
"source" : " coin360.com " ,
"changes" : {
"7d" : -1 ,
"24h" : -5 ,
"1h" : -10
}
}