def main(client): # Initialize appropriate service. pql_service = client.GetService('PublisherQueryLanguageService', version='v201311') line_items_file = tempfile.NamedTemporaryFile(prefix='line_items_', suffix='.csv', delete=False) ad_units_file = tempfile.NamedTemporaryFile(prefix='ad_units_', suffix='.csv', delete=False) line_items_pql_query = ( 'SELECT Name, Id, Status FROM Line_Item ORDER BY Id ' 'ASC') ad_units_pql_query = 'SELECT Name, Id FROM Ad_Unit ORDER BY Id ASC' # Downloads the response from PQL select statement to the specified file print('Saved line items to... %s' % DfpUtils.DownloadPqlResultSetToCsv( pql_service, line_items_pql_query, line_items_file).name) print('Saved ad units to... %s' % DfpUtils.DownloadPqlResultSetToCsv( pql_service, ad_units_pql_query, ad_units_file).name)
def main(client): # Initialize appropriate service. pql_service = client.GetService('PublisherQueryLanguageService', version='v201405') output_file = tempfile.NamedTemporaryFile(prefix='line_items_named_like_', suffix='.csv', delete=False) # Create bind value to select line items with names starting with 'line item'. values = [{ 'key': 'name', 'value': { 'xsi_type': 'TextValue', 'value': 'line item%' } }] pql_query = ('SELECT Name, Id, Status FROM Line_Item ' 'WHERE Name LIKE :name') # Downloads the response from PQL select statement to the specified file print('Saved line items to... %s' % DfpUtils.DownloadPqlResultSetToCsv( pql_service, pql_query, output_file, values).name)
def main(client): # Initialize appropriate service. pql_service = client.GetService('PublisherQueryLanguageService', version='v201403') output_file = tempfile.NamedTemporaryFile(prefix='geo_target_type_', suffix='.csv', delete=False) # Create bind value to select geo-targets of type 'City'. values = [{ 'key': 'type', 'value': { 'xsi_type': 'TextValue', 'value': 'City' } }] pql_query = ('SELECT Name, Id FROM Geo_Target ' 'WHERE targetable = true AND Type = :type') # Downloads the response from PQL select statement to the specified file print('Saved geo targets to... %s' % DfpUtils.DownloadPqlResultSetToCsv( pql_service, pql_query, output_file, values).name)
def testDownloadPqlResultSetToCsv(self): pql_service = mock.Mock() csv_file = tempfile.NamedTemporaryFile() csv_file_name = csv_file.name header = [{ 'labelName': 'Some random header...' }, { 'labelName': 'Another header...' }] rval = [{ 'values': [{ 'value': 'Some random PQL response...', 'Value_Type': 'TextValue' }, { 'value': { 'date': { 'year': '1999', 'month': '04', 'day': '03' } }, 'Value_Type': 'DateValue' }, { 'value': '123', 'Value_Type': 'NumberValue' }, { 'value': { 'date': { 'year': '2012', 'month': '11', 'day': '05' }, 'hour': '12', 'minute': '12', 'second': '12', 'timeZoneID': 'PST8PDT' }, 'Value_Type': 'DateTimeValue' }] }, { 'values': [{ 'value': 'A second row of PQL response!', 'Value_Type': 'TextValue' }, { 'value': { 'date': { 'year': '2009', 'month': '02', 'day': '05' } }, 'Value_Type': 'DateValue' }, { 'value': '345', 'Value_Type': 'NumberValue' }, { 'value': { 'date': { 'year': '2013', 'month': '01', 'day': '03' }, 'hour': '02', 'minute': '02', 'second': '02', 'timeZoneID': 'GMT' }, 'Value_Type': 'DateTimeValue' }] }] def VerifyExpectedCall(arg): self.assertEqual( { 'values': None, 'query': ('SELECT Id, Name FROM Line_Item ' 'LIMIT 500 OFFSET 0') }, arg) return [{'rows': rval, 'columnTypes': header}] pql_service._service_name = 'PublisherQueryLanguageService' pql_service.select.side_effect = VerifyExpectedCall file_returned = DfpUtils.DownloadPqlResultSetToCsv( pql_service, 'SELECT Id, Name FROM Line_Item', csv_file) self.assertEqual(file_returned.name, csv_file_name) self.assertEqual(file_returned.readline(), ('"Some random header...",' '"Another header..."\r\n')) self.assertEqual(file_returned.readline(), ('"Some random PQL response...",' '"1999-04-03",' '123,' '"2012-11-05T12:12:12-08:00"\r\n')) self.assertEqual(file_returned.readline(), ('"A second row of PQL response!",' '"2009-02-05",' '345,' '"2013-01-03T02:02:02Z"\r\n')) csv_file.close()
from adspygoogle import DfpClient from adspygoogle.dfp import DfpUtils # Initialize client object. client = DfpClient(path=os.path.join('..', '..', '..', '..', '..')) # Initialize appropriate service. pql_service = client.GetService( 'PublisherQueryLanguageService', version='v201311') output_file = tempfile.NamedTemporaryFile(prefix='line_items_named_like_', suffix='.csv', delete=False) # Create bind value to select line items with names starting with 'line item'. values = [{ 'key': 'name', 'value': { 'xsi_type': 'TextValue', 'value': 'line item%' } }] pql_query = ('SELECT Name, Id, Status FROM Line_Item ' 'WHERE Name LIKE :name') # Downloads the response from PQL select statement to the specified file print ('Saved line items to... %s' % DfpUtils.DownloadPqlResultSetToCsv( pql_service, pql_query, output_file, values).name)
import tempfile sys.path.insert(0, os.path.join('..', '..', '..', '..', '..')) # Import appropriate classes from the client library. from adspygoogle import DfpClient from adspygoogle.dfp import DfpUtils # Initialize client object. client = DfpClient(path=os.path.join('..', '..', '..', '..', '..')) # Initialize appropriate service. pql_service = client.GetService( 'PublisherQueryLanguageService', version='v201311') line_items_file = tempfile.NamedTemporaryFile(prefix='line_items_', suffix='.csv', delete=False) ad_units_file = tempfile.NamedTemporaryFile(prefix='ad_units_', suffix='.csv', delete=False) line_items_pql_query = 'SELECT Name, Id, Status FROM Line_Item ORDER BY Id ASC' ad_units_pql_query = 'SELECT Name, Id FROM Ad_Unit ORDER BY Id ASC' # Downloads the response from PQL select statement to the specified file print ('Saved line items to... %s' % DfpUtils.DownloadPqlResultSetToCsv( pql_service, line_items_pql_query, line_items_file).name) print ('Saved ad units to... %s' % DfpUtils.DownloadPqlResultSetToCsv( pql_service, ad_units_pql_query, ad_units_file).name)