Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
Jaime Collado
/
socialfairness-api
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
085369f4
authored
Nov 08, 2023
by
Jaime Collado
Browse files
Options
_('Browse Files')
Download
Email Patches
Plain Diff
Testing runs in parallel to make it faster
parent
eacdd3be
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
29 additions
and
11 deletions
app/api.py
app/api.py
View file @
085369f4
from
multiprocessing
import
Pool
from
fastapi
import
FastAPI
,
HTTPException
,
status
from
fastapi.middleware.cors
import
CORSMiddleware
...
...
@@ -20,6 +22,21 @@ app.add_middleware(
allow_headers
=
[
"*"
],
)
# ---------- UTILS ----------
def
test_scraper
(
url
):
scraped_comments
=
scrap
(
url
)
if
scraped_comments
:
return
True
return
False
def
scrap
(
url
):
scraper
=
GlobalScraper
(
url
)
df
=
scraper
.
process
()
scraped_comments
=
df
.
comments
.
tolist
()
return
scraped_comments
# ---------- HTTP METHODS ----------
@app.post
(
"/confiabilidad"
,
response_model
=
schemas
.
OutputPrediction
,
tags
=
[
"Prediction"
])
async
def
predict_reliability
(
...
...
@@ -67,9 +84,7 @@ async def scrap_url(
time
.
sleep
(
10
)
print
(
"Después del sleep"
)
scraper
=
GlobalScraper
(
url
.
url
)
df
=
scraper
.
process
()
scraped_comments
=
df
.
comments
.
tolist
()
scraped_comments
=
scrap
(
url
.
url
)
if
not
scraped_comments
:
raise
HTTPException
(
status_code
=
status
.
HTTP_404_NOT_FOUND
,
...
...
@@ -78,7 +93,7 @@ async def scrap_url(
return
{
"comments"
:
scraped_comments
}
@app.get
(
"/test-scraper"
,
response_model
=
schemas
.
ScrapersTested
,
tags
=
[
"Scraper"
])
async
def
test_scraper
():
async
def
test_scraper
s
():
"""Tests whether the scrapers work or not.
Returns:
...
...
@@ -104,11 +119,15 @@ async def test_scraper():
test_result
=
{
k
:
False
for
k
,
_
in
newspapers
.
items
()}
pool
=
Pool
(
processes
=
14
)
for
newspaper
,
url
in
newspapers
.
items
():
scraper
=
GlobalScraper
(
url
)
df
=
scraper
.
process
()
scraped_comments
=
df
.
comments
.
tolist
()
if
scraped_comments
:
test_result
[
newspaper
]
=
True
test_result
[
newspaper
]
=
pool
.
apply_async
(
test_scraper
,
[
url
])
pool
.
close
()
pool
.
join
()
final
=
{
k
:
v
.
get
()
for
k
,
v
in
test_result
.
items
()}
return
{
"scrapers"
:
final
}
return
{
"scrapers"
:
test_result
}
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a 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