-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathestimate-extract.rkt
98 lines (84 loc) · 3.08 KB
/
estimate-extract.rkt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#lang racket/base
(require db
gregor
net/http-easy
racket/cmdline
racket/file
racket/list
racket/port
tasks
threading)
(define (download-estimates symbol)
(make-directory* (string-append "/var/tmp/zacks/estimates/" (~t (today) "yyyy-MM-dd")))
(call-with-output-file (string-append "/var/tmp/zacks/estimates/" (~t (today) "yyyy-MM-dd") "/" symbol ".detailed-estimates.html")
(λ (out) (with-handlers ([exn:fail?
(λ (error)
(displayln (string-append "Encountered error for " symbol))
(displayln error))])
(~> (string-append "https://www.zacks.com/stock/quote/" symbol "/detailed-earning-estimates")
(get _)
(response-body _)
(write-bytes _ out))))
#:exists 'replace))
(define db-user (make-parameter "user"))
(define db-name (make-parameter "local"))
(define db-pass (make-parameter ""))
(define first-symbol (make-parameter ""))
(define last-symbol (make-parameter ""))
(command-line
#:program "racket estimate-extract.2023-02-27.rkt"
#:once-each
[("-f" "--first-symbol") first
"First symbol to query. Defaults to nothing"
(first-symbol first)]
[("-l" "--last-symbol") last
"Last symbol to query. Defaults to nothing"
(last-symbol last)]
[("-n" "--db-name") name
"Database name. Defaults to 'local'"
(db-name name)]
[("-p" "--db-pass") password
"Database password"
(db-pass password)]
[("-u" "--db-user") user
"Database user name. Defaults to 'user'"
(db-user user)])
(define dbc (postgresql-connect #:user (db-user) #:database (db-name) #:password (db-pass)))
(define symbols (query-list dbc "
select
act_symbol
from
nasdaq.symbol
where
is_etf = false and
is_test_issue = false and
is_next_shares = false and
security_name !~ 'ETN' and
nasdaq_symbol !~ '[-\\$\\+\\*#!@%\\^=~]' and
case when nasdaq_symbol ~ '[A-Z]{4}[L-Z]'
then security_name !~ '(Note|Preferred|Right|Unit|Warrant)'
else true
end and
last_seen = (select max(last_seen) from nasdaq.symbol) and
case when $1 != ''
then act_symbol >= $1
else true
end and
case when $2 != ''
then act_symbol <= $2
else true
end
order by
act_symbol;
"
(first-symbol)
(last-symbol)))
(disconnect dbc)
(define delay-interval 10)
(define delays (map (λ (x) (* delay-interval x)) (range 0 (length symbols))))
(with-task-server (for-each (λ (l) (schedule-delayed-task (λ () (thread (λ () (download-estimates (first l)))))
(second l)))
(map list symbols delays))
; add a final task that will halt the task server
(schedule-delayed-task (λ () (schedule-stop-task)) (* delay-interval (length delays)))
(run-tasks))