Specifying a Custom Date Range in Googleads API TargetingIdeaService - python

I am trying to use the Googleads API TargetingIdeaService to plan budget and get forecast for a list of keywords, as in Keyword Planner:
Keyword Planner Screenshot
I want to specify the date range as next year (for example, 20180301~20190228), but I can't find any parameters or syntax that allow me to do so. The MonthlySearchVolumeAttribute is for selecting data for the past 12 months, so that doesn't seem to work for the situation.
Below is my python script for defining the selector:
selector = {
'searchParameters': [
{
'xsi_type': 'RelatedToQuerySearchParameter',
'queries': keyword_list
},
{
# Location setting (optional)
'xsi_type': 'LocationSearchParameter',
'locations': [{'id': '2840'}] #US
},
{
# Language setting (optional).
'xsi_type': 'LanguageSearchParameter',
'languages': [{'id': '1000'}] #English
},
{
# Network search parameter (optional)
'xsi_type': 'NetworkSearchParameter',
'networkSetting': {
'targetGoogleSearch': True,
'targetSearchNetwork': False,
'targetContentNetwork': False,
'targetPartnerSearchNetwork': True
}
}
],
'ideaType': 'KEYWORD',
'requestType': 'STATS',
'requestedAttributeTypes': ['KEYWORD_TEXT', 'SEARCH_VOLUME','COMPETITION','AVERAGE_CPC'],
# Configure the selector's Paging to limit the number of results returned by a single request
'paging': {
'startIndex': str(offset),
'numberResults': str(PAGE_SIZE)
},
}
Is there anything I can do to achieve my goal? Thanks!

Related

CDK WAF Python Multiple Statement velues error

I have AWS WAF CDK that is working with rules, and now I'm trying to add a rule in WAF with multiple statements, but I'm getting this error:
Resource handler returned message: "Error reason: You have used none or multiple values for a field that requires exactly one value., field: STATEMENT, parameter: Statement (Service: Wafv2, Status Code: 400, Request ID: 6a36bfe2-543c-458a-9571-e929142f5df1, Extended Request ID: null)" (RequestToken: b751ae12-bb60-bb75-86c0-346926687ea4, HandlerErrorCode: InvalidRequest)
My Code:
{
'name': 'ruleName',
'priority': 3,
'statement': {
'orStatement': {
'statements': [
{
'iPSetReferenceStatement': {
'arn': 'arn:myARN'
}
},
{
'iPSetReferenceStatement': {
'arn': 'arn:myARN'
}
}
]
}
},
'action': {
'allow': {}
},
'visibilityConfig': {
'sampledRequestsEnabled': True,
'cloudWatchMetricsEnabled': True,
'metricName': 'ruleName'
}
},
There are two things going on there:
Firstly, your capitalization is off. iPSetReferenceStatement cannot be parsed and creates an empty statement reference. The correct key is ipSetReferenceStatement.
However, as mentioned here, there is a jsii implementation bug causing some issues with the IPSetReferenceStatementProperty. This causes it not to be parsed properly resulting in a jsii error when synthesizing.
You can fix it by using the workaround mentioned in the post.
Add to your file containing the construct:
import jsii
from aws_cdk import aws_wafv2 as wafv2 # just for clarity, you might already have this imported
#jsii.implements(wafv2.CfnRuleGroup.IPSetReferenceStatementProperty)
class IPSetReferenceStatement:
#property
def arn(self):
return self._arn
#arn.setter
def arn(self, value):
self._arn = value
Then define your ip reference statement as follows:
ip_set_ref_stmnt = IPSetReferenceStatement()
ip_set_ref_stmnt.arn = "arn:aws:..."
ip_set_ref_stmnt_2 = IPSetReferenceStatement()
ip_set_ref_stmnt_2.arn = "arn:aws:..."
Then in the rules section of the webacl, you can use it as follows:
...
rules=[
{
'name': 'ruleName',
'priority': 3,
'statement': {
'orStatement': {
'statements': [
wafv2.CfnWebACL.StatementProperty(
ip_set_reference_statement=ip_set_ref_stmnt
),
wafv2.CfnWebACL.StatementProperty(
ip_set_reference_statement=ip_set_ref_stmnt_2
),
]
}
},
'action': {
'allow': {}
},
'visibilityConfig': {
'sampledRequestsEnabled': True,
'cloudWatchMetricsEnabled': True,
'metricName': 'ruleName'
}
}
]
...
This should synthesize your stack as expected.

How to change keyword status with Google Ads API and Python

I was trying to change the keyword status via Google Ads API the following code shows how to update the keyword bid... however, I was looking for a way to set the keyword status as paused, I haven't been able to find any info within the documentation to paused the keyword
from googleads import adwords
AD_GROUP_ID = 'INSERT_AD_GROUP_ID_HERE'
CRITERION_ID = 'INSERT_KEYWORD_CRITERION_ID_HERE'
def main(client, ad_group_id, criterion_id):
# Initialize appropriate service.
ad_group_criterion_service = client.GetService(
'AdGroupCriterionService', version='v201809')
# Construct operations and update bids.
operations = [{
'operator': 'SET',
'operand': {
'xsi_type': 'BiddableAdGroupCriterion',
'adGroupId': ad_group_id,
'criterion': {
'id': criterion_id,
},
'biddingStrategyConfiguration': {
'bids': [
{
'xsi_type': 'CpcBid',
'bid': {
'microAmount': '1000000'
}
}
]
}
}
}]
ad_group_criteria = ad_group_criterion_service.mutate(operations)
# Display results.
if 'value' in ad_group_criteria:
for criterion in ad_group_criteria['value']:
if criterion['criterion']['Criterion.Type'] == 'Keyword':
print('Ad group criterion with ad group id "%s" and criterion id '
'"%s" currently has bids:'
% (criterion['adGroupId'], criterion['criterion']['id']))
for bid in criterion['biddingStrategyConfiguration']['bids']:
print('\tType: "%s", value: %s' % (bid['Bids.Type'],)
bid['bid']['microAmount'])
else:
print('No ad group criteria were updated.')
if __name__ == '__main__':
# Initialize client object.
adwords_client = adwords.AdWordsClient.LoadFromStorage()
main(adwords_client, AD_GROUP_ID, CRITERION_ID)
Thanks in advance for the help...
After more research I found the following guide.
https://developers.google.com/adwords/api/docs/reference/v201809/AdGroupCriterionService.BiddableAdGroupCriterion#userstatus
In order to modified the keyword status the construct operations needs to be like this.
operations = [{
'operator': 'SET',
'operand': {
'xsi_type': 'BiddableAdGroupCriterion',
'adGroupId': ad_group_id,
'criterion': {
'id': criterion_id,
},
'userStatus': 'ENABLED'
}
}]

i want place order virtual server with mutiple quantity ,how i can generate order container

Hi all i have used softlayer api using python
i need place virtual server with multiple quantity ,how generate order container ??
i have like this,
import SoftLayer
client = SoftLayer.Client(username='XXXXXX',api_key='xxxxxx')
vmorderparmers = {
'hostname':'testhost',
'domain': 'exampledomain.com',
'datacenter': 'sjc01',
'startCpus':1,
'maxMemory': 1024,
'localDiskFlag': True,
'hourlyBillingFlag': True,
'operatingSystemReferenceCode':'CENTOS_6_64',
"blockDevices": [
{
"device": "0",
"diskImage": {
"capacity": 100
}
}
]
}
oc = client['Virtual_Guest'].generateOrderTemplate(vmorderparmers)
after i have check qunatity
oc['quantity']
give one how i change that
suppose i change quantity like this
oc['quantity']=2
result=client['Product_Order'].placeOrder(oc)
i got error invalid order contianer
There are many ways to order several virtual servers, in your case the method SoftLayer_Virtual_Guest::generateOrderTemplate returns the container type SoftLayer_Container_Product_Order_Virtual_Guest that can be send to the method SoftLayer_Product_Order::verifyOrder or SoftLayer_Product_Order::placeOrder.
If you look the example for Virtual Servers in the SoftLayer_Product_Order::placeOrder page. You'll notice that you need to modify the quantity AND virtualGuest parameters as following.
'virtualGuests': [
{
'domain': 'exampledomain.com',
'hostname': 'testhost1'
},
{
'domain': 'exampledomain.com',
'hostname': 'testhost2'
},
{
'domain': 'exampledomain.com',
'hostname': 'testhost3'
}
],
'quantity': 3
Then your python code should looks like this:
"""
Create a VSI using the simplified way.
The script creates an order template by using the method generateOrderTemplate(), we'll
modify the response in order to create several virtual servers.
See below for more information.
Important manual pages:
http://sldn.softlayer.com/reference/services/SoftLayer_Virtual_Guest/generateOrderTemplate/
http://sldn.softlayer.com/reference/datatypes/SoftLayer_Container_Product_Order_Virtual_Guest
http://sldn.softlayer.com/reference/datatypes/SoftLayer_Container_Product_Order
http://sldn.softlayer.com/reference/services/SoftLayer_Product_Order/verifyOrder/
http://sldn.softlayer.com/reference/services/SoftLayer_Product_Order/placeOrder/
http://sldn.softlayer.com/reference/datatypes/SoftLayer_Virtual_Guest
License: http://sldn.softlayer.com/article/License
Author: SoftLayer Technologies, Inc. <sldn#softlayer.com>
"""
import SoftLayer
# Your SoftLayer API username.
USERNAME = 'set me'
API_KEY = 'set me'
# To get the configuration options to create the server call the
# SoftLayer_Virtual_Guest::getCreateObjectOptions method.
vmorderparmers = {
'hostname': 'testhost',
'domain': 'exampledomain.com',
'datacenter': {'name': 'sjc01'},
'startCpus': 1,
'maxMemory': 1024,
'localDiskFlag': True,
'hourlyBillingFlag': True,
'operatingSystemReferenceCode': 'CENTOS_6_64',
'blockDevices': [
{
'device': '0',
'diskImage': {'capacity': 100}
}
]
}
# Declare the API client
client = SoftLayer.create_client_from_env(username=USERNAME, api_key=API_KEY)
try:
container = client['SoftLayer_Virtual_Guest'].generateOrderTemplate(vmorderparmers)
# In order to create several VSI we modify the following parameters in the response
container['quantity'] = 3
# If you set quantity greater than 1 then you need to define
# hostname/domain per virtual server you wish to order.
container['virtualGuests'] = [
{
'domain': 'exampledomain.com',
'hostname': 'testhost1'
},
{
'domain': 'exampledomain.com',
'hostname': 'testhost2'
},
{
'domain': 'exampledomain.com',
'hostname': 'testhost3'
}
]
"""
Now we are able to verify or place the order.
verifyOrder() will check your order for errors. Replace this with a call to
placeOrder() when you're ready to order. Both calls return a receipt object
that you can use for your records.
"""
receipt = client['SoftLayer_Product_Order'].verifyOrder(container)
print (receipt)
except SoftLayer.SoftLayerAPIError as e:
"""
If there was an error returned from the SoftLayer API then bomb out with the
error message.
"""
print('Unable to create the VSI. %s, %s ' % (e.faultCode, e.faultString))
I hope this help you.

Using where parameter to filter on embedded data_relation?

Is it possible to filter on an embedded data_relation? I was hoping that it would be possible to make request like: /domains?where={"account.field_2_filter_by": "myvalue"} or /domains?where={"account":{"field_2_filter_by": "myvalue"}} but I have not had success.
For example, each DOMAIN item has an associated ACCOUNT item. The DOMAIN account field defines a data_relation to the ACCOUNTS schema, rather than a dict type. In both the ACCOUNTS and DOMAINS schema, the field_2_filter_by was added to the allowed_filters.
schema example:
# URL is /accounts
ACCOUNTS = {
'query_objectid_as_string': True,
'schema': {
'_id': {
'type': 'string',
'readonly': True,
},
'field_2_filter_by': {
'type': 'string',
'readonly': True,
}
},
'resource_methods': ['GET'],
'item_methods': ['GET'],
'allowed_filters': ['_id', 'field_2_filter_by'],
...
}
# URL is /domains
DOMAINS = {
'query_objectid_as_string': True,
'schema': {
'_id': {
'type': 'string',
'readonly': True,
},
'domain': {
'type': 'string',
'readonly': True,
'data_relation': {
'resource': 'accounts',
'embeddable': True
},
}
},
'resource_methods': ['GET'],
'item_methods': ['GET'],
'allowed_filters': ['account', 'account.field_2_filter_by'],
...
}
Lookups are performed on the endpoint resource (the target collection). This is mainly due to the underlying Mongo database, and to how Mongo handles data. There are no joins in mongo, and Eve achieves 'embedding' by performing an additional lookups on the embedded document/collection. You might want to design you data model accordingly, in order to make lookups/queries more performant. A good starting point is MongoDB Data Models and Example Patterns.
For the record, with support for Aggregation Framework coming up with Eve v0.7, you will be able to hook the results of an aggregation query to API endpoints, which might allow for new solutions in this field.

Add Timestamp to ElasticSearch with Elasticsearch-py using Bulk-API

I'm trying to add a timestamp to my data, have elasticsearch-py bulk index it, and then display the data with kibana.
My data is showing up in kibana, but my timestamp is not being used. When I go to the "Discovery" tab after configuring my index pattern, I get 0 results (yes, I tried adjusting the search time).
Here is what my bulk index json looks like:
{'index':
{'_timestamp': u'2015-08-11 14:18:26',
'_type': 'webapp_fingerprint',
'_id': u'webapp_id_redacted_2015_08_13_12_39_34',
'_index': 'webapp_index'
}
}
****JSON DATA HERE***
This will be accepted by elasticsearch and will get imported into Kibana, but the _timestamp field will not actually be indexed (it does show up in the dropdown when configuring an index pattern under "Time-field name").
I also tried formatting the metaFields like this:
{'index': {
'_type': 'webapp_fingerprint',
'_id': u'webapp_id_redacted_2015_08_13_12_50_04',
'_index': 'webapp_index'
},
'source': {
'_timestamp': {
'path': u'2015-08-11 14:18:26',
'enabled': True,
'format': 'YYYY-MM-DD HH:mm:ss'
}
}
}
This also doesn't work.
Finally, I tried including the _timestamp field within the index and applying the format, but I got an error with elasticsearch.
{'index': {
'_timestamp': {
'path': u'2015-08-11 14:18:26',
'enabled': True,
'format': 'YYYY-MM-DD HH:mm:ss'
},
'_type': 'webapp_fingerprint',
'_id': u'webapp_id_redacted_2015_08_13_12_55_53',
'_index': 'webapp_index'
}
}
The error is:
elasticsearch.exceptions.TransportError:
TransportError(500,u'IllegalArgumentException[Malformed action/metadata
line [1], expected a simple value for field [_timestamp] but found [START_OBJECT]]')
Any help someone can provide would be greatly appreciated. I apologize if I haven't explained the issue well enough. Let me know if I need to clarify more. Thanks.
Fixed my own problem. Basically, I needed to add mappings for the timestamp when I created the index.
request_body = {
"settings" : {
"number_of_shards": 1,
"number_of_replicas": 0
},
"mappings" : {
"_default_":{
"_timestamp":{
"enabled":"true",
"store":"true",
"path":"plugins.time_stamp.string",
"format":"yyyy-MM-dd HH:m:ss"
}
}
}
}
print("creating '%s' index..." % (index_name))
res = es.indices.create(index = index_name, body = request_body)
print(" response: '%s'" % (res))
In the latest versions of Elasticsearch, just using the PUT/POST API and ISOFORMAT strings should work.
import datetime
import requests
query = json.dumps(
{
"createdAt": datetime.datetime.now().replace(microsecond=0).isoformat(),
}
)
response = requests.post("https://search-XYZ.com/your-index/log", data=query,
headers={'Content-Type': 'application/json'})
print(response)

Categories

Resources