multithread streamming with tweepy - multithreading

Is there a way to implement a multi-thread streamming in twitter using tweepy?
Currently I am using the following code:
import tweepy
auth = tweepy.OAuthHandler(TWITTER_APP_KEY, TWITTER_APP_SECRET)
auth.set_access_token(TWITTER_KEY, TWITTER_SECRET)
api = tweepy.API(auth)
stream_listener = tweepy.StreamListener()
stream = tweepy.Stream(auth=api.auth, listener=stream_listener)
stream.filter(track=["token"], async=True)
stream.filter(track=settings.TRACK_TERMS)

Related

Which API key to use for IBM Speech-to-Text with IAMAuthenticator? (Python)

How do I set this up correctly? The description seems straight-forward
import os
import json
from ibm_watson import SpeechToTextV1
from ibm_cloud_sdk_core.authenticators import IAMAuthenticator
api_key='XYZ'
service_endpoint='https://api.eu-gb.speech-to-text.watson.cloud.ibm.com'
authenticator = IAMAuthenticator(api_key)
speech2text = SpeechToTextV1(authenticator=authenticator)
speech_to_text.set_service_url(service_endpoint)
speech_models = speech_to_text.list_models().get_result()
print(json.dumps(speech_models, indent=2))
Which API key do I have to pass to the 'IAMAuthenticator'?
I don't quite understand if I first have to create an Instance/Resource (https://cloud.ibm.com/catalog/services/speech-to-text) and use that API key, or only create and use an IBM Cloud API key (https://cloud.ibm.com/iam/apikeys). I have tried different combinations of the above, but cannot connect successfully, raising ApiException: Error: Provided API key could not be found, Code: 400
Actually, you use IAMAuthenticator with the API key. Don't forget to import it.
See the example in the API reference and more information about the SDK in the README. Can you make a call with curl with the same API key and URL
(The BasicAuthenticator is specifically for username and password auth.)
To use the APIKEY directly you must use the BasicAuthenticator instead of the IAMAuthenticator from ibm_cloud_sdk_core.authenticators. See the code snippet below:
from ibm_watson import SpeechToTextV1, ApiException
from ibm_cloud_sdk_core.authenticators import BasicAuthenticator
...
# Basic Authentication with Watson STT API
stt_authenticator = BasicAuthenticator(
'apikey',
'<insert_your_watson_stt_apikey_here>'
)
# Construct a Watson STT client with the authentication object
stt = SpeechToTextV1(
authenticator=stt_authenticator
)
# Set the URL endpoint for your Watson STT client
stt.set_service_url(
'<insert_your_watson_stt_endpoint_url_here>'
)
# Read audio file and call Watson STT API:
with open(
os.path.join(
os.path.dirname(__file__), './.',
'audio_sample.flac'
), 'rb'
) as audio_file:
# Transcribe the audio.flac with Watson STT
# Recognize method API reference:
# https://cloud.ibm.com/apidocs/speech-to-text?code=python#recognize
stt_result = stt.recognize(
audio=audio_file,
content_type='audio/flac',
model='pt-BR_BroadbandModel'
).get_result()
# Print STT API call results
print(json.dumps(stt_result, indent=2))
...
I hope this answer helps you.

Getting status code 403 when working with Tweepy search API

I am trying to get the tweets in a 10 miles radius around Twitter's headquarters in San Francisco location using Tweepy API. I have authenitication information (api key, etc.) in an external .json file.
Below is my code:
import tweepy as tw
import json
import pandas as pd
import datetime
secrets = json.loads(open('secrets.json').read())
api_key = secrets['api_key']
api_secret_key = secrets['api_secret_key']
access_token = secrets['access_token']
access_token_secret = secrets['access_token_secret']
auth = tw.OAuthHandler(api_key, api_secret_key)
auth.set_access_token(access_token, access_token_secret)
api = tw.API(auth)
try:
api.verify_credentials()
print('Authenticated')
except:
print("Error authenticating")
# After running the above, I get status 'Authenitcated'
start = '2020-09-01'
end = '2020-09-26'
location = '37.7749, 122.4149, 10miles'
query = "*"
max_tweets = 1000
tweet_search_results = [status for status in tw.Cursor(api.search, query=query, geocode=location, since=start, until=end).items(max_tweets)]
After running the above, I get this error:
TweepError: Twitter error response: status code = 403
This status code means access to the requested resource is forbidden for some reason. The server understood the request, but will not fulfill due to the client-related issues.
Within the same code I am able to get the list of tweets for a specific user name, but without geolocation data.
Why am I getting 403 when trying to get geolocation tweets?

How to specify an Twitter endpoint using Tweepy

I'm using tweepy trying to get history tweets from a specific user, for example "IBM"
In TwitterAPI I can use
api = TwitterAPI(consumer_key, consumer_secret, access_token_key, access_token_secret)
end_point = 'tweets/search/30day/:development'
param = {'query': 'from:IBM', 'maxResults':'500'}
r = TwitterPager(api, end_point, param)
To specify the end point. I'm wondering how I can do similar in Tweepy.
You cannot do this with Tweepy. tweets/search/30day is a Premium Search endpoint. Tweepy does not support the Premium Search API.

How to interact with the Gmail API - Delegate User - Python 3.7

I am trying to write a Python application which simply adds a user as a delegate to another users mailbox.
I am following the API # Google API Documentation - Users.settings.delegates: create
However, I am struggling to find how to the parameters of:
User - an account which is TOBE added to a delegate Mailbox
Mailbox - the account which has the Mailbox I wish the account to become a delegate of.
I have currently tried making an API which has the delegate user. However, it does not seem to be interacting how I would expect. I am hoping Google will create a responsive API for the browser to support this. However, I am struggling with the code:
from googleapiclient import discovery
from oauth2client.service_account import ServiceAccountCredentials
def main(user_to_be_added, delegated_mailbox):
service_account_credentials = ServiceAccountCredentials.from_json_keyfile_name('credentials/service_account.json')
service_account_credentials = service_account_credentials.create_scoped('https://mail.google.com/ https://www.googleapis.com/auth/gmail.insert https://www.googleapis.com/auth/gmail.modify')
service_account_credentials = service_account_credentials.create_delegated(user_to_be_added)
service = discovery.build('gmail', 'v1', credentials=service_account_credentials)
response = service.users().settings().delegates().create().execute(userId=delegated_mailbox)
if __name__ == '__main__':
main('some_account_to_be_added#gmail.com', 'delegated_mailbox#gmail.com')
Am I interacting with this API completely wrong? If so, how has anyone else achieved this?
Thank you for your time.
Jordan
Working Solution:
from googleapiclient import discovery
from google.oauth2 import service_account
def _create_client(subject):
credentials = service_account.Credentials
credentials = credentials.from_service_account_file('credentials/service_account.json',
scopes=['https://www.googleapis.com/auth/gmail.settings.sharing',
'https://www.googleapis.com/auth/gmail.settings.basic'],
subject=subject)
service = discovery.build('gmail', 'v1', credentials=credentials)
return service
def add_delegate_to_email(user_to_be_added, delegated_mailbox):
service = _create_client(user_to_be_added)
body = {
"delegateEmail": delegated_mailbox,
"verificationStatus": "accepted"
}
try:
response = service.users().settings().delegates().create(userId='me', body=body).execute()
print(response)
except Exception as e:
print('Exception: {}'.format(e))
Main problem: from oauth2client.service_account import ServiceAccountCredentials is deprecated as Google took ownership with google-auth.

How to get cloud speech api response in python if I only have the operation name?

I am sending a long speech recognition file for speech recognition.
operation = client.long_running_recognize(config, audio)
operation_name = operation._operation.name
There is another file where I have to use operation_name (returned by google speech API) to get back the response again.
ref: Python way of polling longrunning operations from operation name in Google Cloud?
I tried get_operation method of the "Long-Running Operations Client":
from google.api_core import operations_v1
api = operations_v1.OperationsClient()
name = ...
response = api.get_operation(name)
but I get the following error for the line:
api = operations_v1.OperationsClient()
TypeError: init() missing 1 required positional argument: 'channel'
Got the answer on Github issues:
from google.cloud import speech
client = speech.SpeechClient()
from google.api_core import operations_v1
api = operations_v1.OperationsClient(client.transport.channel)
name = ...
response = api.get_operation(name)
reference : Github

Resources