Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
A
ai_interpolator_scraping_bot
Manage
Activity
Members
Labels
Plan
Wiki
Custom issue tracker
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Model registry
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
project
ai_interpolator_scraping_bot
Commits
b58a4f0b
Commit
b58a4f0b
authored
1 year ago
by
Marcus Johansson
Browse files
Options
Downloads
Patches
Plain Diff
Issue
#3447299
by Marcus_Johansson: Add a depth scraper to AI Interpolator
parent
151e2890
Branches
Branches containing commit
Tags
Tags containing commit
No related merge requests found
Pipeline
#174187
passed with warnings
1 year ago
Stage: build
Stage: validate
Changes
3
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
src/Batch/DepthCrawler.php
+42
-25
42 additions, 25 deletions
src/Batch/DepthCrawler.php
src/CrawlerHelper.php
+11
-1
11 additions, 1 deletion
src/CrawlerHelper.php
src/ScrapingBot.php
+4
-4
4 additions, 4 deletions
src/ScrapingBot.php
with
57 additions
and
30 deletions
src/Batch/DepthCrawler.php
+
42
−
25
View file @
b58a4f0b
...
...
@@ -27,13 +27,12 @@ class DepthCrawler {
* The context.
*/
public
static
function
startCrawl
(
$entity
,
$link
,
array
$config
,
$fieldDefinition
,
$mode
,
&
$context
)
{
if
(
!
empty
(
$config
[
'cool_down'
]))
{
// Milliseconds.
usleep
(
$config
[
'cool_down'
]
*
1000
);
}
if
(
!
isset
(
$context
[
'results'
][
'links_left'
]))
{
$context
[
'results'
][
'links_left'
]
=
$config
[
'links_left'
]
??
1
;
}
if
(
!
isset
(
$context
[
'results'
][
'found_links'
]))
{
$context
[
'results'
][
'found_links'
]
=
$config
[
'found_links'
]
??
[];
}
$context
[
'message'
]
=
'Crawling '
.
$link
;
$context
[
'results'
][
'links_left'
]
--
;
...
...
@@ -41,6 +40,11 @@ class DepthCrawler {
if
(
in_array
(
$link
,
$context
[
'results'
][
'found_links'
]
??
[]))
{
return
;
}
if
(
!
empty
(
$config
[
'cool_down'
]))
{
// Milliseconds.
usleep
(
$config
[
'cool_down'
]
*
1000
);
}
// Scrape the link.
$options
[
'useChrome'
]
=
$config
[
'use_chrome'
];
$options
[
'waitForNetworkRequests'
]
=
$config
[
'wait_for_network'
];
...
...
@@ -70,25 +74,8 @@ class DepthCrawler {
if
(
$value
)
{
$context
[
'results'
][
'found_texts'
][]
=
$value
;
}
$context
[
'results'
][
'found_links'
][]
=
$link
;
// If we are at the end, save and return.
if
(
$config
[
'depth'
]
==
0
)
{
if
(
$context
[
'results'
][
'links_left'
]
==
0
)
{
$saveTexts
=
[];
foreach
(
$context
[
'results'
][
'found_texts'
]
as
$foundText
)
{
if
(
$mode
==
'string'
)
{
$saveTexts
[]
=
$foundText
;
}
else
{
$saveTexts
[]
=
[
'value'
=>
$foundText
,
'format'
=>
\Drupal
::
service
(
'ai_interpolator_scraping_bot.crawler_helper'
)
->
getTextFormat
(
$fieldDefinition
)];
}
}
$entity
->
set
(
$fieldDefinition
->
getName
(),
$saveTexts
);
$entity
->
save
();
}
return
;
}
// If its wanted to just do inside the body, we get the body only using regex.
if
(
$config
[
'body_only'
])
{
preg_match
(
'/<body[^>]*>(.*?)<\/body>/is'
,
$rawHtml
,
$body
);
...
...
@@ -97,32 +84,62 @@ class DepthCrawler {
}
}
// Parse the html, collecting links starting with http* or / using regex.
$newOperations
=
[];
$batch
=
\batch_get
();
preg_match_all
(
'/href=["\']?([^"\'>]+)["\']?/'
,
$rawHtml
,
$matches
);
if
(
!
empty
(
$matches
[
1
]))
{
$links
=
$matches
[
1
];
$links
=
\Drupal
::
service
(
'ai_interpolator_scraping_bot.crawler_helper'
)
->
cleanLinks
(
$links
,
$link
,
$config
);
$config
[
'depth'
]
--
;
$batch
=
\batch_get
();
// If we have links, scrape them.
$config
[
'links_left'
]
=
$context
[
'results'
][
'links_left'
]
+
count
(
$links
);
$config
[
'found_links'
]
=
$context
[
'results'
][
'found_links'
];
foreach
(
$links
as
$link
)
{
// Get the extension if it has one.
$extension
=
pathinfo
(
$link
,
PATHINFO_EXTENSION
);
// If its in found links, we don't scrape it.
if
(
in_array
(
$link
,
$config
[
'found_links'
]))
{
continue
;
}
// If it has no extension or if it is a web page, we scrape it.
if
(
in_array
(
$extension
,
[
'html'
,
'htm'
,
'asp'
,
'php'
])
||
empty
(
$extension
))
{
// Add to the batch job.
$context
[
'results'
][
'links_left'
]
++
;
$
batch
[
'o
perations
'
]
[]
=
[
$
newO
perations
[]
=
[
'Drupal\ai_interpolator_scraping_bot\Batch\DepthCrawler::startCrawl'
,
[
$entity
,
$link
,
$config
,
$fieldDefinition
,
$mode
],
];
}
}
if
(
!
empty
(
$batch
[
'operations'
]))
{
if
(
!
empty
(
$newOperations
))
{
$batch
[
'operations'
]
=
!
empty
(
$batch
[
'operations'
])
?
array_merge_recursive
(
$batch
[
'operations'
],
$newOperations
)
:
$newOperations
;
\batch_set
(
$batch
);
}
}
$jobsLeft
=
FALSE
;
if
(
!
empty
(
$batch
[
'operations'
]))
{
foreach
(
$batch
[
'operations'
]
as
$operation
)
{
if
(
$operation
[
0
]
==
'Drupal\ai_interpolator_simple_crawler\Batch\DepthCrawler::startCrawl'
)
{
$jobsLeft
=
TRUE
;
break
;
}
}
}
// If there are no jobs left, save it.
if
(
!
$jobsLeft
)
{
$saveTexts
=
[];
foreach
(
$context
[
'results'
][
'found_texts'
]
as
$foundText
)
{
if
(
$mode
==
'string'
)
{
$saveTexts
[]
=
$foundText
;
}
else
{
$saveTexts
[]
=
[
'value'
=>
$foundText
,
'format'
=>
\Drupal
::
service
(
'ai_interpolator_simple_crawler.crawler_helper'
)
->
getTextFormat
(
$fieldDefinition
)];
}
}
$entity
->
set
(
$fieldDefinition
->
getName
(),
$saveTexts
);
$entity
->
save
();
}
}
}
This diff is collapsed.
Click to expand it.
src/CrawlerHelper.php
+
11
−
1
View file @
b58a4f0b
...
...
@@ -104,7 +104,11 @@ class CrawlerHelper {
foreach
(
$links
as
$link
)
{
// If it is a hash link, remove it.
if
(
str_contains
(
$link
,
'#'
))
{
continue
;
// We get the base link.
$link
=
explode
(
'#'
,
trim
(
$link
))[
0
];
if
(
!
$link
)
{
continue
;
}
}
// If it is a mailto link, remove it.
if
(
strpos
(
$link
,
'mailto:'
)
===
0
)
{
...
...
@@ -160,6 +164,12 @@ class CrawlerHelper {
}
// If its relative, but without /, make it absolute.
if
(
strpos
(
$link
,
'/'
)
!==
0
&&
strpos
(
$link
,
'http'
)
!==
0
)
{
// If the parent link has a file we have to remove it.
$parts
=
explode
(
'/'
,
$parentLink
);
array_pop
(
$parts
);
$parentLink
=
implode
(
'/'
,
$parts
);
$parentLink
=
substr
(
$parentLink
,
-
1
)
==
'/'
?
$parentLink
:
$parentLink
.
'/'
;
// Then add parents
$link
=
$parentLink
.
$link
;
}
// If its wanted to just keep the host, then remove the rest.
...
...
This diff is collapsed.
Click to expand it.
src/ScrapingBot.php
+
4
−
4
View file @
b58a4f0b
...
...
@@ -194,10 +194,10 @@ class ScrapingBot {
$apiEndPoint
.
=
count
(
$query_string
)
?
'?'
.
http_build_query
(
$query_string
)
:
''
;
// We can wait some.
$options
[
'connect_timeout'
]
=
3
0
;
$options
[
'read_timeout'
]
=
3
0
;
// Don't let Guzzle die, just forward body and status.
$options
[
'http_errors'
]
=
FALSE
;
$options
[
'connect_timeout'
]
=
6
0
;
$options
[
'read_timeout'
]
=
6
0
;
$options
[
'timeout'
]
=
60
;
// Headers.
$options
[
'auth'
]
=
[
$this
->
userName
,
...
...
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