For loop not iterating in Mailchimp - python

So, I'm trying to get every sent to list from the Mailchimp API. I've made this code to elaborate a list of it to later generate a join and get both the list and the campaign name and not the campaign id only:
try:
client = MailchimpMarketing.Client()
client.set_config({
"api_key": "xxxx",
"server": "xxx"
})
response_id = client.campaigns.list(count=1000)
df_full = pd.json_normalize(response_id['campaigns'])
df_id = df_full['id']
df_id = df_id.values.tolist()
df_id_camp_name = df_full[['id','settings.subject_line']]
#display(df_id)
except ApiClientError as error:
print("Error: {}".format(error.text))
This part of the script gets the ID for each campaign and each campaign name.
for id in df_id:
try:
client = MailchimpMarketing.Client()
client.set_config({
"api_key": "xxxxx",
"server": "xxxx"
})
response_open = client.reports.get_campaign_recipients(id, count=1000)
df_open = pd.json_normalize(response_open['sent_to'])
df_open_full = df_open.append(df_open)
except ApiClientError as error:
print("Error: {}".format(error.text))
df_open_full.to_excel("campaign_reports_mailchimp.xlsx")
And this last piece, takes the ID from the generated list and allegedly runs the code for each ID. Problem is that the generated excel only brings one campaign and doesn't iterates for the rest of the IDs.
I know that the API has a limit count but it should move to the next ID once the cap reaches since it restarts the for loop itself.
What could be wrong in this case?
Thanks!

Adding some details about the print itself of each piece of code:
First block:
['076ff218d1', '08f16d1014', '0abfd11f5d', '0bb98a7839', '0bca92ee08', '0be206c6ac', '0e048c0d08', '0e28d0feee', '138271cade', '14bf2cd33b', '15c9ce24ed', '17b5302f30', '19721c1e8a', '1d8cc5c1da', '1f5362e2f4', '205480a063', '225bc2469b', '22f286dfbe', '26dec9723b', '2846539c87', '296e9c24f5', '2aa089aa37', '2f819130ff', '352d7913ae', '3a563ffb24', '3a73d3e5b6', '3c83f64170', '3d87a76763', '3e6a903948', '404ab63b91', '4198b629c6', '424b941199', '42e948e744', '46a29946a3', '48e56d7481', '4a0a55eb73', '4caf7e8cc1', '4e3c90946f', '53c8e8a8de', '54600382dd', '55a8915fb8', '5a28843185', '5d575f0de8', '60488c9e4b', '612b068a5b', '6161c05556', '61f5edcefa', '623c98757a', '689ae72a35', '68a8b5dadd', '6b3797ea1a', '6b606e78fb', '6dd276171d', '6ead2584c8', '6f99e38311', '70632fe9e7', '709b6fd5f8', '72a1b314b4', '74b92a828e', '75bdf2a3fe', '75cce79a85', '7687c62b55', '79e63229a8', '79f67ec7b8', '7f9dddc6c0', '807a75418e', '8548a5f525', '8602aa9cdd', '87c4bd5a07', '8bb822eeb3', '8ec05b63fa', '8f0c7d0cce', '900018816a', '924976c731', '933a2af282', '95a170d72c', '977beb5616', '98f8ee7fed', '99dbbc746c', '9a01a1b518', 'a1ad97ae8e', 'a4aa98b22b', 'a7c535a5b9', 'a978bab42b', 'ab13c82454', 'ab7c015390', 'acdc57b754', 'ad66024938', 'ae8464e351', 'ae95f63873', 'aeba2b962a', 'af0d9fe032', 'af6a4efe07', 'b19c553cd1', 'b5f4196035', 'b7a9ced6c8', 'b7eab10f0f', 'b80b52c97b', 'bd56fd7a6d', 'bdbb60aec7', 'c142343cfd', 'c2eb923ada', 'c407b9856d', 'c4636be5a1', 'c6145916ae', 'c84e39f8ef', 'c937f5876e', 'c97497c3e4', 'ca468b0942', 'cf2a040b92', 'cf81c2ac84', 'd006696585', 'd1b57067d2', 'd67915da02', 'd687b97dec', 'd698158ac5', 'd78cb47ccd', 'da0e85a878', 'dfc6a9bffc', 'dfe8e851e8', 'e08ce9ad82', 'e33f24fdcb', 'e4c478afb4', 'e8e3faaf5a', 'ebee2d5079', 'ecafe77954', 'ef1dae3863', 'f045de38f4', 'fa07a15c0e', 'fa3936c575', 'fa4def8ca1', 'fc1f708dc7', 'fe4f89c745']
Second block of code:
Extract of code display(df)
Also adding an error displayed in the loop because I'm using pd.append instead of pd.concatenate (since I don't have any other df to concatenate with)
FutureWarning: The frame.append method is deprecated and will be
removed from pandas in a future version. Use pandas.concat instead.

Related

passing parameter in Rest API request from other file or list variable- using

new to python and API.
i have list of values like below
typeid=['1','12','32','1000','9']
I have to pass this value as parameter in API request, so that it would take one typeid at a time and append the json. code i have following but not sure how it will move from one value to other?
# activity type id store as following in other .py file typeid=['1','12','32','1000','9']
#importing the file in main program file.
From typeid list import activitytypeids
act1 = requests.get(host + '/rest/v1/activities.json',
params={
'activityTypeIds': activitytypeids[0]
}).text
json_obj = json.loads(act1)
results.append(json_obj)
more_result = json_obj['moreResult']
while True:
act1 = requests.get(host + '/rest/v1/activities.json',
params={
'activityTypeIds': activitytypeids[0]
}).text
json_obj = json.loads(act1)
results.append(json_obj)
more_result =json(results['moreResult'])
if not more_result:
break
How do I pass the activity's in request param one by one, so that get the result of all type ids.
take your code to get one id and put it in a function that accepts an activity_id, and change all activitytypeids[0] to just be activity_id
From typeid list import activitytypeids
def get_activity_id(activity_id):
act1 = requests.get(host + '/rest/v1/activities.json',
params={
'activityTypeIds': activity_id
}).text
return act1.json()
then you can just iterate over your list
results = [get_activity_id(id) for id in activitytypeids]
that said it seems very surprising that a variable named activityTypeIds only accepts one id ... i would very much expect this to be able to accept a list based on nothing more than the variable name

How do I store JSON response to Array - Python request

I can't find a solution because the lack of understanding of programming.
I have created multiple Python Scripts where I do API calls to get results, the results are converted to JSON.
I want to store two specific fields from the JSON result for each user in an array, so I then later can make a "search" based of a users input(field), match it to the array and send a post request later.
The fields I want is: 'email' and 'userid', so when I search for the input (email) I can retrieve the userid, and send the post call from a button click.
So I understand that I can retrieve either email or userid by using the teo first lines to print the values. But the issue is when i try to store it in the array:
users = response.json()
print(users['user'][1]['userid']) #get user id
print(users['user'][1]['email']) #get email
json_length = len(users['user']) #get amount of users
print(json_length)
user_list = []
for i in users:
user_details = {"id":None, "email":None}
user_details['id'] = users['userid'][i]
user_details['email'] = users['email'][i]
user_list.append(user_details)
print(user_list)
I get the following error:
Exception has occurred: TypeError list indices must be integers or
slices, not str
File "C:...test2.py",
line 32, in
user_details['id'] = users['user'][i]['userid']
The JSON (multiple fields are removed and renamed)
{
"total":"2001",
"totalcount":"2",
"user":[
{
"userid":1,
"name":{
"firstname":"John",
"lastname":"Doe (Inactive)"
},
"email":"j.doe#something.com",
"status":"Inactive",
"organisation":{
"orgname":"something",
"orgid":1
},
},
{
"userid":2,
"name":{
"firstname":"Emma",
"lastname":"Hart (Inactive)"
},
"email":"e.hart#otherthing.com",
"status":"Inactive",
"organisation":{
"orgname":"otherthing",
"orgid":2
},
}
]
}
Any help for pointing what I'm doing wrong or help?
You're looping over the full JSON response keys, not the actual users list, then i would be each user object, so use that rather than the response again
Try
for i in users.get('user', []):
...
user_details['id'] = i['userid']
user_details['email'] = i['email']
You can also build that list in one line
user_list = [ {"id":u["userid"], "email":u["email"]} for u in users.get('user', []) ]
print(user_list)

parse github api.. getting string indices must be integers error

I need to loop through commits and get name, date, and messages info from
GitHub API.
https://api.github.com/repos/droptable461/Project-Project-Management/commits
I have many different things but I keep getting stuck at string indices must be integers error:
def git():
#name , date , message
#https://api.github.com/repos/droptable461/Project-Project-Management/commits
#commit { author { name and date
#commit { message
#with urlopen('https://api.github.com/repos/droptable461/Project Project-Management/commits') as response:
#source = response.read()
#data = json.loads(source)
#state = []
#for state in data['committer']:
#state.append(state['name'])
#print(state)
link = 'https://api.github.com/repos/droptable461/Project-Project-Management/events'
r = requests.get('https://api.github.com/repos/droptable461/Project-Project-Management/commits')
#print(r)
#one = r['commit']
#print(one)
for item in r.json():
for c in item['commit']['committer']:
print(c['name'],c['date'])
return 'suc'
Need to get person who did the commit, date and their message.
item['commit']['committer'] is a dictionary object, and therefore the line:
for c in item['commit']['committer']: is transiting dictionary keys.
Since you are calling [] on a string (the dictionary key), you are getting the error.
Instead that code should look more like:
def git():
link = 'https://api.github.com/repos/droptable461/Project-Project-Management/events'
r = requests.get('https://api.github.com/repos/droptable461/Project-Project-Management/commits')
for item in r.json():
for key in item['commit']['committer']:
print(item['commit']['committer']['name'])
print(item['commit']['committer']['date'])
print(item['commit']['message'])
return 'suc'

sed recognition response to DynamoDB table using Lambda-python

I am using Lambda to detect faces and would like to send the response to a Dynamotable.
This is the code I am using:
rekognition = boto3.client('rekognition', region_name='us-east-1')
dynamodb = boto3.client('dynamodb', region_name='us-east-1')
# --------------- Helper Functions to call Rekognition APIs ------------------
def detect_faces(bucket, key):
response = rekognition.detect_faces(Image={"S3Object": {"Bucket": bucket,
"Name": key}}, Attributes=['ALL'])
TableName = 'table_test'
for face in response['FaceDetails']:
table_response = dynamodb.put_item(TableName=TableName, Item='{0} - {1}%')
return response
My problem is in this line:
for face in response['FaceDetails']:
table_response = dynamodb.put_item(TableName=TableName, Item= {'key:{'S':'value'}, {'S':'Value')
I am able to see the result in the console.
I don't want to add specific item(s) to the table- I need the whole response to be transferred to the table.
Do do this:
1. What to add as a key and partition key in the table?
2. How to transfer the whole response to the table
i have been stuck in this for three days now and can't figure out any result. Please help!
******************* EDIT *******************
I tried this code:
rekognition = boto3.client('rekognition', region_name='us-east-1')
# --------------- Helper Functions to call Rekognition APIs ------------------
def detect_faces(bucket, key):
response = rekognition.detect_faces(Image={"S3Object": {"Bucket": bucket,
"Name": key}}, Attributes=['ALL'])
TableName = 'table_test'
for face in response['FaceDetails']:
face_id = str(uuid.uuid4())
Age = face["AgeRange"]
Gender = face["Gender"]
print('Generating new DynamoDB record, with ID: ' + face_id)
print('Input Age: ' + Age)
print('Input Gender: ' + Gender)
dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table(os.environ['test_table'])
table.put_item(
Item={
'id' : face_id,
'Age' : Age,
'Gender' : Gender
}
)
return response
It gave me two of errors:
1. Error processing object xxx.jpg
2. cannot concatenate 'str' and 'dict' objects
Can you pleaaaaase help!
When you create a Table in DynamoDB, you must specify, at least, a Partition Key. Go to your DynamoDB table and grab your partition key. Once you have it, you can create a new object that contains this partition key with some value on it and the object you want to pass itself. The partition key is always a MUST upon creating a new Item in a DynamoDB table.
Your JSON object should look like this:
{
"myPartitionKey": "myValue",
"attr1": "val1",
"attr2:" "val2"
}
EDIT: After the OP updated his question, here's some new information:
For problem 1)
Are you sure the image you are trying to process is a valid one? If it is a corrupted file Rekognition will fail and throw that error.
For problem 2)
You cannot concatenate a String with a Dictionary in Python. Your Age and Gender variables are dictionaries, not Strings. So you need to access an inner attribute within these dictionaries. They have a 'Value' attribute. I am not a Python developer, but you need to access the Value attribute inside your Gender object. The Age object, however, has 'Low' and 'High' as attributes.
You can see the complete list of attributes in the docs
Hope this helps!

how to fetch data from google plus using api key in python?

**When i send a request like --
f = urllib.urlopen(https://www.googleapis.com/plus/v1/people/103777531434977807649/activities/public?key=*************** )
json=f.read()
print json
it returns some thing like this not the required json
{
"kind": "plus#activityFeed",
"etag": "\"seVFOlIgH91k2i-GrbizYfaw_AM/chWYjTdvKRLG9yxkeAfrCrofGHk\"",
"nextPageToken": "CAIQ__________9_IAAoAA",
"title": "Google+ List of Activities for Collection PUBLIC",
"items": []
}
what i have to do to get the right response????
this is the code:
import json
f = urllib.urlopen('https://www.googleapis.com/plus/v1/people/'+id+'/activities /public?key=*****************&maxResults=100')
s = f.read()
f.close()
ss=json.loads(s)
print ss
try:
nextpagetoken=str(ss['nextPageToken'])
i=0
str_current_datetime=str(datetime.now())
gp_crawldate=str_current_datetime.split(" ")[0]
gp_groupid=id
db = MySQLdb.connect("localhost","root","****","googleplus" )
cursor=db.cursor()
while i<len(ss['items']):
gp_kind=str(ss['items'][i]['kind'])
gp_title=str(ss['items'][i]['title'].encode('utf8'))
gp_published=str(ss['items'][i]['published'][0:10])
check=int(cool(str(ss['items'][i]['published'][0:19])))#this method is defined in the code
gp_activityid=str(ss['items'][i]['id'])
gp_actorid=str(ss['items'][i]['actor']['id'])
gp_verb=str(ss['items'][i]['verb'])
gp_objecttype=str(ss['items'][i]['object']['objectType'])
gp_originalcontent=str(ss['items'][i]['object']['content'].encode('utf8'))
gp_totalreplies=str(ss['items'][i]['object']['replies']['totalItems'])
gp_totalplusone=str(ss['items'][i]['object']['plusoners']['totalItems'])
gp_totalreshare=str(ss['items'][i]['object']['resharers']['totalItems'])
#gp_geocode=str(ss['items'][i]['geocode'])
#gp_placename=str(ss['items'][i]['placeName'])
i=i+1
is the any change in g+api???
The response you posted is a correct response. If the items field is an empty list, then the user that you are fetching the posts for has probably never posted anything publicly. In this case, I confirmed that the user has no public posts simply by visiting their profile.

Categories

Resources