sarim commited on
Commit
7fcf369
·
1 Parent(s): 9d0fa54

update dividend

Browse files
Files changed (2) hide show
  1. models.py +17 -2
  2. ticker_detail.py +101 -13
models.py CHANGED
@@ -909,11 +909,26 @@ class ETF(BaseModel):
909
 
910
  class Dividend(BaseModel):
911
  announcement_date: str
912
- financial_results: str
913
- details: str
 
 
 
914
  book_closure_start: Optional[str] = None
915
  book_closure_end: Optional[str] = None
916
 
 
 
 
 
 
 
 
 
 
 
 
 
917
  class TickerData(BaseModel):
918
  market: str
919
  st: str
 
909
 
910
  class Dividend(BaseModel):
911
  announcement_date: str
912
+ period_end: Optional[str] = None
913
+ period: Optional[str] = None
914
+ percentage: Optional[float] = None
915
+ installment: Optional[str] = None
916
+ type: Optional[str] = None
917
  book_closure_start: Optional[str] = None
918
  book_closure_end: Optional[str] = None
919
 
920
+ ###
921
+ # announcement_date": announcement,
922
+ # "period_end": period_end,
923
+ # "period": period,
924
+ # "percentage": percentage,
925
+ # "installment": installment,
926
+ # "type": dividend_type,
927
+ # "book_closure_start": start,
928
+ # "book_closure_end": end,
929
+
930
+ ##
931
+
932
  class TickerData(BaseModel):
933
  market: str
934
  st: str
ticker_detail.py CHANGED
@@ -7,6 +7,27 @@ import re
7
  import httpx
8
  from typing import Dict, List, Optional
9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
 
11
  class PSXScraper:
12
 
@@ -153,6 +174,11 @@ class PSXScraper:
153
  self.data["key_people"] = people
154
 
155
 
 
 
 
 
 
156
  def parse_payouts(self):
157
  url = "https://dps.psx.com.pk/company/payouts"
158
 
@@ -169,13 +195,11 @@ class PSXScraper:
169
  data={"symbol": self.symbol},
170
  timeout=30,
171
  )
172
-
173
  response.raise_for_status()
174
 
175
  soup = BeautifulSoup(response.text, "lxml")
176
 
177
  table = soup.select_one("table.tbl")
178
-
179
  if table is None:
180
  self.data["dividends"] = []
181
  return
@@ -188,23 +212,88 @@ class PSXScraper:
188
  if len(cols) != 4:
189
  continue
190
 
191
- announcement_date = cols[0]
192
- financial_results = cols[1]
193
- details = cols[2]
194
- book_closure = cols[3]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
195
 
 
 
 
 
 
 
 
 
196
  start = None
197
  end = None
198
 
199
- if book_closure != "-":
200
- parts = re.split(r"\s*-\s*", book_closure, maxsplit=1)
 
201
  if len(parts) == 2:
202
- start, end = parts
 
 
 
 
 
 
 
 
203
 
204
  dividends.append({
205
- "announcement_date": announcement_date,
206
- "financial_results": financial_results,
207
- "details": details,
 
 
 
208
  "book_closure_start": start,
209
  "book_closure_end": end,
210
  })
@@ -270,7 +359,6 @@ def clean_range(value: str) -> str:
270
 
271
 
272
  def map_to_ticker_data(raw: dict, symbol: str) -> TickerData:
273
- print(raw)
274
  key_people_list = [
275
  KeyPerson(
276
  position=item["designation"],
 
7
  import httpx
8
  from typing import Dict, List, Optional
9
 
10
+ PERIOD_MAP = {
11
+ "IQ": "First Quarter",
12
+ "HYR": "Half Year",
13
+ "IIIQ": "Third Quarter",
14
+ "YR": "Full Year",
15
+ }
16
+
17
+ ROMAN_MAP = {
18
+ "i": "First",
19
+ "ii": "Second",
20
+ "iii": "Third",
21
+ "iv": "Fourth",
22
+ "v": "Fifth",
23
+ "F":"Final"
24
+ }
25
+
26
+ DIVIDEND_TYPE = {
27
+ "D": "Dividend",
28
+ "B": "Bonus",
29
+ "R": "Rights",
30
+ }
31
 
32
  class PSXScraper:
33
 
 
174
  self.data["key_people"] = people
175
 
176
 
177
+
178
+
179
+
180
+
181
+
182
  def parse_payouts(self):
183
  url = "https://dps.psx.com.pk/company/payouts"
184
 
 
195
  data={"symbol": self.symbol},
196
  timeout=30,
197
  )
 
198
  response.raise_for_status()
199
 
200
  soup = BeautifulSoup(response.text, "lxml")
201
 
202
  table = soup.select_one("table.tbl")
 
203
  if table is None:
204
  self.data["dividends"] = []
205
  return
 
212
  if len(cols) != 4:
213
  continue
214
 
215
+ # -----------------------------
216
+ # Announcement Date
217
+ # -----------------------------
218
+ announcement = datetime.strptime(
219
+ cols[0],
220
+ "%B %d, %Y %I:%M %p"
221
+ ).isoformat()
222
+
223
+ # -----------------------------
224
+ # Financial Results
225
+ # Example: 31/03/2026(IIIQ)
226
+ # -----------------------------
227
+ period_end = None
228
+ period = None
229
+
230
+ m = re.match(r"(\d{2}/\d{2}/\d{4})\((.*?)\)", cols[1])
231
+
232
+ if m:
233
+ period_end = datetime.strptime(
234
+ m.group(1),
235
+ "%d/%m/%Y"
236
+ ).date().isoformat()
237
+
238
+ period = PERIOD_MAP.get(m.group(2), m.group(2))
239
+
240
+ # -----------------------------
241
+ # Details
242
+ # Example:
243
+ # 80%(iii) (D)
244
+ # 100%(F) (D)
245
+ # -----------------------------
246
+ percentage = None
247
+ installment = None
248
+ dividend_type = None
249
+
250
+ m = re.search(
251
+ r"([\d.]+)%(?:\((.*?)\))?\s*\((.*?)\)",
252
+ cols[2]
253
+ )
254
+
255
+ if m:
256
+ percentage = float(m.group(1))
257
+
258
+ installment = m.group(2)
259
+ if installment:
260
+ installment = ROMAN_MAP.get(
261
+ installment.lower(),
262
+ installment
263
+ )
264
 
265
+ dividend_type = DIVIDEND_TYPE.get(
266
+ m.group(3),
267
+ m.group(3)
268
+ )
269
+
270
+ # -----------------------------
271
+ # Book Closure
272
+ # -----------------------------
273
  start = None
274
  end = None
275
 
276
+ if cols[3] != "-":
277
+ parts = re.split(r"\s*-\s*", cols[3], maxsplit=1)
278
+
279
  if len(parts) == 2:
280
+ start = datetime.strptime(
281
+ parts[0],
282
+ "%d/%m/%Y"
283
+ ).date().isoformat()
284
+
285
+ end = datetime.strptime(
286
+ parts[1],
287
+ "%d/%m/%Y"
288
+ ).date().isoformat()
289
 
290
  dividends.append({
291
+ "announcement_date": announcement,
292
+ "period_end": period_end,
293
+ "period": period,
294
+ "percentage": percentage,
295
+ "installment": installment,
296
+ "type": dividend_type,
297
  "book_closure_start": start,
298
  "book_closure_end": end,
299
  })
 
359
 
360
 
361
  def map_to_ticker_data(raw: dict, symbol: str) -> TickerData:
 
362
  key_people_list = [
363
  KeyPerson(
364
  position=item["designation"],