Created
March 11, 2013 16:13
-
-
Save clay-whitley/5135368 to your computer and use it in GitHub Desktop.
A python script that consumes a CSV file with user data, parses out the data, and then calls an analytics API to import the user data into a content management system.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import csv | |
| import os | |
| import sys | |
| import datetime | |
| os.chdir("/home/Development/") | |
| fileList = os.listdir(".") | |
| # fnumber = int(sys.argv[1]) | |
| # fname = fileList[fnumber] | |
| fname = str(sys.argv[1]) | |
| dv = open(fname, 'rb') | |
| csvRead = csv.DictReader(dv) | |
| parsed_data = [] | |
| for row in csvRead: | |
| parsed_data.append([row["userkey"], row["BGC_User_ID"], row["BGC_Respondent_ID"], row["Transaction_value"]]) | |
| dv.close() | |
| # Logic for input to Liveball | |
| from suds.client import Client | |
| url = 'http://bgc2.postclickmarketing.com/Services/RespondentService.asmx?WSDL' | |
| client = Client(url) | |
| x = 0 | |
| errorCount = 0 | |
| for row in reversed(parsed_data): | |
| x+=1 | |
| uky = row[0] | |
| uid = row[1] | |
| rid = row[2] | |
| tval = row[3] | |
| tag = 'Activated' | |
| dname = 'Amount' | |
| if tval != "0": | |
| try: | |
| print uky + ',' + uid + ',' + rid + ',' + tval | |
| client.service.Convert(uid, uky, rid) | |
| client.service.SaveData(uid, uky, rid, dname, tval) | |
| except: | |
| print 'an error occurred with the API on respondent: ' + uky + ',' + uid + ',' + rid | |
| errorCount+=1 | |
| else: | |
| try: | |
| print uky + ',' + uid + ',' + rid | |
| client.service.Convert(uid, uky, rid) | |
| except: | |
| print 'an error occurred with the API on respondent: ' + uky + ',' + uid + ',' + rid | |
| errorCount+=1 | |
| print dv.name | |
| print x | |
| print "Errors:" + errorCount | |
| # Creating and populating the log.csv file | |
| os.chdir("/home/Development/Logs/") | |
| timestamp = str(datetime.datetime.utcnow()) | |
| log_name = timestamp + "_Log.csv" | |
| print log_name | |
| log = open(log_name, 'wb') | |
| csvWrite = csv.writer(log) | |
| for row in parsed_data: | |
| csvWrite.writerow(row) | |
| log.close() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment