Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
Parser
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
Package registry
Harbor Registry
Model registry
Operate
Environments
Terraform modules
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
CMS
Parser
Commits
ebdc667e
Commit
ebdc667e
authored
8 months ago
by
Guilherme Eduardo
Browse files
Options
Downloads
Patches
Plain Diff
issue
#5
: ADD log in parser.py
parent
7ddffa92
Branches
Branches containing commit
No related tags found
1 merge request
!5
issue #5: ADD log in parser.py
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
app/parser.py
+68
-23
68 additions, 23 deletions
app/parser.py
with
68 additions
and
23 deletions
app/parser.py
+
68
−
23
View file @
ebdc667e
from
flask
import
Flask
,
request
from
flask
import
Flask
,
request
import
os
import
os
import
logging
import
sys
app
=
Flask
(
__name__
)
app
=
Flask
(
__name__
)
# Diretórios onde os arquivos Markdown serão salvos
#Configuração do log
logging
.
basicConfig
(
stream
=
sys
.
stdout
,
filemode
=
'
a
'
,
level
=
logging
.
INFO
,
format
=
'
%(asctime)s - %(levelname)s - %(message)s
'
)
# Diretórios onde os arquivos Markdown serão salvos dentro da pasta do hugo
DIR_O_C3SL
=
os
.
path
.
join
(
os
.
getcwd
(),
'
../cms-c3sl/themes/c3sl/exampleSite/content
'
)
DIR_O_C3SL
=
os
.
path
.
join
(
os
.
getcwd
(),
'
../cms-c3sl/themes/c3sl/exampleSite/content
'
)
DIR_POSTS
=
os
.
path
.
join
(
os
.
getcwd
(),
'
../cms-c3sl/themes/c3sl/exampleSite/content/noticia
'
)
DIR_POSTS
=
os
.
path
.
join
(
os
.
getcwd
(),
'
../cms-c3sl/themes/c3sl/exampleSite/content/noticia
'
)
DIR_PROJECTS
=
os
.
path
.
join
(
os
.
getcwd
(),
'
../cms-c3sl/themes/c3sl/exampleSite/content/projetos
'
)
DIR_PROJECTS
=
os
.
path
.
join
(
os
.
getcwd
(),
'
../cms-c3sl/themes/c3sl/exampleSite/content/projetos
'
)
def
create_or_update_post
(
post_id
,
data
,
event
,
content_type
):
def
create_or_update_post
(
post_id
,
data
,
event
,
content_type
):
if
not
data
:
if
not
data
:
return
logging
.
error
(
f
"
Code: 400
"
f
"
Status: JSON file not found
\n
"
)
logging
.
exception
(
"
error occurred while processing the event
"
)
return
"
JSON not found
"
,
400
#Caso a requisição seja para criar/alterar o conteudo do "Quem Somos"
if
content_type
==
'
o-c3sl
'
:
if
content_type
==
'
o-c3sl
'
:
attributes
=
data
.
get
(
'
entry
'
,
{})
attributes
=
data
.
get
(
'
entry
'
,
{})
title
=
attributes
.
get
(
'
Titulo
'
,
''
)
title
=
attributes
.
get
(
'
Titulo
'
,
''
)
...
@@ -27,7 +40,9 @@ layout: 'legal'
...
@@ -27,7 +40,9 @@ layout: 'legal'
---
---
{
content
}
{
content
}
"""
"""
#Caso a requisição seja para criar/alterar um projeto
elif
content_type
==
'
projeto
'
:
elif
content_type
==
'
projeto
'
:
#Coleta os dados necessários para estar no arquivo JSON
attributes
=
data
.
get
(
'
entry
'
,
{})
attributes
=
data
.
get
(
'
entry
'
,
{})
title
=
attributes
.
get
(
'
Titulo
'
,
''
)
title
=
attributes
.
get
(
'
Titulo
'
,
''
)
status
=
attributes
.
get
(
'
Status
'
,
''
)
status
=
attributes
.
get
(
'
Status
'
,
''
)
...
@@ -39,18 +54,19 @@ layout: 'legal'
...
@@ -39,18 +54,19 @@ layout: 'legal'
edited_line
=
'
Edited: true
\n
'
if
event
==
'
entry.update
'
else
''
edited_line
=
'
Edited: true
\n
'
if
event
==
'
entry.update
'
else
''
content_markdown
=
f
"""
---
content_markdown
=
f
"""
---
title:
"
{
title
}
"
title:
"
{
title
}
"
author:
"
Parceiro:
{
partner
}
"
author:
"
Parceiro:
{
partner
}
"
status:
"
{
status
}
"
status:
"
{
status
}
"
{
edited_line
}
ShowReadingTime: true
{
edited_line
}
ShowReadingTime: true
---
---
---
---
{
content
}
{
content
}
"""
"""
##Caso a requisição seja para criar/alterar uma notícia
else
:
elif
content_type
==
'
post
'
:
#Coleta os dados necessários para estar no arquivo JSON
attributes
=
data
.
get
(
'
entry
'
,
{})
attributes
=
data
.
get
(
'
entry
'
,
{})
content
=
attributes
.
get
(
'
Descricao
'
,
''
)
content
=
attributes
.
get
(
'
Descricao
'
,
''
)
published_at
=
attributes
.
get
(
'
Data
'
,
''
)
published_at
=
attributes
.
get
(
'
Data
'
,
''
)
...
@@ -82,58 +98,87 @@ summary: "{summary}"
...
@@ -82,58 +98,87 @@ summary: "{summary}"
{
content
}
{
content
}
"""
"""
#Registra o conteúdo no arquivo conforme diretório estabelecido
try
:
try
:
with
open
(
file_path
,
'
w
'
,
encoding
=
'
utf8
'
)
as
file
:
with
open
(
file_path
,
'
w
'
,
encoding
=
'
utf8
'
)
as
file
:
file
.
write
(
content_markdown
)
file
.
write
(
content_markdown
)
#print(f"Conteúdo do Markdown criado/atualizado para o post ID {post_id}:\n{content_markdown}\n
")
logging
.
info
(
f
"
Content
'
{
title
}
'
\n
Saved in:
{
file_path
}
"
)
#print(f"Post '{title}' salvo como {file_path}")
except
Exception
as
e
:
except
Exception
as
e
:
print
(
f
"
Erro ao escrever o arquivo:
{
e
}
"
)
logging
.
error
(
f
"
Error writing file
{
e
}
"
)
return
f
"
Error writing file
{
e
}
"
,
400
#Deleta algum tipo de conteudo enviado por requisição
def
delete_post
(
post_id
,
content_type
):
def
delete_post
(
post_id
,
content_type
):
#Deletando uma notícia
if
content_type
==
'
post
'
:
if
content_type
==
'
post
'
:
file_name
=
f
"
{
post_id
}
.md
"
file_name
=
f
"
{
post_id
}
.md
"
file_path
=
os
.
path
.
join
(
DIR_POSTS
,
file_name
)
file_path
=
os
.
path
.
join
(
DIR_POSTS
,
file_name
)
if
os
.
path
.
exists
(
file_path
):
if
os
.
path
.
exists
(
file_path
):
os
.
remove
(
file_path
)
os
.
remove
(
file_path
)
#print(f"Post com ID {post_id} deletado
")
logging
.
info
(
f
"
200 - DELETED: Post with ID number
{
post_id
}
"
)
else
:
else
:
print
(
f
"
Arquivo com ID
{
post_id
}
não encontrado para exclusão
"
)
logging
.
error
(
f
"
400 - ERROR: Post with with ID number
{
post_id
}
not found for exclusion
"
)
#Deletando um projeto
elif
content_type
==
'
projeto
'
:
elif
content_type
==
'
projeto
'
:
file_name
=
f
"
{
post_id
}
.md
"
file_name
=
f
"
{
post_id
}
.md
"
file_path
=
os
.
path
.
join
(
DIR_PROJECTS
,
file_name
)
file_path
=
os
.
path
.
join
(
DIR_PROJECTS
,
file_name
)
if
os
.
path
.
exists
(
file_path
):
if
os
.
path
.
exists
(
file_path
):
os
.
remove
(
file_path
)
os
.
remove
(
file_path
)
#print(f"Post com ID {post_id} deletado
")
logging
.
info
(
f
"
200 - DELETED: Project with ID number
{
post_id
}
"
)
else
:
else
:
print
(
f
"
Arquivo com ID
{
post_id
}
não encontrado para exclusão
"
)
logging
.
error
(
f
"
200 - ERROR: File of project with ID number
{
post_id
}
not found for exclusion
"
)
else
:
logging
.
error
(
f
"
400 - ERROR: Content type:
'
{
content_type
}
'
not recognized for deletion
"
)
#Função responsavel por interpretar as requisições do webhooks
@app.route
(
'
/
'
,
methods
=
[
'
POST
'
])
@app.route
(
'
/
'
,
methods
=
[
'
POST
'
])
def
run_script
():
def
run_script
():
data
=
request
.
json
data
=
request
.
json
#print("Dados recebidos:", data)
print
(
"
JSON received:
"
,
data
)
logging
.
info
(
"
Data (JSON) received
"
)
#Do arquivo JSON, separa ID do conteudo, tipo do evento e o conteúdo
post_id
=
data
.
get
(
'
entry
'
,
{}).
get
(
'
id
'
)
post_id
=
data
.
get
(
'
entry
'
,
{}).
get
(
'
id
'
)
event
=
data
.
get
(
'
event
'
)
event
=
data
.
get
(
'
event
'
)
content_type
=
data
.
get
(
'
model
'
,
''
)
# Obtém o tipo de conteúdo do Strapi
content_type
=
data
.
get
(
'
model
'
,
''
)
# Obtém o tipo de conteúdo do Strapi
print
(
f
"
Event
o
:
{
event
}
, ID:
{
post_id
}
, T
ipo
:
{
content_type
}
"
)
print
(
f
"
Event:
{
event
}
, ID:
{
post_id
}
, T
ype
:
{
content_type
}
"
)
if
not
post_id
:
if
not
post_id
:
logging
.
error
(
f
"
Code: 400
"
f
"
Status: ID not found in JSON:
{
data
}
"
)
logging
.
exception
(
"
error occurred while processing the event
"
)
return
"
ID not found in JSON
"
,
400
return
"
ID not found in JSON
"
,
400
if
event
in
[
'
entry.update
'
,
'
entry.publish
'
]:
if
event
in
[
'
entry.create
'
]:
logging
.
info
(
f
"
File created in Strapi- ID:
{
post_id
}
- Type:
{
content_type
}
"
f
"
\n
Data:
{
data
}
"
)
return
f
"
File created in Strapi
{
post_id
}
- Type:
{
content_type
}
"
,
200
elif
event
in
[
'
entry.update
'
,
'
entry.publish
'
]:
create_or_update_post
(
post_id
,
data
,
event
,
content_type
)
create_or_update_post
(
post_id
,
data
,
event
,
content_type
)
logging
.
info
(
f
"
\n
File created in Hugo- ID:
{
post_id
}
- Type:
{
content_type
}
"
f
"
\n
Data:
{
data
}
\n
Operation completed successfully
\n
"
)
return
f
"
Operation completed successfully
\n
Data:
{
data
}
"
,
200
elif
event
in
[
'
entry.delete
'
,
'
entry.unpublish
'
]:
elif
event
in
[
'
entry.delete
'
,
'
entry.unpublish
'
]:
delete_post
(
post_id
,
content_type
)
delete_post
(
post_id
,
content_type
)
logging
.
info
(
f
"
File removed - ID:
{
post_id
}
- Type:
{
content_type
}
"
)
return
f
"
File removed with ID
{
post_id
}
"
,
200
else
:
else
:
return
"
Evento não reconhecido
"
,
400
logging
.
error
(
f
"
Code: 400
\n
"
f
"
Status: Event
{
event
}
not recognized
\n
"
f
"
JSON Object:
{
data
}
"
)
logging
.
exception
(
"
error occurred while processing the event
"
)
return
f
"
Event
{
event
}
not recognized
"
,
400
return
"
Operação concluída com sucesso
"
,
200
if
__name__
==
'
__main__
'
:
if
__name__
==
'
__main__
'
:
app
.
run
(
port
=
5000
)
app
.
run
(
port
=
5000
)
\ No newline at end of file
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