how to fetch data from google plus using api key in python? - 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.

Related

For loop not iterating in Mailchimp

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.

Issue with reading from json file

I'm working with project and I'm trying to write from json file. Unfortunately I found a problem. When I tried to read it I dont' get everything the same as this in my file pliki.json
pliki.json:
[
{
"name": "",
"pesel": "",
"choosendir": "Amsterdam-Berlin",
"lot id": "1",
"bilet class": "Biznes ",
"bilet_price": " 68",
"seat": "5"
}
]
Function reading:
import csv
from lot import DatabaseofLoty, Lot
import json
from person import Person, Database
from ticket import Ticket
def read_info_aboutpeople(path):
with open(path, "r") as file_handle:
people = []
try:
rowing = json.load(file_handle)
for row in rowing:
name =row["name"],
print(name)
pesel = row["pesel"],
print(pesel)
choosendir = row["choosendir"]
print(choosendir)
lot_id = row["lot id"],
print(lot_id)
bilet_class = row["bilet class"],
bilet_price = row["bilet_price"],
seat = row["seat"]
ticket = Ticket(bilet_class,bilet_price)
person = Person(name, pesel,ticket, choosendir, lot_id, seat)
people.append(person)
database = Database(people)
return database
except Exception:
database = Database([])
print(read_info_aboutpeople("pliki.json"))
Outputs:
('',)
('',)
Amsterdam-Berlin
('1',)
Why this works like that? How to solve this
Actually json reading works, but look what exactly is happening here:
name = row["name"],
print(type(name)) # <class 'tuple'>
So in real you create one element tuple here with name as it's only element. That's why you see strange print results as most of your variables are just tuples (and that's why your code probably blows up when you use lot_id as you expect that as single str). You need to remove commas to make code work as you expect

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)

Python microsoft graph api

I am using microsoft graph api to pull my emails in python and return them as a json object. There is a limitation that it only returns 12 emails. The code is:
def get_calendar_events(token):
graph_client = OAuth2Session(token=token)
# Configure query parameters to
# modify the results
query_params = {
#'$select': 'subject,organizer,start,end,location',
#'$orderby': 'createdDateTime DESC'
'$select': 'sender, subject',
'$skip': 0,
'$count': 'true'
}
# Send GET to /me/events
events = graph_client.get('{0}/me/messages'.format(graph_url), params=query_params)
events = events.json()
# Return the JSON result
return events
The response I get are twelve emails with subject and sender, and total count of my email.
Now I want iterate over emails changing the skip in query_params to get the next 12. Any method of how to iterate it using loops or recursion.
I'm thinking something along the lines of this:
def get_calendar_events(token):
graph_client = OAuth2Session(token=token)
# Configure query parameters to
# modify the results
json_list = []
ct = 0
while True:
query_params = {
#'$select': 'subject,organizer,start,end,location',
#'$orderby': 'createdDateTime DESC'
'$select': 'sender, subject',
'$skip': ct,
'$count': 'true'
}
# Send GET to /me/events
events = graph_client.get('{0}/me/messages'.format(graph_url), params=query_params)
events = events.json()
json_list.append(events)
ct += 12
# Return the JSON result
return json_list
May require some tweaking but essentially you're adding 12 to the offset each time as long as it doesn't return an error. Then it appends the json to a list and returns that.
If you know how many emails you have, you could also batch it that way.

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'

Categories

Resources