Skip to content

Commit a25ba76

Browse files
Merge pull request #12 from tylerjthomas9/dev
Improve progress bars, rename get_quarterly_filings to download_filings
2 parents 094a474 + 81868e9 commit a25ba76

7 files changed

Lines changed: 107 additions & 53 deletions

File tree

Project.toml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
name = "ScrapeSEC"
22
uuid = "856806e7-be2f-4540-8165-3a51303b7af0"
33
authors = ["tylerjthomas9 <tylerjthomas9@gmail.com>"]
4-
version = "0.6.2"
4+
version = "0.7.0"
55

66
[deps]
77
CSV = "336ed68f-0bac-5ca0-87d4-7b16caf5d00b"
88
DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0"
99
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
1010
HTTP = "cd3eb016-35fb-5094-929b-558a96fad6f3"
11-
ProgressMeter = "92933f4c-e287-5a05-a399-4b506db050ca"
11+
Term = "22787eb5-b846-44ae-b979-8e399b8463ab"
1212
ZipFile = "a5390f91-8eb1-5f08-bee0-b1d1ffed6cea"
1313

1414
[compat]
1515
CSV = "0.9, 0.10"
1616
DataFrames = "1"
1717
HTTP = "1"
18-
ProgressMeter = "1"
19-
ZipFile = "0.9"
18+
Term = "1"
19+
ZipFile = "0.9, 0.10"
2020
julia = "1"
2121

2222
[extras]

README.md

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,44 +10,42 @@
1010

1111
## Installation
1212

13+
From the Julia REPL:
1314
```julia
14-
julia> using Pkg; Pkg.add("ScrapeSEC")
15+
julia> ] # enters the pkg interface
16+
pkg> add ScrapeSEC
1517
```
1618

1719
```julia
18-
]add ScrapeSEC
20+
julia> using Pkg; Pkg.add(ScrapeSEC)
1921
```
2022

2123
From source:
2224
```julia
23-
julia> using Pkg; Pkg.add(url="https://github.com/tylerjthomas9/ScrapeSEC.jl/")
25+
julia> ] # enters the pkg interface
26+
pkg> add https://github.com/tylerjthomas9/ScrapeSEC.jl
2427
```
25-
26-
```julia
27-
]add https://github.com/tylerjthomas9/ScrapeSEC.jl/
28-
```
29-
3028
# Examples
3129

32-
Download filing metadata for 2012-2020 from the [SEC archives](https://www.sec.gov/Archives/).
30+
Download filing metadata for 2020-2022 from the [SEC archives](https://www.sec.gov/Archives/).
3331

3432
```julia
35-
using ScrapeSEC: get_metadata
36-
download_metadata_files(2012, 2020)
33+
using ScrapeSEC
34+
download_metadata_files(2020, 2022)
3735
```
3836

39-
Download 10-K, 8-K, and 10-Q metadata, filings for 2012-2020
37+
Download 10-K, 8-K, and 10-Q metadata, filings for 2020-2022
4038

4139
```julia
42-
using ScrapeSEC: get_quarterly_filings
43-
get_quarterly_filings(2012, 2020; filing_types=["10-K", "8-K", "10-Q"])
40+
using ScrapeSEC
41+
download_filings(2020, 2022; filing_types=["10-K", "8-K", "10-Q"])
4442
```
4543

4644

47-
Download filing metadata for 2012-2020, create a main index file, and download 10-Ks using the master index
45+
Download filing metadata for 2020-2022, create a main index file, and download 10-Ks using the combined index file
4846
```julia
4947
using ScrapeSEC
50-
download_metadata_files(2012, 2020)
48+
download_metadata_files(2020, 2022)
5149
create_main_index()
52-
get_quarterly_filings("./metadata/main_idx.tsv"; filing_types=["10-K", ])
50+
download_filings("./metadata/main_idx.tsv"; filing_types=["10-K", ])
5351
```

docs/src/index.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
# Installation
44
From the Julia REPL:
55
```julia
6-
julia> ]add ScrapeSEC
6+
julia> ] # enters the pkg interface
7+
pkg> add ScrapeSEC
78
```
89

910
```julia
@@ -12,7 +13,8 @@ julia> using Pkg; Pkg.add(ScrapeSEC)
1213

1314
From source:
1415
```julia
15-
julia> ]add https://github.com/tylerjthomas9/ScrapeSEC.jl
16+
julia> ] # enters the pkg interface
17+
pkg> add https://github.com/tylerjthomas9/ScrapeSEC.jl
1618
```
1719

1820
```julia
@@ -32,6 +34,6 @@ ScrapeSEC.create_main_index
3234
# Download Filings
3335
```@docs
3436
download_filing
35-
get_quarterly_filings
37+
download_filings
3638
```
3739

src/ScrapeSEC.jl

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,18 @@ using DataFrames
66
import Dates
77
import CSV
88
import HTTP
9-
using ProgressMeter
9+
using Term.Progress
1010
import ZipFile
1111

12+
const progress_bar_columns = [
13+
Progress.DescriptionColumn,
14+
Progress.SeparatorColumn,
15+
Progress.ProgressColumn,
16+
Progress.CompletedColumn,
17+
Progress.SeparatorColumn,
18+
Progress.ETAColumn,
19+
]
20+
1221
# source files
1322
include("download_metadata.jl")
1423
include("main_index.jl")
@@ -20,7 +29,7 @@ export
2029
create_main_index,
2130

2231
# filing downloaders
23-
get_quarterly_filings,
32+
download_filings,
2433
download_filing
2534

2635
end

src/download_filings.jl

Lines changed: 53 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -34,30 +34,42 @@ end
3434

3535
"""
3636
```julia
37-
function get_quarterly_filings(
37+
function download_filings(
3838
metadata_file::String;
3939
dest="./data/"::String,
4040
filing_types=["10-K", ]::Vector{String},
4141
download_rate=10::Int,
42-
skip_file=true::Bool
42+
skip_file=true::Bool,
43+
pbar=ProgressBar()::pbar,
44+
stop_pbar=true::Bool,
45+
pbar_desc="Downloading Filings"::string,
46+
running_tests=false::Bool
4347
)
4448
```
4549
46-
Get quarterly filings from https://www.sec.gov/Archives/ using a metadata file
50+
Download quarterly filings from https://www.sec.gov/Archives/ using a metadata file
4751
4852
Parameters
4953
* `metadata_file`: CSV file with filing metadata
5054
* `dest`: Destination folder for downloaded filings
5155
* `filing_types`: Types of filings to download (eg. ["10-K", "10-Q"])
5256
* `download_rate`: Number of filings to download every second (limit=10)
5357
* `skip_file`: If true, existing files will be skipped
58+
* `pbar`: ProgressBar (Term.jl)
59+
* `stop_pbar`: If false, progress bar will not be stopped
60+
* `pbar_desc`: pbar Description
61+
* `runnings_tests`: If true, only downloads one file
5462
"""
55-
function get_quarterly_filings(
63+
function download_filings(
5664
metadata_file::String;
5765
dest = "./data/"::String,
5866
filing_types = ["10-K"]::Vector{String},
5967
download_rate = 10::Int,
6068
skip_file = true::Bool,
69+
pbar = ProgressBar()::pbar,
70+
stop_pbar = true::Bool,
71+
pbar_desc = "Downloading Filings"::string,
72+
running_tests = false::Bool,
6173
)
6274

6375
# verify download_rate is valid (less than 10 requests per second, more than 0)
@@ -70,8 +82,6 @@ function get_quarterly_filings(
7082
)
7183
end
7284

73-
println("Metadata: " * metadata_file)
74-
7585
df = CSV.File(metadata_file, delim = "|") |> DataFrame
7686
df = df[(filing_types).(df[!, "Form Type"]), :]
7787

@@ -82,8 +92,12 @@ function get_quarterly_filings(
8292

8393
# download filings at 10 requests per second
8494
sleep_time = 1 / download_rate
85-
@showprogress 1 "Downloading Filings..." for row in eachrow(df)
8695

96+
# setup progress bar
97+
job = addjob!(pbar; N = size(df, 1), description = pbar_desc)
98+
start!(pbar)
99+
for row in eachrow(df)
100+
update!(job)
87101
# check if filing already has been downloaded
88102
full_file = joinpath(dest, replace(row["Filename"], "edgar/data/" => ""))
89103
if isfile(full_file) && skip_file
@@ -95,15 +109,22 @@ function get_quarterly_filings(
95109

96110
# rest to throttle api hits to around 10/second
97111
sleep(sleep_time)
112+
render(pbar)
113+
114+
if running_tests
115+
break
116+
end
117+
end
118+
if stop_pbar
119+
stop!(pbar)
98120
end
99121

100122
return
101123
end
102124

103-
104125
"""
105126
```julia
106-
function get_quarterly_filings(
127+
function download_filings(
107128
start_year::Int,
108129
end_year::Int;
109130
quarters=[1,2,3,4]::Vector{Int},
@@ -112,11 +133,12 @@ function get_quarterly_filings(
112133
download_rate=10::Int,
113134
metadata_dest="./metadata/"::String,
114135
skip_file=true::Bool,
115-
skip_metadata_file=true::Bool
136+
skip_metadata_file=true::Bool,
137+
running_tests=false::Bool
116138
)
117139
```
118140
119-
Get quarterly filings from https://www.sec.gov/Archives/
141+
Download quarterly filings from https://www.sec.gov/Archives/
120142
121143
Parameters
122144
* `start_year`: First year to download filings
@@ -128,8 +150,9 @@ Parameters
128150
* `metadata_dest`: Directory to store metadata files
129151
* `skip_file`: If true, existing files will be skipped
130152
* `skip_metadata_file`: If true, existing metadata files will be skipped
153+
* `runnings_tests`: If true, only downloads one file
131154
"""
132-
function get_quarterly_filings(
155+
function download_filings(
133156
start_year::Int,
134157
end_year::Int;
135158
quarters = [1, 2, 3, 4]::Vector{Int},
@@ -139,6 +162,7 @@ function get_quarterly_filings(
139162
metadata_dest = "./metadata/"::String,
140163
skip_file = true::Bool,
141164
skip_metadata_file = true::Bool,
165+
running_tests = false::Bool,
142166
)
143167

144168
# get current year, quarter to prevent errors trying to get future data
@@ -163,26 +187,36 @@ function get_quarterly_filings(
163187
start_year,
164188
end_year;
165189
quarters = quarters,
166-
#download_rate = download_rate,
167190
dest = metadata_dest,
168191
skip_file = skip_metadata_file,
169192
)
170193

171194

172195
# download all quarterly filings
173-
file_paths = [
174-
joinpath(metadata_dest, string(t[1]) * "-QTR" * string(t[2]) * ".tsv") for
175-
t in time_periods
176-
]
177-
for file in file_paths
178-
get_quarterly_filings(
196+
pbar = ProgressBar(columns = progress_bar_columns)
197+
job = addjob!(
198+
pbar;
199+
N = size(time_periods, 1),
200+
description = "Iterating Over Time Periods...",
201+
)
202+
start!(pbar)
203+
for t in time_periods
204+
update!(job)
205+
file = joinpath(metadata_dest, string(t[1]) * "-QTR" * string(t[2]) * ".tsv")
206+
download_filings(
179207
file;
180208
dest = dest,
181209
filing_types = filing_types,
182210
download_rate = download_rate,
183211
skip_file = skip_file,
212+
pbar = pbar,
213+
stop_pbar = false,
214+
pbar_desc = "Downloading $(t[1]) Q$(t[2]) Filings",
215+
running_tests = running_tests,
184216
)
217+
render(pbar)
185218
end
219+
stop!(pbar)
186220

187221
return
188222
end

src/download_metadata.jl

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ function download_metadata(
6969
end
7070

7171
# download, unzip file
72-
HTTP.download(url, temp_zip)
72+
HTTP.download(url, temp_zip, update_period = Inf)
7373
zarchive = ZipFile.Reader(temp_zip)
7474
for f in zarchive.files
7575
@assert f.name == "master.idx"
@@ -151,9 +151,21 @@ function download_metadata_files(
151151
urls = get_metadata_urls(time_periods)
152152

153153
# download metadata files
154-
@showprogress 1 "Downloading Metadata..." for idx in eachindex(urls)
155-
download_metadata(urls[idx]; dest = dest, skip_file = skip_file, verbose = verbose)
154+
n_files = size(urls, 1)
155+
pbar = ProgressBar(columns = progress_bar_columns)
156+
job = addjob!(pbar; N = n_files, description = "Downloading Metadata CSVs...")
157+
start!(pbar)
158+
@inbounds for idx in eachindex(urls)
159+
update!(job)
160+
ScrapeSEC.download_metadata(
161+
urls[idx];
162+
dest = dest,
163+
skip_file = skip_file,
164+
verbose = verbose,
165+
)
166+
render(pbar)
156167
end
168+
stop!(pbar)
157169

158170
return
159171
end

test/download_filings.jl

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,18 @@
1010
end
1111

1212

13-
@testset "get_quarterly_filings()" begin
13+
@testset "download_filings()" begin
1414

15-
get_quarterly_filings(
15+
download_filings(
1616
1994,
1717
1994;
1818
quarters = [4],
1919
dest = "./temp/",
2020
metadata_dest = "./metadata/",
21+
running_tests = true,
2122
)
2223
@test isfile("./metadata/1994-QTR4.tsv")
23-
@test isfile("./temp/3146/0000950144-94-002172.txt")
2424
rm("./metadata/1994-QTR4.tsv")
25-
# TODO: Is it safe to clear the temp dir? I dont want to accidently delete peoples files who
26-
# run the tests
25+
# TODO: Is it safe to clear the temp dir? I dont want to accidently user files
2726

2827
end

0 commit comments

Comments
 (0)