eshan6704 commited on
Commit
d8f5cc7
·
verified ·
1 Parent(s): 4e0feff

Create info.py

Browse files
Files changed (1) hide show
  1. info.py +164 -0
info.py ADDED
@@ -0,0 +1,164 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # info.py
2
+ import yfinance as yf
3
+ from common import format_large_number, format_timestamp_to_date
4
+
5
+ def fetch_info(symbol):
6
+ STYLE_BLOCK = """
7
+ <style>
8
+ .styled-table {
9
+ border-collapse: collapse;
10
+ margin: 10px 0;
11
+ font-size: 0.9em;
12
+ font-family: sans-serif;
13
+ width: 100%;
14
+ box-shadow: 0 0 10px rgba(0,0,0,0.1);
15
+ }
16
+ .styled-table th, .styled-table td {
17
+ padding: 8px 10px;
18
+ border: 1px solid #ddd;
19
+ }
20
+ .styled-table tbody tr:nth-child(even) {
21
+ background-color: #f9f9f9;
22
+ }
23
+ .card {
24
+ display: block;
25
+ width: 95%;
26
+ margin: 10px auto;
27
+ padding: 15px;
28
+ border: 1px solid #ddd;
29
+ border-radius: 8px;
30
+ box-shadow: 0 2px 5px rgba(0,0,0,0.1);
31
+ background: #fafafa;
32
+ }
33
+ .card-category-title {
34
+ font-size: 1.1em;
35
+ color: #222;
36
+ margin: 0 0 8px;
37
+ border-bottom: 1px solid #eee;
38
+ padding-bottom: 5px;
39
+ }
40
+ .card-content-grid {
41
+ display: flex;
42
+ flex-wrap: wrap;
43
+ gap: 15px;
44
+ }
45
+ .key-value-pair {
46
+ flex: 1 1 calc(20% - 15px);
47
+ box-sizing: border-box;
48
+ min-width: 150px;
49
+ background: #fff;
50
+ padding: 10px;
51
+ border: 1px solid #e0e0e0;
52
+ border-radius: 5px;
53
+ box-shadow: 0 1px 3px rgba(0,0,0,0.05);
54
+ }
55
+ .key-value-pair h3 {
56
+ font-size: 0.95em;
57
+ color: #444;
58
+ margin: 0 0 5px 0;
59
+ border-bottom: none;
60
+ padding-bottom: 0;
61
+ }
62
+ .key-value-pair p {
63
+ font-size: 0.9em;
64
+ color: #555;
65
+ margin: 0;
66
+ font-weight: bold;
67
+ }
68
+ .big-box {
69
+ width:95%;
70
+ margin:20px auto;
71
+ padding:20px;
72
+ border:1px solid #ccc;
73
+ border-radius:8px;
74
+ background:#fff;
75
+ box-shadow:0 2px 8px rgba(0,0,0,0.1);
76
+ font-size:0.95em;
77
+ line-height:1.4em;
78
+ max-height:400px;
79
+ overflow-y:auto;
80
+ }
81
+ </style>
82
+ """
83
+
84
+ yfsymbol = symbol + ".NS"
85
+ content_html = "<h1>No info available</h1>"
86
+
87
+ try:
88
+ ticker = yf.Ticker(yfsymbol)
89
+ info = ticker.info
90
+
91
+ if info:
92
+ long_summary = info.pop("longBusinessSummary", None)
93
+ officers = info.pop("companyOfficers", None)
94
+
95
+ info_categories = {
96
+ "Company Overview": [
97
+ "longName", "symbol", "exchange", "quoteType", "sector", "industry",
98
+ "fullTimeEmployees", "website", "address1", "city", "state", "zip", "country", "phone"
99
+ ],
100
+ "Valuation Metrics": [
101
+ "marketCap", "enterpriseValue", "trailingPE", "forwardPE", "pegRatio",
102
+ "priceToSalesTrailing12Months", "enterpriseToRevenue", "enterpriseToEbitda"
103
+ ],
104
+ "Key Financials": [
105
+ "fiftyTwoWeekHigh", "fiftyTwoWeekLow", "fiftyDayAverage", "twoHundredDayAverage",
106
+ "trailingAnnualDividendRate", "trailingAnnualDividendYield", "dividendRate", "dividendYield",
107
+ "exDividendDate", "lastSplitFactor", "lastSplitDate", "lastDividendValue", "payoutRatio",
108
+ "beta", "sharesOutstanding", "impliedSharesOutstanding"
109
+ ],
110
+ "Operational Details": [
111
+ "auditRisk", "boardRisk", "compensationRisk", "shareHolderRightsRisk", "overallRisk",
112
+ "governanceEpochDate", "compensationAsOfEpochDate"
113
+ ],
114
+ "Trading Information": [
115
+ "open", "previousClose", "dayLow", "dayHigh", "volume", "averageVolume", "averageVolume10days",
116
+ "fiftyTwoWeekChange", "SandP52WeekChange", "currency", "regularMarketDayLow",
117
+ "regularMarketDayHigh", "regularMarketOpen", "regularMarketPreviousClose",
118
+ "regularMarketPrice", "regularMarketVolume", "regularMarketChange", "regularMarketChangePercent"
119
+ ],
120
+ "Analyst & Target": [
121
+ "targetMeanPrice", "numberOfAnalystOpinions", "recommendationKey", "recommendationMean"
122
+ ]
123
+ }
124
+
125
+ categorized_html = ""
126
+ for category_name, keys in info_categories.items():
127
+ category_html = ""
128
+ for key in keys:
129
+ if key in info and info[key] not in [None, []]:
130
+ value = info[key]
131
+ if key in ["exDividendDate","lastSplitDate","governanceEpochDate","compensationAsOfEpochDate"]:
132
+ value = format_timestamp_to_date(value)
133
+ elif key in ["marketCap","enterpriseValue","fullTimeEmployees","volume","averageVolume","averageVolume10days","sharesOutstanding","impliedSharesOutstanding","regularMarketVolume"]:
134
+ value = format_large_number(value)
135
+ elif isinstance(value,(int,float)):
136
+ if 'percent' in key.lower() or 'ratio' in key.lower() or 'yield' in key.lower() or 'beta' in key.lower() or 'payoutratio' in key.lower():
137
+ value = f"{value:.2%}"
138
+ elif 'price' in key.lower() or 'dividend' in key.lower() or 'average' in key.lower():
139
+ value = f"{value:.2f}"
140
+ else:
141
+ value = f"{value:,.0f}"
142
+ category_html += f"<div class='key-value-pair'><h3>{key.replace('_',' ').title()}</h3><p>{value}</p></div>"
143
+
144
+ if category_html:
145
+ categorized_html += f"<h2 class='card-category-title'>{category_name}</h2><div class='card'><div class='card-content-grid'>{category_html}</div></div>"
146
+
147
+ extra_sections = ""
148
+ if long_summary:
149
+ extra_sections += f"<div class='big-box'><h2>Business Summary</h2><p>{long_summary}</p></div>"
150
+
151
+ if officers:
152
+ officer_rows = "".join(
153
+ f"<tr><td>{o.get('name','')}</td><td>{o.get('title','')}</td><td>{o.get('age','')}</td></tr>"
154
+ for o in officers
155
+ )
156
+ officer_table = f"<table class='styled-table'><tr><th>Name</th><th>Title</th><th>Age</th></tr>{officer_rows}</table>"
157
+ extra_sections += f"<div class='big-box'><h2>Company Officers</h2>{officer_table}</div>"
158
+
159
+ content_html = f"{categorized_html}{extra_sections}"
160
+
161
+ except Exception as e:
162
+ content_html = f"<h1>Error</h1><p>{e}</p>"
163
+
164
+ return f"<!DOCTYPE html><html><head>{STYLE_BLOCK}</head><body>{content_html}</body></html>"