How to get twitter trends using python & tweepy? - python

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

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()

Can CampaignPerformanceReportRequest return for all campaigns?

Trying to use the Bing Ads API to duplicate what I see on the Hourly report.
Unfortunately, even though I'm properly authenticated, the data I'm getting back is only for One Campaign (one which has like 1 impression per day). I can see the data in the UI just fine, but authenticated as the same user via the API, I can only seem to get back the smaller data set. I'm using https://github.com/BingAds/BingAds-Python-SDK and basing my code on the example:
def get_hourly_report(
account_id,
report_file_format,
return_only_complete_data,
time):
report_request = reporting_service.factory.create('CampaignPerformanceReportRequest')
report_request.Aggregation = 'Hourly'
report_request.Format = report_file_format
report_request.ReturnOnlyCompleteData = return_only_complete_data
report_request.Time = time
report_request.ReportName = "Hourly Bing Report"
scope = reporting_service.factory.create('AccountThroughCampaignReportScope')
scope.AccountIds = {'long': [account_id]}
# scope.Campaigns = reporting_service.factory.create('ArrayOfCampaignReportScope');
# scope.Campaigns.CampaignReportScope.append();
report_request.Scope = scope
report_columns = reporting_service.factory.create('ArrayOfCampaignPerformanceReportColumn')
report_columns.CampaignPerformanceReportColumn.append([
'TimePeriod',
'CampaignId',
'CampaignName',
'DeviceType',
'Network',
'Impressions',
'Clicks',
'Spend'
])
report_request.Columns = report_columns
return report_request
I'm not super familiar with these ad data APIs, so any insight will be helpful, even if you don't have a solution.
I spent weeks back and forth with Microsoft Support. Here's the result:
You can get logs out of the examples by adding this code:
import logging
logging.basicConfig(level=logging.INFO)
logging.getLogger('suds.client').setLevel(logging.DEBUG)
logging.getLogger('suds.transport').setLevel(logging.DEBUG)
The issue was related to the way the example is built. In the auth_helper.py file there is a method named authenticate that looks like this:
def authenticate(authorization_data):
# import logging
# logging.basicConfig(level=logging.INFO)
# logging.getLogger('suds.client').setLevel(logging.DEBUG)
# logging.getLogger('suds.transport.http').setLevel(logging.DEBUG)
customer_service = ServiceClient(
service='CustomerManagementService',
version=13,
authorization_data=authorization_data,
environment=ENVIRONMENT,
)
# You should authenticate for Bing Ads services with a Microsoft Account.
authenticate_with_oauth(authorization_data)
# Set to an empty user identifier to get the current authenticated Bing Ads user,
# and then search for all accounts the user can access.
user = get_user_response = customer_service.GetUser(
UserId=None
).User
accounts = search_accounts_by_user_id(customer_service, user.Id)
# For this example we'll use the first account.
authorization_data.account_id = accounts['AdvertiserAccount'][0].Id
authorization_data.customer_id = accounts['AdvertiserAccount'][0].ParentCustomerId
As you can see, at the very bottom, it says "For this example, we'll use the first account." It turns out that my company had 2 accounts. This was not configurable anywhere and I had no idea this code was here, but you can add a breakpoint here to see your full list of accounts. We only had 2, so I flipped the 0 to a 1 and everything started working.

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)

Tweepy geo search keeps returning tweets far outside of range

Please help! I am trying complete a geo specific query search using free tweepy API. I realised that the tweet coordinates/place being returned were still from all across the globe. Even when I simplify the code to investigate, the geo still wont work:
# Get auth - deleter is a function I made to format .txt into string, note the appauth not oauth.
def startup(loc):
consumer_secret = deleter(open(loc[0],'r').read(),'\n')
consumer_key = deleter(open(loc[1],'r').read(),'\n')
auth = tweepy.AppAuthHandler(consumer_key, consumer_secret)
api = tweepy.API(auth, wait_on_rate_limit=True,
wait_on_rate_limit_notify=True)
if (not api):
print ("Can't Authenticate"),sys.exit(-1)
return api
# Keys
loc = ['consumer_scret.txt', 'api.txt']
api = startup(loc)
# Search parameters
query = "lockdown"
gg = "5.29126,52.132633,10km"
results = api.search(q=query,geo=gg, count = 100)
for tweet in results:
print(tweet.id,tweet.geo,tweet.place,tweet.coordinates)
No matter how small the search radius, 100 tweets are always returned
If I search in Bangladesh with a radius of 10 mi, tweets with place/coordinates from places as far as the US are returned.
Any help would be really appreciated!
I think your geo parameter is being ignored. The parameter name to pass to the search api is geocode, not geo. Try this:
results = api.search(q=query, geocode=gg, count=100)
When I run that with a search radius as small as 10km I get no results. If I expand the radius to 1000km I get a lot more.
Reference: API.search

Getting Workset id's from the open project Revit API + python

I am trying to get a list of workset name and id's from the active document using Revit API inside of Python node in Dynamo. I am trying to access workset table but this code returns nothing:
doc = __doc__
workset = ActiveWorkset(doc)
active_id = workset.ActiveWorksetId()
OUT = active_id
For now I was just trying to see if i can get active workset first but even that doesnt work.
I haven't tried this in Dynamo, but my trusty RevitPythonShell thinks this should work:
worksetTable = doc.GetWorksetTable()
activeId = worksetTable.GetActiveWorksetId()
workset = worksetTable.GetWorkset(activeId)
this is based on the example from the Revit 2014 API document in the SDK...
The output:
>>> workset
<Autodesk.Revit.DB.Workset object at 0x000000000000002E [Autodesk.Revit.DB.Workset]>
Based on your example, you probably want to do this at the end:
OUT = activeId

Categories

Resources