7
7
import finviz .scraper_functions as scrape
8
8
9
9
# TODO > Add unittests
10
- # TODO > Make self.data list of dicts, not lists of lists of dicts
11
- # TODO > Implement __add__, __slice__, __iter__, __getitem__
10
+ # TODO > Implement __add__ function
12
11
13
12
14
13
class Screener (object ):
@@ -30,7 +29,7 @@ def __init__(self, tickers=None, filters=None, rows=None, order='', signal='', t
30
29
:type signal: str
31
30
:param table: table type eg.: 'Performance'
32
31
:type table: str
33
- :var self.data: pages containing data about each row inside a dictionary
32
+ :var self.data: list of dictionaries containing row data
34
33
:type self.data: list
35
34
"""
36
35
@@ -79,7 +78,7 @@ def __init__(self, tickers=None, filters=None, rows=None, order='', signal='', t
79
78
else :
80
79
self ._rows = rows
81
80
82
- self ._headers = self .__get_table_headers ()
81
+ self .headers = self .__get_table_headers ()
83
82
self .data = self .__search_screener ()
84
83
85
84
def __repr__ (self ):
@@ -101,11 +100,10 @@ def __str__(self):
101
100
""" Returns a string containing readable representation of a table. """
102
101
103
102
table_string = ''
104
- table_list = [self ._headers ]
103
+ table_list = [self .headers ]
105
104
106
- for page in self .data :
107
- for row in page :
108
- table_list .append ([str (row [col ] or '' ) for col in self ._headers ])
105
+ for row in self .data :
106
+ table_list .append ([row [col ] or '' for col in self .headers ])
109
107
110
108
col_size = [max (map (len , col )) for col in zip (* table_list )]
111
109
format_str = ' | ' .join (["{{:<{}}}" .format (i ) for i in col_size ])
@@ -122,21 +120,23 @@ def __len__(self):
122
120
return int (self ._rows )
123
121
124
122
def __getitem__ (self , position ):
125
- return self .data
123
+ """ Returns a dictionary containting specific row data. """
124
+
125
+ return self .data [position ]
126
126
127
127
def to_sqlite (self ):
128
128
""" Exports the generated table into a SQLite database, located in the user's current directory. """
129
129
130
- export_to_db (self ._headers , self .data )
130
+ export_to_db (self .headers , self .data )
131
131
132
132
def to_csv (self ):
133
133
""" Exports the generated table into a CSV file, located in the user's current directory. """
134
134
135
- export_to_csv (self ._headers , self .data )
135
+ export_to_csv (self .headers , self .data )
136
136
137
137
def get_charts (self , period = 'd' , size = 'l' , chart_type = 'c' , ta = '1' ):
138
138
"""
139
- Downloads the charts of tickers shown by the table.
139
+ Downloads the charts of all tickers shown by the table.
140
140
141
141
:param period: table period eg. : 'd', 'w' or 'm' for daily, weekly and monthly periods
142
142
:type period: str
@@ -158,15 +158,14 @@ def get_charts(self, period='d', size='l', chart_type='c', ta='1'):
158
158
base_url = 'https://finviz.com/chart.ashx?' + urlencode (payload )
159
159
chart_urls = []
160
160
161
- for page in self .data :
162
- for row in page :
163
- chart_urls .append (base_url + '&t={}' .format (row .get ('Ticker' )))
161
+ for row in self .data :
162
+ chart_urls .append (base_url + '&t={}' .format (row .get ('Ticker' )))
164
163
165
164
async_connector = Connector (scrape .download_chart_image , chart_urls )
166
165
async_connector .run_connector ()
167
166
168
167
def __get_table_headers (self ):
169
- """ Private function used to return the table headers. """
168
+ """ Private function used to return table headers. """
170
169
171
170
first_row = self ._page_content .cssselect ('tr[valign="middle"]' )
172
171
@@ -182,7 +181,7 @@ def __get_table_headers(self):
182
181
return headers
183
182
184
183
def __get_table_data (self , page = None , url = None ):
185
- """ Private function used to return the table data from a single page. """
184
+ """ Private function used to return table data from a single page. """
186
185
187
186
def scrape_row (line ):
188
187
@@ -203,21 +202,26 @@ def scrape_row(line):
203
202
for row in all_rows :
204
203
205
204
if int (row [0 ].text ) == self ._rows :
206
- values = dict (zip (self ._headers , scrape_row (row )))
205
+ values = dict (zip (self .headers , scrape_row (row )))
207
206
data_sets .append (values )
208
207
break
209
208
210
209
else :
211
- values = dict (zip (self ._headers , scrape_row (row )))
210
+ values = dict (zip (self .headers , scrape_row (row )))
212
211
data_sets .append (values )
213
212
214
213
return data_sets
215
214
216
215
def __search_screener (self ):
217
- """ Private function used to return the data from the FinViz screener. """
216
+ """ Private function used to return data from the FinViz screener. """
218
217
219
218
page_urls = scrape .get_page_urls (self ._page_content , self ._rows , self ._url )
220
219
async_connector = Connector (self .__get_table_data , page_urls )
221
- data = async_connector .run_connector ()
220
+ pages_data = async_connector .run_connector ()
221
+
222
+ data = []
223
+ for page in pages_data :
224
+ for row in page :
225
+ data .append (row )
222
226
223
227
return data
0 commit comments