Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #BigConsumer.py
- from urllib.request import urlopen
- from enum import IntEnum
- class Markets(IntEnum):
- PSI20 = 1, #implemented
- IBEX35 = 2, #TODO
- CAC40 = 3, #TODO
- XETRA = 4 #TODO
- #... #TODO
- #class Markets
- class BigConsumer:
- """
- <span class="stock-item-closePrice">5,205 EUR</span>
- <h3 class="dark-grey show" id="H3">ALTRI SGPS (ALTR)</h3>
- """
- MARK_CLOSE_PRICE = "<span class=\"stock-item-closePrice\">"
- MARK_TICKER = "<h3 class=\"dark-grey show\" id=\"H3\">"
- MARK_CLOSE = "</"
- URL_BASE = "https://big.pt/Reports/StockInfo/StockInfoDetails/"
- def __init__(self, piMarket):
- self.mMarket = piMarket
- self.mData = "" #HTML read from the big.pt site
- self.mProduct = ""
- #def __init__
- """
- https://big.pt/Reports/StockInfo/StockInfoDetails/ALT AE
- https://big.pt/Reports/StockInfo/StockInfoDetails/BCP AM
- https://big.pt/Reports/StockInfo/StockInfoDetails/COR AE
- https://big.pt/Reports/StockInfo/StockInfoDetails/CTT AM
- https://big.pt/Reports/StockInfo/StockInfoDetails/EDP AM
- https://big.pt/Reports/StockInfo/StockInfoDetails/EGL AE
- https://big.pt/Reports/StockInfo/StockInfoDetails/FRV AE
- https://big.pt/Reports/StockInfo/StockInfoDetails/GAL AM
- https://big.pt/Reports/StockInfo/StockInfoDetails/GNV AM
- https://big.pt/Reports/StockInfo/StockInfoDetails/IBS AM
- https://big.pt/Reports/StockInfo/StockInfoDetails/JMT AE
- https://big.pt/Reports/StockInfo/StockInfoDetails/KER AM
- https://big.pt/Reports/StockInfo/StockInfoDetails/NBA AM
- https://big.pt/Reports/StockInfo/StockInfoDetails/PTC AM
- https://big.pt/Reports/StockInfo/StockInfoDetails/PTI AM
- https://big.pt/Reports/StockInfo/StockInfoDetails/REL AM
- https://big.pt/Reports/StockInfo/StockInfoDetails/SEM AM
- https://big.pt/Reports/StockInfo/StockInfoDetails/SON AM
- https://big.pt/Reports/StockInfo/StockInfoDetails/ZON AM
- """
- def productUrl (self, pStrProduct):
- dictSupportedProducts = BigConsumer.getProducts()
- dictKeys = dictSupportedProducts.keys() #"ALTRI", "BCP"
- listKeys = list(dictKeys) #["ALTRI"@0 , ... , "ZON"@??]
- bSupportedProduct = pStrProduct in listKeys
- if (bSupportedProduct):
- strBigSpecific = dictSupportedProducts[pStrProduct]
- strUrl = "%s%s"%(BigConsumer.URL_BASE, strBigSpecific)
- return strUrl
- #if
- return False
- #def productUrl
- @staticmethod
- def getProducts():
- dictProducts = {}
- dictProducts["ALTRI"] = "ALT%20AE"
- dictProducts["BCP"] = "BCP%20AM"
- dictProducts["EDP"] = "EDP%20AM"
- #... TODO
- dictProducts["SONAE"] = "SON%20AM"
- dictProducts["ZON"] = "ZON%20AM"
- return dictProducts
- #def getProducts
- @staticmethod
- def readUrl(pStrUrl):
- request = urlopen(pStrUrl)
- bytesRead = request.read()
- strData = bytesRead.decode("UTF-8")
- return strData
- #def readUrl
- def productReader(self, pStrProduct):
- strProductUrl = self.productUrl(pStrProduct)
- if (strProductUrl!=False):
- #product exists
- strData = BigConsumer.readUrl(strProductUrl)
- self.mData = strData
- self.mProduct = pStrProduct
- #if
- return False
- #def productReader
- def mustReloadFromWeb (self, pStrProduct):
- bMustLoadFromWeb = self.mData == "" or \
- (self.mData != "" and \
- self.mProduct != pStrProduct)
- return bMustLoadFromWeb
- #def mustReloadFromWeb
- def getClosingPrice (self, pStrProduct):
- bMustReload = self.mustReloadFromWeb(pStrProduct)
- if (bMustReload):
- self.productReader(pStrProduct)
- #if
- if (self.mData!=""):
- strData = self.dataExtractor\
- (BigConsumer.MARK_CLOSE_PRICE)
- return strData
- #if
- return False
- #def getClosingPrice
- def getTicker (self, pStrProduct):
- bMustReload = self.mustReloadFromWeb(pStrProduct)
- if (bMustReload):
- self.productReader(pStrProduct)
- # if
- if (self.mData != ""):
- strData = self.dataExtractor\
- (BigConsumer.MARK_TICKER)
- return strData
- # if
- return False
- #def getTicker
- #"Artur".find("r") ---> 1
- def dataExtractor(self, pStrMark):
- if (self.mData!=""):
- indexOfMarkStart = self.mData.find(pStrMark)
- if (indexOfMarkStart!=-1):
- strRelevant = self.mData[
- indexOfMarkStart + len(pStrMark)
- :
- ]
- iEndIndex = strRelevant.find(BigConsumer.MARK_CLOSE)
- if (iEndIndex!=-1):
- strWanted = strRelevant[0:iEndIndex]
- return strWanted
- #if there is an end index
- #if there is a start index
- #if there is data
- return False
- #def dataExtractor
- #class BigConsumer
- c1 = BigConsumer(Markets.PSI20) #OK
- supportedProducts = BigConsumer.getProducts()
- print (supportedProducts)
- #print (c1.productUrl("ZON")) #OK
- print (c1.getClosingPrice("ZON")) #5.20
- print (c1.getTicker("ZON")) #"ALTR"
- #"Artur"[2:4] => "tu"
Advertisement
Add Comment
Please, Sign In to add comment