Skip to content

Commit

Permalink
Add feature to UserAgentService -> getUserAgent
Browse files Browse the repository at this point in the history
  • Loading branch information
pazakharov committed Oct 30, 2022
1 parent 0b06414 commit 068d5c5
Show file tree
Hide file tree
Showing 9 changed files with 468 additions and 18 deletions.
12 changes: 12 additions & 0 deletions Src/SeleniumToolsModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use yii\base\Module;
use yii\base\BootstrapInterface;
use Zakharov\Yii2SeleniumTools\Console\SeleniumToolsController;
use Zakharov\Yii2SeleniumTools\Utils\UserAgent\UserAgentService;
use Zakharov\Yii2SeleniumTools\Utils\UserAgent\UserAgentProvider;
use Zakharov\Yii2SeleniumTools\Utils\UserAgent\UserAgentsDotIoParser;

Expand Down Expand Up @@ -65,6 +66,7 @@ public function bootstrap($app)
public function init()
{
$this->startedAt = time();
Yii::$container->setSingleton(\Zakharov\Yii2SeleniumTools\Utils\UserAgent\UserAgentService::class, \Zakharov\Yii2SeleniumTools\Utils\UserAgent\UserAgentService::class);
Yii::$container->setSingleton(\Zakharov\Yii2SeleniumTools\Utils\UserAgent\UserAgentProvider::class, $this->userAgentProvider);
Yii::$container->setSingleton(\Spatie\Crawler\Crawler::class, \Spatie\Crawler\Crawler::class);
Yii::$container->setSingleton(\Spatie\Crawler\CrawlQueues\CrawlQueue::class, \Spatie\Crawler\CrawlQueues\ArrayCrawlQueue::class);
Expand Down Expand Up @@ -121,4 +123,14 @@ public function getUserAgentProvider()
{
return Yii::$container->get(UserAgentProvider::class);
}

/**
* getUserAgentService
*
* @return UserAgentService
*/
public function getUserAgentService()
{
return Yii::$container->get(UserAgentService::class);
}
}
10 changes: 6 additions & 4 deletions Src/StepBrowser/StepBrowserComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Facebook\WebDriver\Remote\DesiredCapabilities;
use Zakharov\Yii2SeleniumTools\SeleniumToolsModule;
use Zakharov\Yii2SeleniumTools\StepBrowser\ProfileModel;
use Zakharov\Yii2SeleniumTools\Utils\UserAgent\UserAgentService;

/**
* StepBrowser it is a simple chromium profiles manager.
Expand Down Expand Up @@ -199,13 +200,14 @@ protected function getDefaultWebdriverBinary()

/**
* buildUserAgent
*
* @param array $keys Keys for find user agent strings in db
* @return string
*/
protected function buildUserAgent()
protected function buildUserAgent(array $keys = [])
{
//TODO: implement user agent generation
return 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36';
$service = $this->module->getUserAgentService();
$userAgent = $service->getUserAgent($keys);
return $userAgent->__toString();
}

/**
Expand Down
18 changes: 17 additions & 1 deletion Src/Utils/UserAgent/UserAgentService.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Zakharov\Yii2SeleniumTools\Utils\UserAgent;

use PDO;
use Yii;
use Zakharov\Yii2SeleniumTools\models\UserAgent;
use Zakharov\Yii2SeleniumTools\Utils\UserAgent\UserAgentProvider;
Expand Down Expand Up @@ -43,6 +42,23 @@ public function fillBaseWithUserAgents($filterKeys = [])
->execute();
}

/**
* getUserAgent
*
* @param string[] $keys
* @return UserAgent|null
*/
public function getUserAgent(array $keys = []): ?UserAgent
{
$query = UserAgent::find();
foreach ($keys as $key) {
$query->andFilterWhere(['like', 'ua', $key]);
}
$userAgents = $query->limit(100)->all();
shuffle($userAgents);
return array_pop($userAgents);
}

/**
* filterArrayOfUserAgentsWithFilter
*
Expand Down
5 changes: 5 additions & 0 deletions Src/models/UserAgent.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,9 @@ public function attributeLabels()
'created_at' => 'Created At',
];
}

public function __toString()
{
return $this->ua;
}
}
2 changes: 1 addition & 1 deletion Tests/Fixtures/UserAgentFixture.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace tests\unit\fixtures;
namespace tests\Fixtures;

use yii\test\ActiveFixture;
use Zakharov\Yii2SeleniumTools\models\UserAgent;
Expand Down
9 changes: 0 additions & 9 deletions Tests/_bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,3 @@

require_once __DIR__ . '/../vendor/yiisoft/yii2/Yii.php';
require __DIR__ . '/../vendor/autoload.php';

// $alias = \Yii::getAlias('@app');

// \Yii::$app->setModule('seleniumTools', [
// 'class' => \Zakharov\Yii2SeleniumTools\SeleniumToolsModule::class,
// 'params' => [
// 'screenshotPath' => '@app/runtime'
// ]
// ]);
Loading

0 comments on commit 068d5c5

Please sign in to comment.