Skip to content
Toggle navigation
Projects
Groups
Snippets
Help
Письменов Дмитрий Иванович
/
yourroomads
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Settings
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit 724f324c
authored
Jun 22, 2021
by
Vladislav
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'master' of
https://gitlab.veeble.ru/pismenov/yourroomads
2 parents
79ecfc30
be040e48
Hide whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
79 additions
and
79 deletions
app/Console/Commands/AdGroupsAdd.php
app/Console/Commands/KeywordsAdd.php
app/Jobs/ProcessCallAPI.php
app/Service/API/YandexDirect.php
app/Service/Handlers/DirectHandler.php
app/Service/Requests/Direct/AddAdGroups.php
app/Service/Requests/Direct/GetAdGroups.php
app/Service/Requests/Direct/GetKeywords.php
database/migrations/2021_06_07_08638_create_keywords_table.php
app/Console/Commands/AdGroupsAdd.php
View file @
724f324
...
@@ -46,7 +46,6 @@ class AdGroupsAdd extends Command
...
@@ -46,7 +46,6 @@ class AdGroupsAdd extends Command
->
get
();
->
get
();
foreach
(
$tokens
as
$token
)
{
foreach
(
$tokens
as
$token
)
{
$token
->
load
([
$token
->
load
([
'dictionaryCampaignsEnabledForExternalSynchronized.groupsForNotExternal'
=>
function
(
HasMany
$query
)
{
'dictionaryCampaignsEnabledForExternalSynchronized.groupsForNotExternal'
=>
function
(
HasMany
$query
)
{
return
$query
->
has
(
'group'
);
return
$query
->
has
(
'group'
);
...
...
app/Console/Commands/KeywordsAdd.php
View file @
724f324
...
@@ -79,10 +79,13 @@ class KeywordsAdd extends Command
...
@@ -79,10 +79,13 @@ class KeywordsAdd extends Command
])
])
->
get
();
->
get
();
GoalKeyword
::
whereIn
(
'id'
,
$goalKeywords
->
pluck
(
'id'
)
->
toArray
())
foreach
(
array_chunk
(
$goalKeywords
->
pluck
(
'id'
)
->
toArray
(),
1000
)
as
$items
){
->
update
([
GoalKeyword
::
whereIn
(
'id'
,
$items
)
'reserve_create_at'
=>
Carbon
::
now
(),
->
update
([
]);
'reserve_create_at'
=>
Carbon
::
now
(),
]);
}
$factory
->
getRequest
(
'Keywords'
,
'add'
)
$factory
->
getRequest
(
'Keywords'
,
'add'
)
->
call
([
->
call
([
...
...
app/Jobs/ProcessCallAPI.php
View file @
724f324
...
@@ -64,11 +64,13 @@ class ProcessCallAPI implements ShouldQueue
...
@@ -64,11 +64,13 @@ class ProcessCallAPI implements ShouldQueue
}
}
}
}
$response
=
$api
->
execute
();
$response
=
$api
->
execute
();
$handler
=
AdsHandler
::
getInstance
(
$this
->
api
);
$handler
->
checkError
(
$response
);
$limits
->
acceptRezerv
(
$this
->
limitId
,
new
HeaderLimits
(
$response
->
headers
()));
$limits
->
acceptRezerv
(
$this
->
limitId
,
new
HeaderLimits
(
$response
->
headers
()));
//TODO: обработать результат.
//TODO: обработать результат.
// если не хватило баллов на все что хотели запросить, то в очередь отправляем новый запрос на получение новых данных
// если не хватило баллов на все что хотели запросить, то в очередь отправляем новый запрос на получение новых данных
AdsHandler
::
getInstance
(
$this
->
api
)
->
handle
(
$response
);
$handler
->
handle
(
$response
);
}
catch
(
\Exception
$e
)
{
}
catch
(
\Exception
$e
)
{
//TODO: надо отдельно выделить ошибки вызовов, за которые списываются баллы
//TODO: надо отдельно выделить ошибки вызовов, за которые списываются баллы
//https://yandex.ru/dev/direct/doc/dg/concepts/errors.html
//https://yandex.ru/dev/direct/doc/dg/concepts/errors.html
...
...
app/Service/API/YandexDirect.php
View file @
724f324
...
@@ -3,6 +3,7 @@ namespace App\Service\API;
...
@@ -3,6 +3,7 @@ namespace App\Service\API;
use
Illuminate\Http\Client\Response
;
use
Illuminate\Http\Client\Response
;
use
Illuminate\Support\Facades\Http
;
use
Illuminate\Support\Facades\Http
;
use
Illuminate\Support\Facades\Log
;
class
YandexDirect
extends
API
{
class
YandexDirect
extends
API
{
private
$url
=
'https://oauth.yandex.ru/token'
;
private
$url
=
'https://oauth.yandex.ru/token'
;
...
@@ -42,9 +43,12 @@ class YandexDirect extends API{
...
@@ -42,9 +43,12 @@ class YandexDirect extends API{
public
function
execute
()
:
Response
public
function
execute
()
:
Response
{
{
return
Http
::
withBody
(
$this
->
request
->
getBody
(),
'none'
)
// Log::debug($this->request->getBody());
$result
=
Http
::
withBody
(
$this
->
request
->
getBody
(),
'none'
)
->
withHeaders
(
$this
->
request
->
getHeaders
())
->
withHeaders
(
$this
->
request
->
getHeaders
())
->
withToken
(
$this
->
request
->
getToken
()
->
token
)
->
withToken
(
$this
->
request
->
getToken
()
->
token
)
->
post
(
$this
->
request
->
getUrl
());
->
post
(
$this
->
request
->
getUrl
());
// Log::debug($result->json());
return
$result
;
}
}
}
}
app/Service/Handlers/DirectHandler.php
View file @
724f324
...
@@ -12,4 +12,11 @@ class DirectHandler extends AdsHandler
...
@@ -12,4 +12,11 @@ class DirectHandler extends AdsHandler
{
{
$this
->
response
=
$response
->
json
();
$this
->
response
=
$response
->
json
();
}
}
public
function
checkError
(
$response
){
$this
->
parse
(
$response
);
if
(
$this
->
response
[
'error'
]){
throw
new
\Exception
(
$this
->
response
[
'error'
][
'error_detail'
]);
}
}
}
}
app/Service/Requests/Direct/AddAdGroups.php
View file @
724f324
...
@@ -23,6 +23,7 @@ class AddAdGroups extends DirectRequest
...
@@ -23,6 +23,7 @@ class AddAdGroups extends DirectRequest
public
function
call
(
$params
=
null
)
public
function
call
(
$params
=
null
)
{
{
$this
->
requestPrepare
(
$params
);
$this
->
requestPrepare
(
$params
);
Log
::
debug
(
$this
->
getParams
());
$process
=
new
ProcessCallLimitedAPI
(
$this
);
$process
=
new
ProcessCallLimitedAPI
(
$this
);
dispatch
(
$process
)
->
onQueue
(
'limits'
);
dispatch
(
$process
)
->
onQueue
(
'limits'
);
}
}
...
...
app/Service/Requests/Direct/GetAdGroups.php
View file @
724f324
...
@@ -15,7 +15,7 @@ use Illuminate\Support\Facades\Log;
...
@@ -15,7 +15,7 @@ use Illuminate\Support\Facades\Log;
class
GetAdGroups
extends
DirectRequest
class
GetAdGroups
extends
DirectRequest
{
{
protected
$max_count
=
-
1
;
protected
$max_count
=
10000
;
protected
$max_count_CampaignIds
=
10
;
protected
$max_count_CampaignIds
=
10
;
protected
$max_count_Ids
=
10000
;
protected
$max_count_Ids
=
10000
;
...
@@ -29,13 +29,10 @@ class GetAdGroups extends DirectRequest
...
@@ -29,13 +29,10 @@ class GetAdGroups extends DirectRequest
public
function
getObjectsCount
()
public
function
getObjectsCount
()
{
{
$params
=
$this
->
getParams
();
$params
=
$this
->
getParams
();
if
(
isset
(
$params
[
'SelectionCriteria'
][
'CampaignIds'
]))
{
return
-
1
;
}
if
(
isset
(
$params
[
'SelectionCriteria'
][
'Ids'
]))
{
if
(
isset
(
$params
[
'SelectionCriteria'
][
'Ids'
]))
{
return
count
(
$params
[
'SelectionCriteria'
][
'Ids'
]);
return
count
(
$params
[
'SelectionCriteria'
][
'Ids'
]);
}
}
return
-
1
;
return
$this
->
getMaxCount
()
;
}
}
public
function
slice
(
$maxObjects
)
:
?
APIRequest
public
function
slice
(
$maxObjects
)
:
?
APIRequest
...
@@ -87,9 +84,9 @@ class GetAdGroups extends DirectRequest
...
@@ -87,9 +84,9 @@ class GetAdGroups extends DirectRequest
$external_id
=
(
string
)
$ad_group
[
'CampaignId'
];
$external_id
=
(
string
)
$ad_group
[
'CampaignId'
];
$campaign
=
$campaigns
->
firstWhere
(
'external_id'
,
$external_id
);
//
$campaigns->firstWhere('external_id', $external_id);
if
(
!
$campaign
)
{
if
(
!
$campaign
=
$campaigns
[
$external_id
]
??
false
)
{
continue
;
continue
;
}
}
...
@@ -136,7 +133,7 @@ class GetAdGroups extends DirectRequest
...
@@ -136,7 +133,7 @@ class GetAdGroups extends DirectRequest
]);
]);
}
}
}
else
{
}
/*
else {
$goalAdGroup = GoalAdGroup::firstWhere('external_id', $external_id);
$goalAdGroup = GoalAdGroup::firstWhere('external_id', $external_id);
if (!$goalAdGroup)
if (!$goalAdGroup)
...
@@ -151,7 +148,7 @@ class GetAdGroups extends DirectRequest
...
@@ -151,7 +148,7 @@ class GetAdGroups extends DirectRequest
}
}
$goalAdGroup->update($data);
$goalAdGroup->update($data);
}
}
*/
}
}
...
...
app/Service/Requests/Direct/GetKeywords.php
View file @
724f324
...
@@ -16,7 +16,7 @@ use Illuminate\Support\Facades\Log;
...
@@ -16,7 +16,7 @@ use Illuminate\Support\Facades\Log;
class
GetKeywords
extends
DirectRequest
class
GetKeywords
extends
DirectRequest
{
{
protected
$max_count
=
-
1
;
protected
$max_count
=
10000
;
protected
$max_count_CampaignIds
=
10
;
protected
$max_count_CampaignIds
=
10
;
protected
$max_count_AdGroupIds
=
1000
;
protected
$max_count_AdGroupIds
=
1000
;
protected
$max_count_Ids
=
10000
;
protected
$max_count_Ids
=
10000
;
...
@@ -59,6 +59,10 @@ class GetKeywords extends DirectRequest
...
@@ -59,6 +59,10 @@ class GetKeywords extends DirectRequest
try
{
try
{
$external_ids
=
[];
$external_ids
=
[];
if
(
!
$this
->
getToken
()
->
isMain
())
{
return
;
//добавлять фразы будем только по основному. По цеелвым будем наоборот выгружать.
}
if
(
!
isset
(
$response
[
'result'
][
'Keywords'
]))
{
if
(
!
isset
(
$response
[
'result'
][
'Keywords'
]))
{
return
;
return
;
}
}
...
@@ -80,14 +84,11 @@ class GetKeywords extends DirectRequest
...
@@ -80,14 +84,11 @@ class GetKeywords extends DirectRequest
$ad_groups
=
AdGroup
::
whereIn
(
'external_id'
,
array_keys
(
$external_ids
))
$ad_groups
=
AdGroup
::
whereIn
(
'external_id'
,
array_keys
(
$external_ids
))
->
get
()
->
get
()
->
keyBy
(
'external_id'
);
->
keyBy
(
'external_id'
);
}
else
{
$ad_groups
=
GoalAdGroup
::
whereIn
(
'external_id'
,
array_keys
(
$external_ids
))
->
get
()
->
keyBy
(
'external_id'
);
}
}
$ids
=
[];
// $ids = [];
$campaign_ids_synced_need
=
[];
// $campaign_ids_synced_need = [];
$insertData
=
[];
foreach
(
$response
[
'result'
][
'Keywords'
]
as
$keyword
)
{
foreach
(
$response
[
'result'
][
'Keywords'
]
as
$keyword
)
{
...
@@ -97,67 +98,53 @@ class GetKeywords extends DirectRequest
...
@@ -97,67 +98,53 @@ class GetKeywords extends DirectRequest
continue
;
continue
;
}
}
$external_id
=
(
string
)
$keyword
[
'Id'
];
$external_id
=
(
string
)
$keyword
[
'Id'
];
if
(
$this
->
getToken
()
->
isMain
())
{
$data
=
[
'external_id'
=>
$external_id
,
'campaign_external_id'
=>
$ad_group
->
campaign
->
external_id
,
'ad_group_external_id'
=>
$ad_group
->
external_id
,
'ad_group_id'
=>
$ad_group
->
getKey
(),
'campaign_id'
=>
$ad_group
->
campaign
->
getKey
(),
'keyword'
=>
$keyword
[
'Keyword'
],
'user_param_1'
=>
$keyword
[
'UserParam1'
]
??
null
,
'user_param_2'
=>
$keyword
[
'UserParam2'
]
??
null
,
'bid'
=>
$keyword
[
'Bid'
],
'context_bid'
=>
$keyword
[
'ContextBid'
],
'strategy_priority'
=>
$keyword
[
'StrategyPriority'
]
??
null
,
'state'
=>
$keyword
[
'State'
],
'status'
=>
$keyword
[
'Status'
],
'serving_status'
=>
$keyword
[
'ServingStatus'
],
'deleted_at'
=>
null
//не забыть убрать признак удаления, если вдруг опять пришла удаленная ранее фраза
];
$keyword
=
Keyword
::
updateOrCreate
([
'external_id'
=>
$external_id
],
$data
);
if
(
$keyword
->
wasRecentlyCreated
)
{
$campaign_ids_synced_need
[
$keyword
->
campaign_id
]
=
true
;
}
elseif
(
$keyword
->
wasChanged
([
'campaign_id'
]))
{
$campaign_ids_synced_need
[
$keyword
->
campaign_id
]
=
true
;
}
elseif
(
$keyword
->
wasChanged
(
$keyword
::
getPropertiesWatch
()
->
toArray
()))
{
$keyword
->
goalKeywords
()
->
has
(
'dictionaryCampaign'
)
->
forExternal
()
->
update
([
'updated_need'
=>
Carbon
::
now
(),
]);
}
$ids
[]
=
$keyword
->
getKey
();
}
else
{
$goalKeyword
=
GoalKeyword
::
firstWhere
(
'external_id'
,
$external_id
);
if
(
!
$goalKeyword
)
continue
;
$ids
[]
=
$goalKeyword
->
getKey
();
$data
=
[
'external_updated_at'
=>
Carbon
::
now
(),
];
if
(
$goalKeyword
->
dictionaryCampaign
->
updated
)
{
$data
[
'updated_need'
]
=
Carbon
::
now
();
}
$goalKeyword
->
update
(
$data
);
}
//идентификатор фразы у нас уникален. Если такая фраза уже есть, нет смысла ее менять, т.к. данные в общем то неизменны.
//если будет изменена фраза, то на самом деле будет фраза с новым идентификатором
//поэтому будем всегда новые добавлять и игонрить те что уже есть
$data
=
[
'external_id'
=>
$external_id
,
'campaign_external_id'
=>
$ad_group
->
campaign_external_id
,
'ad_group_external_id'
=>
$ad_group
->
external_id
,
'ad_group_id'
=>
$ad_group
->
getKey
(),
'campaign_id'
=>
$ad_group
->
campaign_id
,
'keyword'
=>
$keyword
[
'Keyword'
],
'user_param_1'
=>
$keyword
[
'UserParam1'
]
??
null
,
'user_param_2'
=>
$keyword
[
'UserParam2'
]
??
null
,
'bid'
=>
$keyword
[
'Bid'
],
'context_bid'
=>
$keyword
[
'ContextBid'
],
'strategy_priority'
=>
$keyword
[
'StrategyPriority'
]
??
null
,
'state'
=>
$keyword
[
'State'
],
'status'
=>
$keyword
[
'Status'
],
'serving_status'
=>
$keyword
[
'ServingStatus'
],
'deleted_at'
=>
null
//не забыть убрать признак удаления, если вдруг опять пришла удаленная ранее фраза
];
$insertData
[]
=
$data
;
// $keyword = Keyword::updateOrCreate([
// 'external_id' => $external_id
// ], $data);
// if ($keyword->wasRecentlyCreated) {
// $campaign_ids_synced_need[$keyword->campaign_id] = true;
// } elseif ($keyword->wasChanged(['campaign_id'])) {
// $campaign_ids_synced_need[$keyword->campaign_id] = true;
// } elseif ($keyword->wasChanged($keyword::getPropertiesWatch()->toArray())) {
// $keyword->goalKeywords()->has('dictionaryCampaign')->forExternal()->update([
// 'updated_need' => Carbon::now(),
// ]);
// }
}
}
foreach
(
array_chunk
(
$insertData
,
1000
)
as
$data
)
{
Keyword
::
insertOrIgnore
(
$data
);
}
// if ($this->getToken()->isMain()) {
// if ($this->getToken()->isMain()) {
//
//
// if (isset($this->getParams()['SelectionCriteria']['AdGroupIds'])) {
// if (isset($this->getParams()['SelectionCriteria']['AdGroupIds'])) {
...
...
database/migrations/2021_06_07_08638_create_keywords_table.php
View file @
724f324
...
@@ -32,7 +32,7 @@ class CreateKeywordsTable extends Migration
...
@@ -32,7 +32,7 @@ class CreateKeywordsTable extends Migration
Schema
::
create
(
'keywords'
,
function
(
Blueprint
$table
)
{
Schema
::
create
(
'keywords'
,
function
(
Blueprint
$table
)
{
$table
->
id
();
$table
->
id
();
$table
->
bigInteger
(
'external_id'
);
$table
->
bigInteger
(
'external_id'
)
->
unique
()
;
$table
->
bigInteger
(
'ad_group_external_id'
);
$table
->
bigInteger
(
'ad_group_external_id'
);
$table
->
bigInteger
(
'campaign_external_id'
);
$table
->
bigInteger
(
'campaign_external_id'
);
$table
->
bigInteger
(
'ad_group_id'
)
->
unsigned
();
$table
->
bigInteger
(
'ad_group_id'
)
->
unsigned
();
...
...
Write
Preview
Markdown
is supported
Attach a file
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 post a comment