Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
hackinscience
hkis-website
Commits
01673090
Unverified
Commit
01673090
authored
Jun 01, 2021
by
Julien Palard
Browse files
fetch and push scripts to handle pages.
parent
c8b0de30
Changes
2
Hide whitespace changes
Inline
Side-by-side
scripts/fetch.py
View file @
01673090
...
...
@@ -4,6 +4,7 @@
import
argparse
from
getpass
import
getpass
from
pathlib
import
Path
from
functools
import
lru_cache
import
json
import
requests
...
...
@@ -17,6 +18,7 @@ def parse_args():
parser
.
add_argument
(
"--endpoint"
,
default
=
"https://www.hackinscience.org/api/exercises/"
)
parser
.
add_argument
(
"--page"
,
default
=
"exercises"
)
return
parser
.
parse_args
()
...
...
@@ -37,17 +39,28 @@ def main():
elif
not
args
.
password
:
args
.
password
=
getpass
()
next_exercise_page
=
args
.
endpoint
@
lru_cache
()
def
get
(
url
):
return
requests
.
get
(
url
,
auth
=
(
args
.
username
,
args
.
password
)).
json
()
while
next_exercise_page
:
exercises
=
requests
.
get
(
next_exercise_page
,
auth
=
(
args
.
username
,
args
.
password
)
).
json
()
exercises
=
get
(
next_exercise_page
)
if
"results"
not
in
exercises
:
print
(
exercises
)
exit
(
1
)
for
exercise
in
exercises
[
"results"
]:
path
=
Path
(
"exercises"
)
/
exercise
[
"slug"
]
if
exercise
[
"category"
]
is
not
None
:
category
=
get
(
exercise
[
"category"
])[
"slug"
]
else
:
category
=
"exercises"
page
=
get
(
exercise
[
"page"
])
if
page
[
"slug"
]
!=
args
.
page
:
continue
path
=
Path
(
category
)
/
exercise
[
"slug"
]
path
.
mkdir
(
exist_ok
=
True
,
parents
=
True
)
del
exercise
[
"wording"
]
# Only use _en and _fr.
print
(
"Downloading"
,
exercise
[
"title"
])
for
file
in
(
"check.py"
,
"solution.py"
,
...
...
@@ -56,7 +69,7 @@ def main():
"wording_fr.md"
,
"initial_solution.py"
,
):
(
path
/
(
file
)
)
.
write_text
(
(
path
/
file
).
write_text
(
fix_newline_at_end_of_file
(
exercise
[
file
.
split
(
"."
)[
0
]]).
replace
(
"
\r\n
"
,
"
\n
"
)
...
...
scripts/push.py
View file @
01673090
...
...
@@ -28,9 +28,8 @@ def main():
args
.
password
=
Path
(
args
.
password_file
).
read_text
().
rstrip
(
"
\n
"
)
elif
not
args
.
password
:
args
.
password
=
getpass
()
for
exercise
in
Path
(
"exercises"
).
glob
(
"*/"
):
with
open
(
exercise
/
"meta"
)
as
f
:
meta
=
json
.
load
(
f
)
for
exercise
in
Path
(
"."
).
glob
(
"*/*/meta"
):
meta
=
json
.
loads
(
exercise
.
read_text
())
if
args
.
only
:
if
args
.
only
not
in
meta
[
"slug"
]:
continue
...
...
@@ -42,7 +41,7 @@ def main():
"wording_en.md"
,
"initial_solution.py"
,
):
meta
[
file
.
split
(
"."
)[
0
]]
=
(
exercise
/
(
file
)
)
.
read_text
()
meta
[
file
.
split
(
"."
)[
0
]]
=
(
exercise
.
parent
/
file
).
read_text
()
print
(
"Uploading "
,
meta
[
"title"
])
response
=
requests
.
put
(
meta
[
"url"
],
json
=
meta
,
auth
=
(
args
.
username
,
args
.
password
)
...
...
Write
Preview
Supports
Markdown
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!
Cancel
Please
register
or
sign in
to comment