Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
Maják
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
TO
Maják
Commits
30a40988
Commit
30a40988
authored
3 years ago
by
OndraRehounek
Committed by
jan.bednarik
3 years ago
Browse files
Options
Downloads
Patches
Plain Diff
fixed Prague imports
parent
222761b1
No related branches found
No related tags found
2 merge requests
!442
Release
,
!432
Feature/majak imports
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
district/jekyll_import.py
+59
-16
59 additions, 16 deletions
district/jekyll_import.py
with
59 additions
and
16 deletions
district/jekyll_import.py
+
59
−
16
View file @
30a40988
...
...
@@ -121,9 +121,18 @@ def get_or_create_image(
try
:
urllib
.
request
.
urlretrieve
(
img_url
,
img_path
)
except
(
HTTPError
,
UnicodeEncodeError
,
InvalidURL
,
IsADirectoryError
):
# FIXME praha má např. https://a.pirati.cz/praha/img/posts/srovnani.png
# ale dává to 'https://a.pirati.cz//img/assets/img/posts/srovnani.png'
try
:
# druhý pokus s "posts" přidáno do URL (obvykle je ve file_path)
img_url
=
"
https://a.pirati.cz/{}/img/posts/{}
"
.
format
(
img_assets_folder
,
img_name
.
split
()[
0
]
)
urllib
.
request
.
urlretrieve
(
img_url
,
img_path
)
except
(
HTTPError
,
UnicodeEncodeError
,
InvalidURL
,
IsADirectoryError
,
):
logger
.
warning
(
"
Nedohledán obrázek při importu článků
"
,
extra
=
{
...
...
@@ -277,7 +286,7 @@ class JekyllArticleImporter:
self
.
message_list
.
append
(
{
"
level
"
:
INFO
,
"
text
"
:
"
{} článků s tímto názve
v
již existuje
"
.
format
(
"
text
"
:
"
{} článků s tímto názve
m
již existuje
"
.
format
(
self
.
exists_counter
),
}
...
...
@@ -310,6 +319,39 @@ class JekyllArticleImporter:
text
=
re
.
split
(
r
"
^\s*$
"
,
text
.
strip
(),
flags
=
re
.
MULTILINE
)[
0
]
return
plain_md
.
convert
(
text
)
@staticmethod
def
handle_meta_is_str
(
meta
:
str
)
->
dict
:
"""
Snaží se vyřešit situaci, že meta se nenaparsovala na dict, ale na string,
kde je sice klíč:hodnota, ale další klíč následuje za mezerou po předchozí
hodnotě.
Iteruju teda přes rozesekaný string přes dvojtečky, ale každá value kromě
poslední (viz if idx == len(string_parts) - 2) má za poslední mezerou klíč
pro další hodnotu. Takže položku z další iterace splitnu přes mezery,
spojím všechny kromě poslední do value a tu přiřadím klíči z aktuální iterace.
Poslední položka iterace už je samotná value, takže tam handluju jinak.
Protože sahám na +1, tak hlídám číslo iterace pro přeskočení poslední (kde
už je položka iterace pouze value bez key, takže jí beru
v před-předposlední...).
"""
logger
.
info
(
"
Meta se neparsuje na dict, ale na str - zkouším složitější parse
"
,
extra
=
{
"
article_meta
"
:
meta
},
)
string_parts
=
meta
.
split
(
"
:
"
)
meta_dict
=
{}
for
idx
,
part
in
enumerate
(
string_parts
):
if
idx
==
len
(
string_parts
)
-
1
:
break
key
=
part
.
split
()[
-
1
]
if
idx
==
len
(
string_parts
)
-
2
:
value
=
string_parts
[
idx
+
1
]
else
:
value
=
"
"
.
join
(
string_parts
[
idx
+
1
].
split
()[
0
:
-
1
])
meta_dict
.
update
({
key
:
value
})
return
meta_dict
def
import_post
(
self
,
file_path
):
from
district.models
import
DistrictArticlePage
...
...
@@ -328,13 +370,14 @@ class JekyllArticleImporter:
md
=
r
[
2
]
html
=
html_md
.
convert
(
md
)
# meta_date = meta["date"]
# article_date = meta_date if isinstance(meta_date, date) else meta["date"].split()[0]
if
isinstance
(
meta
,
str
):
# pokud se špatně naparsovalo meta (není dict)
meta
=
self
.
handle_meta_is_str
(
meta
)
try
:
title
=
meta
[
"
title
"
]
except
TypeError
:
logger
.
warning
(
"
Nelze importovat článek -
YAML se neparsuje na dict
"
,
"
Nelze importovat článek -
nepodařilo se získat title
"
,
extra
=
{
"
article_meta
"
:
meta
},
)
self
.
skipped_counter
+=
1
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
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