Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
F
fao-fetcher
Manage
Activity
Members
Labels
Plan
Issues
1
Issue boards
Milestones
Wiki
Code
Merge requests
0
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
dbnomics-fetchers
fao-fetcher
Commits
919166a8
Commit
919166a8
authored
3 years ago
by
Pierre Dittgen
Browse files
Options
Downloads
Patches
Plain Diff
Formatting using black
parent
62d39a24
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
2
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
convert.py
+165
-114
165 additions, 114 deletions
convert.py
download.py
+24
-16
24 additions, 16 deletions
download.py
with
189 additions
and
130 deletions
convert.py
+
165
−
114
View file @
919166a8
This diff is collapsed.
Click to expand it.
download.py
+
24
−
16
View file @
919166a8
...
...
@@ -35,7 +35,7 @@ from pathlib import Path
# Download main FAO file
def
download_file
(
path
:
Path
,
url
):
"""
Download, unzip and save FAO file to target_dir
"""
"""
Download, unzip and save FAO file to target_dir
"""
print
(
"
Downloading database from
"
,
url
)
resp
=
requests
.
get
(
url
)
with
ZipFile
(
BytesIO
(
resp
.
content
))
as
zfile
:
...
...
@@ -56,20 +56,20 @@ def download_file(path: Path, url):
def
download_dataset_json
(
path
:
Path
,
url
):
"""
Download dataset.json file
"""
"""
Download dataset.json file
"""
elements
=
url
.
split
(
"
/
"
)
r
=
requests
.
get
(
url
)
print
(
"
Downloading
"
+
elements
[
-
1
]
+
"
data
"
)
with
open
(
str
(
path
)
+
"
/
"
+
"
datasets.json
"
,
'
wb
'
)
as
f
:
with
open
(
str
(
path
)
+
"
/
"
+
"
datasets.json
"
,
"
wb
"
)
as
f
:
f
.
write
(
r
.
content
)
def
download_flag_json
(
path
:
Path
,
url
):
"""
Download flag.json file
"""
"""
Download flag.json file
"""
elements
=
url
.
split
(
"
/
"
)
r
=
requests
.
get
(
url
)
print
(
"
Downloading
"
+
elements
[
-
1
]
+
"
data
"
)
with
open
(
str
(
path
)
+
"
/
"
+
"
flags.json
"
,
'
wb
'
)
as
f
:
with
open
(
str
(
path
)
+
"
/
"
+
"
flags.json
"
,
"
wb
"
)
as
f
:
f
.
write
(
r
.
content
)
...
...
@@ -77,21 +77,27 @@ log = logging.getLogger(__name__)
def
main
():
parser
=
argparse
.
ArgumentParser
(
description
=
__doc__
,
formatter_class
=
argparse
.
RawDescriptionHelpFormatter
)
parser
.
add_argument
(
'
target_dir
'
,
type
=
Path
,
help
=
'
path of target directory
'
)
parser
.
add_argument
(
'
--debug-http
'
,
action
=
'
store_true
'
,
help
=
'
display http.client debug messages
'
)
parser
.
add_argument
(
'
--log
'
,
default
=
'
WARNING
'
,
help
=
'
level of logging messages
'
)
parser
=
argparse
.
ArgumentParser
(
description
=
__doc__
,
formatter_class
=
argparse
.
RawDescriptionHelpFormatter
)
parser
.
add_argument
(
"
target_dir
"
,
type
=
Path
,
help
=
"
path of target directory
"
)
parser
.
add_argument
(
"
--debug-http
"
,
action
=
"
store_true
"
,
help
=
"
display http.client debug messages
"
)
parser
.
add_argument
(
"
--log
"
,
default
=
"
WARNING
"
,
help
=
"
level of logging messages
"
)
args
=
parser
.
parse_args
()
numeric_level
=
getattr
(
logging
,
args
.
log
.
upper
(),
None
)
if
not
isinstance
(
numeric_level
,
int
):
raise
ValueError
(
'
Invalid log level: {}
'
.
format
(
args
.
log
))
raise
ValueError
(
"
Invalid log level: {}
"
.
format
(
args
.
log
))
logging
.
basicConfig
(
format
=
"
%(levelname)s:%(name)s:%(asctime)s:%(message)s
"
,
level
=
numeric_level
,
stream
=
sys
.
stdout
,
# Use stderr if script outputs data to stdout.
)
logging
.
getLogger
(
"
urllib3
"
).
setLevel
(
logging
.
DEBUG
if
args
.
debug_http
else
logging
.
WARNING
)
logging
.
getLogger
(
"
urllib3
"
).
setLevel
(
logging
.
DEBUG
if
args
.
debug_http
else
logging
.
WARNING
)
if
args
.
debug_http
:
http
.
client
.
HTTPConnection
.
debuglevel
=
1
...
...
@@ -101,7 +107,9 @@ def main():
url
=
"
http://fenixservices.fao.org/faostat/static/bulkdownloads/FAOSTAT.zip
"
dataset_url
=
"
http://fenixservices.fao.org/faostat/static/bulkdownloads/datasets_E.json
"
dataset_url
=
(
"
http://fenixservices.fao.org/faostat/static/bulkdownloads/datasets_E.json
"
)
flag_url
=
"
http://fenixservices.fao.org/faostat/api/v1/en/definitions/types/flag
"
...
...
@@ -119,7 +127,7 @@ def main():
zip_dict
=
dict
()
for
name
in
zip_list
:
if
name
not
in
zip_dict
.
keys
():
zip_dict
[
name
]
=
''
zip_dict
[
name
]
=
""
csv_list
=
[]
for
file
in
os
.
listdir
(
target_dir
):
if
"
.csv
"
in
file
:
...
...
@@ -129,10 +137,10 @@ def main():
if
zip
.
strip
(
"
_All_Data_(Normalized).zip
"
)
in
csv
:
if
"
Flags
"
not
in
csv
:
zip_dict
[
zip
]
=
csv
# Write zip_list into .txt file and save it to target_dir
with
open
(
str
(
target_dir
)
+
"
/zip_dict.txt
"
,
'
w
'
)
as
fd
:
# Write zip_list into .txt file and save it to target_dir
with
open
(
str
(
target_dir
)
+
"
/zip_dict.txt
"
,
"
w
"
)
as
fd
:
json
.
dump
(
zip_dict
,
fd
)
if
__name__
==
'
__main__
'
:
if
__name__
==
"
__main__
"
:
sys
.
exit
(
main
())
This diff is collapsed.
Click to expand it.
Preview
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment