am_dot_com

FP 2021-12-06

Dec 6th, 2021 (edited)
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.34 KB | None | 0 0
  1. #BigConsumer.py
  2. from urllib.request import urlopen
  3.  
  4. from enum import IntEnum
  5.  
  6. class Markets(IntEnum):
  7. PSI20 = 1, #implemented
  8. IBEX35 = 2, #TODO
  9. CAC40 = 3, #TODO
  10. XETRA = 4 #TODO
  11. #... #TODO
  12. #class Markets
  13.  
  14. class BigConsumer:
  15. """
  16. <span class="stock-item-closePrice">5,205 EUR</span>
  17. <h3 class="dark-grey show" id="H3">ALTRI SGPS (ALTR)</h3>
  18. """
  19. MARK_CLOSE_PRICE = "<span class=\"stock-item-closePrice\">"
  20. MARK_TICKER = "<h3 class=\"dark-grey show\" id=\"H3\">"
  21. MARK_CLOSE = "</"
  22. URL_BASE = "https://big.pt/Reports/StockInfo/StockInfoDetails/"
  23.  
  24. def __init__(self, piMarket):
  25. self.mMarket = piMarket
  26. self.mData = "" #HTML read from the big.pt site
  27. self.mProduct = ""
  28. #def __init__
  29.  
  30. """
  31. https://big.pt/Reports/StockInfo/StockInfoDetails/ALT AE
  32. https://big.pt/Reports/StockInfo/StockInfoDetails/BCP AM
  33. https://big.pt/Reports/StockInfo/StockInfoDetails/COR AE
  34. https://big.pt/Reports/StockInfo/StockInfoDetails/CTT AM
  35. https://big.pt/Reports/StockInfo/StockInfoDetails/EDP AM
  36. https://big.pt/Reports/StockInfo/StockInfoDetails/EGL AE
  37. https://big.pt/Reports/StockInfo/StockInfoDetails/FRV AE
  38. https://big.pt/Reports/StockInfo/StockInfoDetails/GAL AM
  39. https://big.pt/Reports/StockInfo/StockInfoDetails/GNV AM
  40. https://big.pt/Reports/StockInfo/StockInfoDetails/IBS AM
  41. https://big.pt/Reports/StockInfo/StockInfoDetails/JMT AE
  42. https://big.pt/Reports/StockInfo/StockInfoDetails/KER AM
  43. https://big.pt/Reports/StockInfo/StockInfoDetails/NBA AM
  44. https://big.pt/Reports/StockInfo/StockInfoDetails/PTC AM
  45. https://big.pt/Reports/StockInfo/StockInfoDetails/PTI AM
  46. https://big.pt/Reports/StockInfo/StockInfoDetails/REL AM
  47. https://big.pt/Reports/StockInfo/StockInfoDetails/SEM AM
  48. https://big.pt/Reports/StockInfo/StockInfoDetails/SON AM
  49. https://big.pt/Reports/StockInfo/StockInfoDetails/ZON AM
  50. """
  51.  
  52. def productUrl (self, pStrProduct):
  53. dictSupportedProducts = BigConsumer.getProducts()
  54. dictKeys = dictSupportedProducts.keys() #"ALTRI", "BCP"
  55. listKeys = list(dictKeys) #["ALTRI"@0 , ... , "ZON"@??]
  56. bSupportedProduct = pStrProduct in listKeys
  57. if (bSupportedProduct):
  58. strBigSpecific = dictSupportedProducts[pStrProduct]
  59. strUrl = "%s%s"%(BigConsumer.URL_BASE, strBigSpecific)
  60. return strUrl
  61. #if
  62. return False
  63. #def productUrl
  64.  
  65. @staticmethod
  66. def getProducts():
  67. dictProducts = {}
  68. dictProducts["ALTRI"] = "ALT%20AE"
  69. dictProducts["BCP"] = "BCP%20AM"
  70. dictProducts["EDP"] = "EDP%20AM"
  71. #... TODO
  72. dictProducts["SONAE"] = "SON%20AM"
  73. dictProducts["ZON"] = "ZON%20AM"
  74. return dictProducts
  75. #def getProducts
  76.  
  77.  
  78. @staticmethod
  79. def readUrl(pStrUrl):
  80. request = urlopen(pStrUrl)
  81. bytesRead = request.read()
  82. strData = bytesRead.decode("UTF-8")
  83. return strData
  84. #def readUrl
  85.  
  86. def productReader(self, pStrProduct):
  87. strProductUrl = self.productUrl(pStrProduct)
  88. if (strProductUrl!=False):
  89. #product exists
  90. strData = BigConsumer.readUrl(strProductUrl)
  91. self.mData = strData
  92. self.mProduct = pStrProduct
  93. #if
  94. return False
  95. #def productReader
  96.  
  97. def mustReloadFromWeb (self, pStrProduct):
  98. bMustLoadFromWeb = self.mData == "" or \
  99. (self.mData != "" and \
  100. self.mProduct != pStrProduct)
  101. return bMustLoadFromWeb
  102. #def mustReloadFromWeb
  103.  
  104. def getClosingPrice (self, pStrProduct):
  105. bMustReload = self.mustReloadFromWeb(pStrProduct)
  106. if (bMustReload):
  107. self.productReader(pStrProduct)
  108. #if
  109. if (self.mData!=""):
  110. strData = self.dataExtractor\
  111. (BigConsumer.MARK_CLOSE_PRICE)
  112. return strData
  113. #if
  114. return False
  115. #def getClosingPrice
  116.  
  117. def getTicker (self, pStrProduct):
  118. bMustReload = self.mustReloadFromWeb(pStrProduct)
  119. if (bMustReload):
  120. self.productReader(pStrProduct)
  121. # if
  122. if (self.mData != ""):
  123. strData = self.dataExtractor\
  124. (BigConsumer.MARK_TICKER)
  125. return strData
  126. # if
  127. return False
  128. #def getTicker
  129.  
  130. #"Artur".find("r") ---> 1
  131. def dataExtractor(self, pStrMark):
  132. if (self.mData!=""):
  133. indexOfMarkStart = self.mData.find(pStrMark)
  134. if (indexOfMarkStart!=-1):
  135. strRelevant = self.mData[
  136. indexOfMarkStart + len(pStrMark)
  137. :
  138. ]
  139.  
  140. iEndIndex = strRelevant.find(BigConsumer.MARK_CLOSE)
  141. if (iEndIndex!=-1):
  142. strWanted = strRelevant[0:iEndIndex]
  143. return strWanted
  144. #if there is an end index
  145. #if there is a start index
  146. #if there is data
  147. return False
  148. #def dataExtractor
  149. #class BigConsumer
  150.  
  151. c1 = BigConsumer(Markets.PSI20) #OK
  152. supportedProducts = BigConsumer.getProducts()
  153. print (supportedProducts)
  154. #print (c1.productUrl("ZON")) #OK
  155. print (c1.getClosingPrice("ZON")) #5.20
  156. print (c1.getTicker("ZON")) #"ALTR"
  157.  
  158. #"Artur"[2:4] => "tu"
Advertisement
Add Comment
Please, Sign In to add comment