هذه وحدة بسيطة تسترجع قيم عملات التشفير الفورية في BTC و USD من Coin360.com و CoinmarketCap APIs
قم بتثبيت الحزمة أولاً
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
}
}