Python script run directly but not from hapi.js using python-shell

190 views
Skip to first unread message

shu...@mobistreak.com

unread,
Dec 21, 2016, 4:12:10 AM12/21/16
to AdWords API Forum
If i call the script.py directly in my terminal then it works but when i call it from my controller.js then it does not. A simple program works in the script.py but my below code does not. I have tried it with options, sending data using events like on, message but nothing works. Can anyone help why it does not work? what should be done to make it work?

controller.js

var PythonShell = require('python-shell');
PythonShell.defaultOptions = { scriptPath: './server/python/' };

exports.adwords =  {
  handler: function(request, reply){
    PythonShell.run('script.py', function (err, res) {
      if (err) throw err;
      reply(res); 
    });
}); 
script.py
 
 
import logging
import os
import sys, json
import csv
from googleads import adwords

logging.basicConfig(level=logging.INFO)
logging.getLogger('suds.transport').setLevel(logging.DEBUG)


def main(client):
   report_downloader = client.GetReportDownloader(version='v201609')

report = {
  'reportName': 'LAST_7_DAYS AD_PERFORMANCE_REPORT',
  'dateRangeType': 'LAST_7_DAYS',
  'reportType': 'AD_PERFORMANCE_REPORT',
  'downloadFormat': 'CSV',
  'selector': {
      'fields': ['CallOnlyPhoneNumber', 'Cost']
  }
}

file_path = os.path.join(os.path.dirname(os.path.realpath(__file__)),"report.csv")
f = open(file_path,'wb')
f.write(report_downloader.DownloadReportAsString(report, skip_report_summary=True))
f.close()

with open(file_path, 'rb') as f:
  f.next()
  reader = csv.DictReader(f)
  data = dict()

  for row in reader:
    number = row['Call-only ad phone number'].replace("-", "")
    number = number.strip()
    if number:
      if number in data:
        data[number] += int(row['Cost'])
      else:
        data[number] = int(row['Cost'])

print data
f.close()

if __name__ == '__main__':
  adwords_client = adwords.AdWordsClient.LoadFromStorage()
  adwords_client.SetClientCustomerId('111-111-1111') 
  main(adwords_client)
Message has been deleted

shu...@mobistreak.com

unread,
Dec 21, 2016, 4:17:54 AM12/21/16
to AdWords API Forum
I am node.js developer but i learnt python and made my python script work according to me. But what i am not getting is why its not working. Is it some python mistake or something else?
Eventhough if i run it directly it works fine.

shu...@mobistreak.com

unread,
Dec 21, 2016, 4:27:15 AM12/21/16
to AdWords API Forum
Also i am using forever package, so that breaks whenever i call the script and forever does not show any error in console. Something is breaking the process but its not logging. 

Vishal Vinayak (Adwords API Team)

unread,
Dec 21, 2016, 2:01:27 PM12/21/16
to AdWords API Forum
Hi Shubham,

Looks like you are facing an issue when using our Python client library. I would recommend creating a new issue here so that our Python library managers can better understand your issue.   

Regards,
Vishal, AdWords API Team
Reply all
Reply to author
Forward
0 new messages