Facepy events data, description is missing - python

I want to use Python Facepy to fetch the events from our company Facebook site.The strange thing is that everything works fine, except the event description is missing in returned data:
from facepy import GraphAPI
graph = GraphAPI("mysecrettoken")
events = graph.get('ourcompany/events')
for x in events['data']:
print x['description']
KeyError: 'description'
Except description, all data is there (name, start_time) etc etc

The event description isn't provided by the Graph api. I have to fetch it seperately for every event.

Related

How to get twitter handle from tweet using Tweepy API 2.0

I am using the Twitter API StreamingClient using the python module Tweepy. I am currently doing a short stream where I am collecting tweets and saving the entire ID and text from the tweet inside of a json object and writing it to a file.
My goal is to be able to collect the Twitter handle from each specific tweet and save it to a json file (preferably print it in the output terminal as well).
This is what the current code looks like:
KEY_FILE = './keys/bearer_token'
DURATION = 10
def on_data(json_data):
json_obj = json.loads(json_data.decode())
#print('Received tweet:', json_obj)
print(f'Tweet Screen Name: {json_obj.user.screen_name}')
with open('./collected_tweets/tweets.json', 'a') as out:
json.dump(json_obj, out)
bearer_token = open(KEY_FILE).read().strip()
streaming_client = tweepy.StreamingClient(bearer_token)
streaming_client.on_data = on_data
streaming_client.sample(threaded=True)
time.sleep(DURATION)
streaming_client.disconnect()
And I have no idea how to do this, the only thing I found is that someone did this:
json_obj.user.screen_name
However, this did not work at all, and I am completely stuck.
So a couple of things
Firstly, I'd recommend using on_response rather than on_data because StreamClient already defines a on_data function to parse the json. (Then it will fire on_tweet, on_response, on_error, etc)
Secondly, json_obj.user.screen_name is part of API v1 I believe, which is why it doesn't work.
To get extra data using Twitter Apiv2, you'll want to use Expansions and Fields (Tweepy Documentation, Twitter Documentation)
For your case, you'll probably want to use "username" which is under the user_fields.
def on_response(response:tweepy.StreamResponse):
tweet:tweepy.Tweet = response.data
users:list = response.includes.get("users")
# response.includes is a dictionary representing all the fields (user_fields, media_fields, etc)
# response.includes["users"] is a list of `tweepy.User`
# the first user in the list is the author (at least from what I've tested)
# the rest of the users in that list are anyone who is mentioned in the tweet
author_username = users and users[0].username
print(tweet.text, author_username)
streaming_client = tweepy.StreamingClient(bearer_token)
streaming_client.on_response = on_response
streaming_client.sample(threaded=True, user_fields = ["id", "name", "username"]) # using user fields
time.sleep(DURATION)
streaming_client.disconnect()
Hope this helped.
also tweepy documentation definitely needs more examples for api v2
KEY_FILE = './keys/bearer_token'
DURATION = 10
def on_data(json_data):
json_obj = json.loads(json_data.decode())
print('Received tweet:', json_obj)
with open('./collected_tweets/tweets.json', 'a') as out:
json.dump(json_obj, out)
bearer_token = open(KEY_FILE).read().strip()
streaming_client = tweepy.StreamingClient(bearer_token)
streaming_client.on_data = on_data
streaming_client.on_closed = on_finish
streaming_client.sample(threaded=True, expansions="author_id", user_fields="username", tweet_fields="created_at")
time.sleep(DURATION)
streaming_client.disconnect()

How to get twitter trends using python & tweepy?

I want to develop a Jupyter Notebook which on executing every time shows the top 10 Twitter trending topics in India in the last 24 hours.
I got everything set up
auth = tweepy.OAuthHandler(apikey,apisecretkey)
auth.set_access_token(accesskey,accesssecret)
api = tweepy.API(auth)
and when I run trends1 = api.trends_place(23424848), it gives a AttributeError: 'API' object has no attribute 'trends_place'
And if this attribute has been removed then what should I do to get my work done Please help..
You are getting this error because api do not have this trends_place attribute.
If u check the documentation (https://docs.tweepy.org/en/stable/api.html#trends), you will see that instead of using api.trends_place(), the correct syntax is api. followed by the attribute get_place_trends().
So, i suggest the following code to get the desired result:
auth = tweepy.OAuthHandler(apikey,apisecretkey)
auth.set_access_token(accesskey,accesssecret)
api = tweepy.API(auth)
WOEID = 23424848
top_trends = api.get_place_trends(WOEID)
Note that top_trends is an dictionary inside a list of length 1. Treat it like top_trends[0]['trends'] and so on to get specific values. Example:
top_trends[0]['trends'][0]['name']
top_trends[0]['trends'][0]['url']
top_trends[0]['trends'][0]['promoted_content']
top_trends[0]['trends'][0]['query']
top_trends[0]['trends'][0]['tweet_volume']
To get trending topics near a specific location on Twitter,
Once you've set up:
auth = tweepy.OAuthHandler(apikey,apisecretkey)
auth.set_access_token(accesskey,accesssecret)
api = tweepy.API(auth)
use trends = api.trends_place(WOEID) to get the 50 trending topics based on the lWOEID

Get attendees email from Google Calendar API - Python

I'm trying to extract the emails from all Google Calendar events. I've been following other links (Google Calendar API how to find attendee list) but I'm getting an error
attendees = event['attendees'].get('email', event['attendees'].get('email'))
AttributeError: 'list' object has no attribute 'get'
This is the code
for event in events:
start = event['start'].get('dateTime', event['start'].get('date'))
end = event['end'].get('dateTime', event['end'].get('date'))
attendees = event['attendees'].get('email', event['attendees'].get('email'))
print(attendees)
You can't call get() on a list which I'm guessing event['attendees'] is. You have multiple way of handling this. You can loop through the event['attendees'] and get the email from each individual attendee. You could also use map() to do the same thing
forEach Loop example:
event['attendees'].forEach(attendee => console.log(attendee.get('email')))
map example:
const attendees = event['attendees'].map(attendee => attendee.get('email'))

Can't get location or name from Tweepy

So I have this code (obviously purposely hiding the tokens)
import tweepy
consumerkey=''
consumerkeysecret=''
bearer=''
access=''
access_secret=''
auth = tweepy.AppAuthHandler(consumerkey, consumerkeysecret)
api = tweepy.API(auth,wait_on_rate_limit=True,wait_on_rate_limit_notify=True)
tweet='1314271994054152192'
status = api.get_status(tweet,tweet_mode='extended')
print(status.screen_name)
print(status.location)
I'm learning Tweepy and so I used a tweet from Obama to test the features of the API. For whatever reason, I can't get the screen name or location for the tweet, as it says 'Status' object has no attribute 'location'. When I look at the json from printing out all the information about the tweet, both 'location' and 'screen_name' are in there and have text associated with it. How can I get this information?
You need to access the User object inside the Status object:
print(status.user.screen_name)
print(status.user.location)

Is there a way to access facebook events using graphapi

I have code to look up events but keep getting an error:
File "facebook_events.py", line 8, in
events = graph.request('/search?q=Poetry&type=event')
File "/Users/teomeyerhoff/Desktop/projects/jakecongress/facebookenv/lib/python3.7/site-packages/facebook/init.py", line 313, in request
raise GraphAPIError(result)
facebook.GraphAPIError: (#33) This object does not exist or does not support this action.
Has something changed in Facebooks api. It looks as though I can no longer access events using the query string: '/search?q=Poetry&type=event' as a graph request.
import urllib3
import facebook
import requests
token = "EA......" //not actual token
graph = facebook.GraphAPI(access_token=token, version = "2.8")
events = graph.request('/search?q=Poetry&type=event')
print(events)
eventList = events['data']
eventid = eventList[1]['id']
event1 = graph.get_object(id=eventid, fields='attending_count, can_guests_invite, \
category, cover, declined_count, description, \
end_time, guest_list_enabled, interested_count, \
is_canceled, is_page_owned, is_viewer_admin, \
maybe_count, noreply_count, owner, parent_group,\
place, ticket_uri, timezone, type, updated_time')
attenderscount = event1['attending_count']
declinerscount = event1['declined_count']
interestedcount = event1['interested_count']
maybecount = event1['maybe_count']
noreplycount = event1['noreply_count']
attenders = requests.get('https://graph.facebook.com/v2.8/"+eventid+"\
/attending?access_token="+token+"&limit='+str(attenderscount))
attenders_json = attenders.json()
admins = requests.get("https://graph.facebook.com/v2.8/"+eventid+"\
/admins?access_token="+token)
admins_json = admins.json()
Thank you for the help.
It looks as though I can no longer access events using the query string: '/search?q=Poetry&type=event' as a graph request.
Yes, that is the case.
https://developers.facebook.com/docs/graph-api/changelog/breaking-changes#search-4-4
Search API
You can no longer use the /search endpoint with the following object types:
event
group
page
user
Also mentioned in the accompanying blog post, https://developers.facebook.com/blog/post/2018/04/04/facebook-api-platform-product-changes/
Search API
Deprecated:
Support for finding pages, groups, events, users using search.

Categories

Resources