How to fetch users instead of top tweets from twitter via tweepy? - python

I am using tweepy with python (flask) for a small project. I want to fetch users against a query from twitter. Currently I am using cursor object and it returns me tweets. But I want to fetch users instead of tweets.
I think what I am getting right now via cursor is the top results so I want the results in People tab instead of Top tab.
Any idea how should I do that?
Screenshot is attached for what I want!
https://i.stack.imgur.com/0zGUm.png

I found this in tweepy documentation.
After basic twitter auth:
twitterAuth = twitterClient()
api = twitterAuth.get_twitter_client_api()
api.search_users(q, 20, 1)
Here q is your search term, 20 is the number of results per page (max 20 allowed) and 1 indicates the page number from where you want to collect the users. For example if you want to fetch users from 1st page it will be 1 else any other page number.
Hope this helps everyone!

Related

Collect tweets from multiple users at the same time with tweepy

Currently I am working on a project with tweepy to collect new tweets from users very quickly. So far, I have found that the fastest method to collect the newest tweet of a user is like so:
tweets = api.user_timeline(screen_name='user',count=1, include_rts = True,tweet_mode = 'extended')
status = tweets[0]
I was wondering if there is anyway to get the most recent tweets of multiple users in one request? I tried using a streamer, but that ended up having about a 10 second delay between when a tweet was posted and when it popped up, which is way too slow for my application. Please let me know if you have any other ideas on how to fetch tweets quickly.
Thanks
I haven't used tweepy to search the posts by user, but I have used it to extract information on multiple hashtags before.
When I tried with multiple hashtags the code looked like this:
query = "(#nike OR #puma OR #adidas)"
rule = gen_rule_payload(query, results_per_call=100, from_date="2020-11-30", to_date="2020-12-02")
So I would try for you query:
users = "(User1 OR User2 OR User3)"
tweets = api.user_timeline(screen_name=users,count=1, include_rts = True,tweet_mode = 'extended')
status = tweets[0]
I'm not sure if that will work, but fingers crossed! The good thing about working with Twitter is that you can copy paste your search strings directly into the search box on a normal twitter webpage and see if your syntax is correct.

How to get the Facebook Public Page Content Access just to extract data?

For a project at university I need to extract data such as posts and reviews from same Facebook pages. Everything was fine couple of months ago but now to get data from pages you need the Public Page Content Access.
In order to get my app reviewed I need to add:
A platform where I'd use the app
A screencast that shows "how a person sees this feature used in your app"
An explanation of how I'd be using Page Public Content Access to enhance the experience of my app.
Privacy Policy URL
As a student who just needs to extract some data for an exam I don't have any website/platform where I'd use the app. I'm using the Facebook Graph API on Python.
I looked on this website for a Privacy Policy Generator but I don't have any website nor mobile apps where I'd use the API...
Is there some way for my situation to extract data by API without this requirements or it's better for me to find other solutions, such as web scraping?
To be able to extract data from Facebook using a python code you need to register as a developer on Facebook and then have an access token. Here are the steps for it.
Go to link developers.facebook.com, create an account there. Go to
link developers.facebook.com/tools/explorer. Go to “My apps” drop
down in the top right corner and select “add a new app”. Choose a
display name and a category and then “Create App ID”. Again get back
to the same link developers.facebook.com/tools/explorer. You will see
“Graph API Explorer” below “My Apps” in the top right corner. From
“Graph API Explorer” drop down, select your app. Then, select “Get
Token”. From this drop down, select “Get User Access Token”. Select
permissions from the menu that appears and then select “Get Access
Token.” Go to link developers.facebook.com/tools/accesstoken. Select
“Debug” corresponding to “User Token”. Go to “Extend Token Access”.
This will ensure that your token does not expire every two hours.
Python Code to Access Facebook Public Data:
Go to link https://developers.facebook.com/docs/graph-api if want to collect data on anything that is available publicly. See https://developers.facebook.com/docs/graph-api/reference/v2.7/. From this documentation, choose any field you want from which you want to extract data such as “groups” or “pages” etc. Go to examples of codes after having selected these and then select “facebook graph api” and you will get hints on how to extract information. This blog is primarily on getting events data.
First of all, import ‘urllib3’, ‘facebook’, ‘requests’ if they are already available. If not, download these libraries. Define a variable token and set its value to what you got above as “User Access Token”.
token= ‘aiufniqaefncqiuhfencioaeusKJBNfljabicnlkjshniuwnscslkjjndfi’
Getting list of Events:
Now to find information on events for any search term say “Poetry” and limiting those events’ number to 10000:
graph = facebook.GraphAPI(access_token=token, version = 2.7)
events = graph.request(‘/search?q=Poetry&type=event&limit=10000’)
This will give a dictionary of all the events that have been created on Facebook and has string “Poetry” in its name. To get the list of events, do:
eventList = events[‘data’]
Extracting all information for a event from the list of events extracted above:
Get the EventID of the first event in the list by
eventid = eventList[1][‘id’]
For this EventID, get all information and set few variables which will be used later by:
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’]
Getting the list of all those who are attending an event and converting the response into json format:
attenders = requests.get(“https://graph.facebook.com/v2.7/"+eventid+"/attending?
access_token="+token+”&limit=”+str(attenderscount))
attenders_json = attenders.json()
Getting the admins of the event:
admins = requests.get(“https://graph.facebook.com/v2.7/"+eventid+"/admins?
access_token="+token)
admins_json = admins.json()
And similarly you can extract other information such as photos/videos/feed of that event if you want.
Go to https://developers.facebook.com/docs/graph-api/reference/event/ and see “Edges” part in the documentation.

Twitter API - Python , searching for tweet by date

# Initiate the connection to Twitter
twitter = Twitter(auth=oauth)
# Search for latest tweets about "pakistan"
results = twitter.search.tweets(q='pakistan',until=2008 - 08 - 19, )
print results
I am trying to retrieve tweets that are earlier than this date by one week. It does not return anything. However, I have searched manually on twitter and found that tweets exist.
When you use the Twitter API to download tweets you will have access to tweets back to roughly one week old. This is despite the fact that you can see tweets older than one week on Twitter's website. This is a built-in limitation of the API.
To have access to a bigger time span you can do the following ways:
download everyday data and add up gradually.
you can search on the web to find a dataset
The best way is to ask Twitter to give you the data for a specific time span while you have an API developer account. You have asked for a quote using this address:
https://www.trackmyhashtag.com/twitter-dataset#request-data-form

facebook graph api {page}/links only returns for 60 days

I'm managing a Facebook page in which I'm also analyzing it's insights. We own the page and every post on the page feed(page doesn't allow other users to post). I'm doing an analysis on all of the posts that we've every created.
I've been using {page}/posts edge to get the post ids but found out that it only returns a subset of the data. Then I tried {page}/links and {page}/videos because these are the post types I'm mostly interested in. The video edge works great; it gave me all of the videos ids from the page. However, {page}/links only returned 2 months worth of link ids.
Here is a sample GET I'm using (I'm trying to get the post ids from 10/2014 to 12/2014):
https://graph.facebook.com/v2.2/{actual_page_id}/links?fields=id,created_time&since=1414175236&until=1419445636&access_token=[The_actual_access_token]
But I get an empty result string:
{"data": []}
And when I set the date with in the 2 months frame I can get proper response.
My question is: Is there a way to get ALL of the Facebook page posts ids that we have created? I've tried to set limits and paging but none have worked. Thank you very much for your help.
The Below snippet should solve your issue, It uses Facepy and handles paging on its own.
from facepy import GraphAPI
import json
access = '<access_token>'
graph = GraphAPI(access)
page_id= '<page_id>'
data= graph.get(page_id+ "/posts?fields=id", page=True, retry=5)
data1=[]
for i in data:
data1.append(i)
print data1

Facebook API - Using 'limit' property to change paging limit in Python

I'm using Python to access Facebook pages and retrieve information. When I try to retrieve the list of users who likes an item (ex: a post, a picture), i get only a partial list. I think this is due to paging limit. I know there is a way to change the paging limit in Facebook Graph:
https://graph.facebook.com/[node]/likes?limit=1000
but I do not know how to implement the 'limit' property in Python code.
The code I use to gather the likes is:
import facebook
graph = facebook.GraphAPI(token)
profile = graph.get_object(page_id)
likes = str(profile.get('likes'))
the above will get you the number of likes to the particular page. You can't get the list of fans of particular page. However you can check the if a particular user likes a page .
check the following page for all other common things that can be done with facebook graph api
https://developers.facebook.com/docs/graph-api/common-scenarios

Categories