Twitter data extraction by user using Twitter API - python-3.x

How can i get user tweets using tweepy , but with user name not screen name, i have a list for company employees and i want to get their tweets, is there any solution ?
``` config.read('config.ini')
api_key = config['twitter']['api_key']
api_key_secret = config['twitter']['api_key_secret']
access_token = config['twitter']['access_token']
access_token_secret = config['twitter']['access_token_secret']
# authentication
auth = tweepy.OAuthHandler(api_key, api_key_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
#users = df['Name'].tolist()
user_ids = []
limit=300
for user in users:
tweets = tweepy.Cursor(api.user_timeline, screen_name=user, count=200,
tweet_mode='extended').items(limit)
#tweets = api.user_timeline(screen_name=user, count=limit,
tweet_mode='extended')
# create DataFrame
columns = ['User', 'Tweet']
data = []
for tweet in tweets:
data.append([tweet.user.screen_name, tweet.full_text])
df1 = pd.DataFrame(data, columns=columns)
print(df1)```

Related

How to retrieve the user's name and email address from a Google OAuth callback?

I am trying to retrieve the signed in user's name and email address once they have signed in via Google. Here is my code:
#app.route("/callback")
def callback():
flow.fetch_token(authorization_response=request.url)
if not session["state"] == request.args["state"]:
abort(500) # State does not match!
credentials = flow.credentials
request_session = requests.session()
cached_session = cachecontrol.CacheControl(request_session)
token_request = google.auth.transport.requests.Request(session=cached_session)
id_info = id_token.verify_oauth2_token(
id_token=credentials._id_token,
request=token_request,
audience=oauth_client_id
)
session["google_id"] = id_info.get("sub")
session["name"] = id_info.get("name")
session["email"] = id_info.get("email")
return redirect("/")
Nothing displays when I store session["name"] = id_info.get("name") or session["email"] = id_info.get("email") in a variable and print this to console?

Can I use urlencode in python to insert a string of characters into this Spotify API request?

I am working with Spotify API to request song data through a URL. I imported urlencode to run the song's ID as a function parameter into the url. Essentially, I need the ID portion of the url request to be the ID by itself, not "id=<id string>"
I tried assigning the id string to a parameter of my 'search' function. The search function takes a user input song ID from spotify, inserts it into the proper position in the URL request, and sends it to the spotify database to retrieve that songs data analysis. The program successfully sends out the request, but the id portion I am filling in puts "id=<song ID>" instead of the song ID by itself.
import requests
import datetime
from urllib.parse import urlencode
# In[3]:
import base64
# In[4]:
client_id = 'fb5af83351d4402fa82904fc04f7fc9e'
client_secret = 'b5057eb39b024180b61b02eb45fb97a6'
# In[5]:
class SpotifyAPI(object):
access_token = None
access_token_expires = datetime.datetime.now()
access_token_did_expire = True
client_id = None
client_secret = None
token_url = "https://accounts.spotify.com/api/token"
def __init__(self, client_id, client_secret, *args, **kwargs):
super().__init__(*args, **kwargs)
self.client_id = client_id
self.client_secret = client_secret
def get_client_credentials(self):
client_id = self.client_id
client_secret = self.client_secret
if client_secret == None or client_id == None:
raise Exception("You must set client_id and client_secret")
client_creds = f"{client_id}:{client_secret}"
client_creds_b64 = base64.b64encode(client_creds.encode())
return client_creds_b64.decode()
def get_token_headers(self):
client_creds_b64 = self.get_client_credentials()
return {
"Authorization": f"Basic {client_creds_b64}"
}
def get_token_data(self):
return {
"grant_type": "client_credentials"
}
def perform_authorization(self):
token_url = self.token_url
token_data = self.get_token_data()
token_headers = self.get_token_headers()
r = requests.post(token_url, data=token_data, headers=token_headers)
print(r.json())
if r.status_code not in range(200,299):
raise Exception("Could not authenticate client")
#return False
data = r.json()
now = datetime.datetime.now()
access_token = data['access_token']
expires_in = data['expires_in'] #seconds
expires = now + datetime.timedelta(seconds=expires_in)
self.access_token = access_token
self.access_token_expires = expires
self.access_token_did_expire = expires < now
return True
def get_access_token(self):
token = self.access_token
expires = self.access_token_expires
now = datetime.datetime.now()
if expires < now:
self.perform_authorization()
return self.get_access_token()
elif token == None:
self.perform_authorization()
return self.get_access_token()
return token
def search(self, id):
access_token = self.get_access_token()
headers = {
"Authorization": f"Bearer {access_token}"
}
end_point = "https://api.spotify.com/v1/audio-analysis/"
data = urlencode({"id":id})
print(data)
lookup_url = f"{end_point}{data}"
print(lookup_url)
r = requests.get(lookup_url, headers = headers)
if r.status_code not in range(200, 299):
return r
return r.json()
it returns
{'access_token': 'BQCLoKT_b2PF7KPSbscosa1dCpE5rzd_RBkswOvwklVdlAL4AeEGCDn0iYuqac5o86BTqCIz0m95u3olLp4', 'token_type': 'Bearer', 'expires_in': 3600}
id=1UGD3lW3tDmgZfAVDh6w7r
https://api.spotify.com/v1/audio-analysis/id=1UGD3lW3tDmgZfAVDh6w7r

Retrieving Facebook Lead Data

I tryed to use this code to retrieving Facebook Lead Data
FacebookAdsApi.init(app_id, app_secret, access_token)
me = AdUser(fbid='me')
my_accounts = list(me.get_ad_accounts(fields=['name']))
my_account = my_accounts[3]
ads = my_account.get_ads(params=params)
for ad in ads[10:20]:
print(ad.get_leads())
But I get an empty response {"data": []} for each ad.
In ADS Manager, I see that there are leads.
App permissions:
pages_show_list,
ads_management,
leads_retrieval,
pages_read_engagement,
pages_read_user_content,
pages_manage_ads.
Second try:
params = {
'access_token':access_token
}
adgroup_id = ad_id
if access_token:
url = 'https://graph.facebook.com/v12.0/%s/leads' %adgroup_id
resp = session.get(url=url, params=params)
print(resp.text)
Annswer: {"data":[]}
Please tell me what needs to be added to get the correct result?

youtube API get all playlist id from a channel : python

I'm trying to have a list of all playlists from a specific channel in youtube API ... in python
I'd like to have a list of playlist_id in a array
all_playlist_item = []
if I launch
https://www.googleapis.com/youtube/v3/playlists?part=snippet&channelId=UC1udnO-W6gpR9qzleJ5SDKw&key=xxxxxx
here's the response from this request
{
"kind": "youtube#playlistListResponse",
"etag": "PMwo-9BIp7p_L2ynH9sFOGIOido",
"nextPageToken": "CAUQAA",
"pageInfo": {
"totalResults": 17,
"resultsPerPage": 5
},
"items": [
{
"kind": "youtube#playlist",
"etag": "-V67IpyB9a1JGDGJ4pVQnEoMRy4",
"id": "PLWi7PxnyAMeN1tb-ldDzJcnJy2yd5wYrO",
"snippet": {
...
I thing i have to used nextPageToken, but I don't know how to code this function for playlist items
Here's my code (extract in excel all videos just from a specific playlist)
channel_name="UC1udnO-W6gpR9qzleJ5SDKw"
playlist_name="PLWi7PxnyAMeOJmVLv8Z_N3edNyipsnHbo"
api_key = "xxxxxx"
from apiclient.discovery import build
import pandas as pd
youtube = build('youtube', 'v3', developerKey=api_key)
def get_channel_videos(channel_id):
# get Uploads playlist id
res = youtube.channels().list(id=channel_id,
part='contentDetails').execute()
playlist_id = res['items'][0]['contentDetails']['relatedPlaylists']['uploads']
videos = []
next_page_token = None
while 1:
res = youtube.playlistItems().list(playlistId=playlist_name,
part='snippet',
maxResults=50,
pageToken=next_page_token).execute()
videos += res['items']
next_page_token = res.get('nextPageToken')
if next_page_token is None:
break
return videos
videos = get_channel_videos(channel_name)
def get_videos_stats(video_ids):
stats = []
for i in range(0, len(video_ids), 50):
res = youtube.videos().list(id=','.join(video_ids[i:i+50]),
part='statistics').execute()
stats += res['items']
return stats
video_ids = list(map(lambda x:x['snippet']['resourceId']['videoId'], videos))
stats = get_videos_stats(video_ids)
d = []
if len(stats)!=len(videos):
i=1
j=0
else:
i=0
j=0
len_video = len(videos)
len_stats = len(stats)
for video in videos:
if i >= len_video:
break
Url_video='https://www.youtube.com/watch?v='+videos[i]['snippet']['resourceId']['videoId']+'&list='+playlist_name
d.append((videos[i]['snippet']['title'],
videos[i]['snippet']['resourceId']['videoId'],
Url_video,
stats[j]['statistics']['viewCount'],
stats[j]['statistics']['likeCount'],
stats[j]['statistics']['dislikeCount']
))
i+=1
j+=1
df=pd.DataFrame(d, columns=('Titre_video', 'ID_video', 'Url_video','vues','like','dislike'))
df['vues'] = df['vues'].astype(int)
df['like'] = df['like'].astype(int)
df['dislike'] = df['dislike'].astype(int)
df.index+=1
df.to_excel("youtube-playlist.xlsx")
From the pagination doc, you can use :
youtube.playlistItems().list_next(request, response) for iterating playlist item response
youtube.playlists().list_next(request, response) for iterating channel response
Get all videos from Playlist
import googleapiclient.discovery
playlist_id = "PLWi7PxnyAMeOJmVLv8Z_N3edNyipsnHbo"
youtube = googleapiclient.discovery.build("youtube", "v3", developerKey = "YOUR_API_KEY")
request = youtube.playlistItems().list(
part = "snippet",
playlistId = playlist_id,
maxResults = 50
)
response = request.execute()
playlist_items = []
while request is not None:
response = request.execute()
playlist_items += response["items"]
request = youtube.playlistItems().list_next(request, response)
print(f"total: {len(playlist_items)}")
print(playlist_items)
Get all playlists from channel
import googleapiclient.discovery
channel_id = "UC1udnO-W6gpR9qzleJ5SDKw"
youtube = googleapiclient.discovery.build("youtube", "v3", developerKey = "YOUR_API_KEY")
request = youtube.playlists().list(
part = "snippet",
channelId = channel_id,
maxResults = 50
)
response = request.execute()
playlists = []
while request is not None:
response = request.execute()
playlists += response["items"]
request = youtube.playlists().list_next(request, response)
print(f"total: {len(playlists)}")
print(playlists)

Unable to convert tweets to JSON format and then save as text file

I am trying to create a json object which I then want to save an a text file where each tweet's JSON data representing its own line. This is what I have but its not working:
#Connect to Twitter API:
import tweepy
consumer_key = 'xx'
consumer_secret = 'xx'
access_token = 'xxx'
access_secret = 'xx'
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_secret)
api = tweepy.API(auth)
tweets=[]
#pull first entry only for testing
for i in df_image_pred.tweet_id[0:1]:
t=api.get_status(i, tweet_mode='extended',wait_on_rate_limit=True,wait_on_rate_limit_notify=True)
tweets.append(t._json)
#save as text file
f = open("dict.txt","w")
f.write( str(tweets) )
f.close()
The output is this:
When read the text file in pandas, this is my output in a dataframe which is the issue here:

Resources