I currently use the function get_users_following(user_id_account, max_results=1000) to get the list of follows of an account, to know who he follows on twitter. So far it works well as long as the user follows less than 1000 people because the API limits to a list of maximum 1000 users. The problem is that when he follows more than 1000 people I can't get the last people. The function always gives me the first 1000 and ignores the last ones
https://docs.tweepy.org/en/stable/client.html#tweepy.Client.get_users_followers
https://developer.twitter.com/en/docs/twitter-api/users/follows/api-reference/get-users-id-following
There is a pagination_token parameter but I don't know how to use it. What I want is just the last X new people followed so I can add them to a database and get a notification for each new entry
client = tweepy.Client(api_token)
response = client.get_users_following(id=12345, max_results=1000)
Is it possible to go directly to the last page?
Tweepy handles the pagination with the Paginator class (see the documentation here).
For example, if you want to see all the pages, you could do something like that:
# Use the wait_on_rate_limit argument if you don't handle the exception yourself
client = tweepy.Client(api_token, wait_on_rate_limit=True)
# Instantiate a new Paginator with the Tweepy method and its arguments
paginator = tweepy.Paginator(client.get_users_following, 12345, max_results=1000)
for response_page in paginator:
print(response_page)
Or you could also directly get the full list of the user's followings:
# Instantiate a new Paginator with the Tweepy method and its arguments
paginator = tweepy.Paginator(client.get_users_following, 12345, max_results=1000)
for user in paginator.flatten(): # Without a limit argument, it gets all users
print(user.id)
Related
Is there a way to check if the user is blocked like an is_blocked method? The way I have it set up right now is to get a list of my blocked users and comparing the author of a Tweet to the list, but it's highly inefficient, as it constantly runs into the rate limit.
Relevent Code:
blocked_screen_names = [b.screen_name for b in tweepy.Cursor(api.blocks).items()]
for count, tweet in enumerate(tweepy.Cursor(api.search, q = query, lang = 'en', result_type = 'recent', tweet_mode = 'extended').items(500)):
if tweet.user.screen_name in blocked_screen_names:
continue
No, you'll have to do that the way you're currently doing it.
(Also, just a side note, you're better off checking your blocked users via their account ID rather than their screen name, because this value will not change, whereas a user's screen name can)
For future reference, just check the Twitter API documentation, where you can get the answer for something like this straight away :) save yourself the waiting for someone to answer it for you here!
You'll notice that both the documentation for V1 and V2 do not contain an attribute as you have described:
V1 User Object:
https://developer.twitter.com/en/docs/twitter-api/v1/data-dictionary/object-model/tweet
V2 User Object:
https://developer.twitter.com/en/docs/twitter-api/data-dictionary/object-model/user
This is my callback that executes when a user is added to a collection
# Create a callback on_snapshot function to capture changes
def on_snapshot_user(col_snapshot, changes, read_time):
print(u'Callback received query snapshot user.')
for change in changes:
if change.type.name == 'ADDED':
doc = change.document.to_dict()
email = doc["email"]
group = doc["survey_group"]
gender = doc["survey_gender"]
age = doc["survey_age"]
userInfo = User.query.filter_by(email=email).first()
if userInfo is None:
User.insert(User(email))
userInfo = User.query.filter_by(email=email).first()
userInfo.group = group
userInfo.gender = gender
userInfo.age = age
User.update(userInfo)
email is a primary key. When a user is added to Firestore, this triggers twice and it gives an error of a duplicate key.
How do I make this execute once in Python as metadata.PendingWrites is not yet supported in Python?
I am developing in Flask. I'm new to it so would appreciate any kind of help.
Edit:
Some context - I am adding the data I get to a PostgreSQL database. I am planning to do so as I need to build a kind of leaderboard and I'm planning to store the user info + their points in a postgreSQL table.
There are some steps to resolve multiple execution issue :
1. First would be to create a doc reference variable for the document you want to execute.
Create a doc_ref variable for the document you want to populate.
Syntax : doc_ref = db.Collection(collection_name)
2. Second step would be to watch the document.
doc_watch would be the variable created for watching the document.
Syntax : doc_watch = doc_ref.on_snapshot_functionName(on_snapshot_functionName)
3. Third step would be to Terminate watch on the document so it will be executed once.
You can use unsubscribe() to terminate the watch.
Syntax : doc_watch.unsubscribe()
For further details related to real time updates in Cloud Firestore, you can refer to the documentation [1].
[1] : https://cloud.google.com/firestore/docs/query-data/listen
Below is my current code. It connects successfully to the organization. How can I fetch the results of a query in Azure like they have here? I know this was solved but there isn't an explanation and there's quite a big gap on what they're doing.
from azure.devops.connection import Connection
from msrest.authentication import BasicAuthentication
from azure.devops.v5_1.work_item_tracking.models import Wiql
personal_access_token = 'xxx'
organization_url = 'zzz'
# Create a connection to the org
credentials = BasicAuthentication('', personal_access_token)
connection = Connection(base_url=organization_url, creds=credentials)
wit_client = connection.clients.get_work_item_tracking_client()
results = wit_client.query_by_id("my query ID here")
P.S. Please don't link me to the github or documentation. I've looked at both extensively for days and it hasn't helped.
Edit: I've added the results line that successfully gets the query. However, it returns a WorkItemQueryResult class which is not exactly what is needed. I need a way to view the column and results of the query for that column.
So I've figured this out in probably the most inefficient way possible, but hope it helps someone else and they find a way to improve it.
The issue with the WorkItemQueryResult class stored in variable "result" is that it doesn't allow the contents of the work item to be shown.
So the goal is to be able to use the get_work_item method that requires the id field, which you can get (in a rather roundabout way) through item.target.id from results' work_item_relations. The code below is added on.
for item in results.work_item_relations:
id = item.target.id
work_item = wit_client.get_work_item(id)
fields = work_item.fields
This gets the id from every work item in your result class and then grants access to the fields of that work item, which you can access by fields.get("System.Title"), etc.
I am trying to learn how to use the new WebHooks service on IFTTT, and I'm struggling to figure out how it is supposed to work. Most of the projects I can find online seem to refer to a deprecated "maker" service, and there are very little resources for interpreting the new channel.
Let's say I want to check the value of "online" every ten minutes in the following json file: https://lichess.org/api/users/status?ids=thibault
I can write a Python script that extracts this value like so:
response = urlopen('https://lichess.org/api/users/status?ids=thibault')
thibault = response.read()
data = json.loads(thibault)
status = data[0]['online']
If the status is equal to "true", I would like to receive a notification via email or text message. How do I integrate the python script and the webhooks service? Or do I even need to use this script at all? I assume I need some kind of cron job that regularly runs this Python script, but how do I connect this job with IFTTT?
When I create a new applet on IFTTT I am given the ability to create a trigger with a random event name, but it's unclear what that event name corresponds to.
I have a similar setup for my IFTTT webhook service. To the best of my understanding, the answer to your question is yes, you need this script (or similar) to scrap the online value, and you'll probably want to do a cron job (my approach) or keep the script running (wouldn't be my preference).
IFTTT's webhooks a json of up to 3 values, which you can POST to a given event and key name.
Below is a very simple excerpt of my webhook API:
def push_notification(*values, **kwargs):
# config is in json format
config = get_config()
report = {}
IFTTT = {}
# set default event/key if kwargs are not present
for i in ['event', 'key']:
IFTTT[i] = kwargs[i] if kwargs and i in kwargs.keys() else config['IFTTT'][i]
# unpack values received (up to 3 is accepted by IFTTT)
for i, value in enumerate(values, 1):
report[f"value{i}"] = value
if report:
res = requests.post(f"https://maker.ifttt.com/trigger/{IFTTT['event']}/with/key/{IFTTT['key']}", data=report)
# TODO: add try/except for status
res.raise_for_status()
return res
else:
return None
You probably don't need all these, but my goal was to set up a versatile solution. At the end of the day, all you really need is this line here:
requests.post(f"https://maker.ifttt.com/trigger/{event}/with/key/{key}", data={my_json_up_to_3_values})
Where you will be placing your webhook event name and secret key value. I stored them in a config file. The secret key will be available once you sign up on IFTTT for the webhook service (go to your IFTTT webhook applet setting). You can find your key with a quick help link like this: https://maker.ifttt.com/use/{your_secret_key}. The event can be a default value you set on your applet, or user can choose their event name if you allow.
In your case, you could do something like:
if status:
push_notification("Status is True", "From id thibault", event="PushStatus", key="MysEcR5tK3y")
Note: I used f-strings with version 3.6+ (It's great!), but if you have a lower version, you should switch all the f-strings to str.format().
I am trying to make a Slack Bot using python, and I have a issue, I am not able to get the users from a specific channel, I only succeed if I take all of the users. Basically I want only those from (eg. random channel).
Until now I tried to get the team's ID from every user and compare it to a channel ID, but that failed because everyone is having the same ID and I can't figure out why.
Here is the snippet of the code:
def users_of_the_channel():
global slack_client
#this is the variable which is initialized with SlackClient(BOT_TOKEN)
api_call = slack_client.api_call( "users.list",channel="C0XXXXXXX")
if api_call.get('ok'):
channels = api_call.get('members')
for channel in channels:
print ("this is cool : ", channel['team_id'])
The issue I believe is that when I initialize the api_call variable I call the function with the users.list argument, I tried with usergroups.list and usergroups.users.list but with no success.
Basically to keep it short I need the list with the users from a channel, and the documentation hasn't helped me.
users.list does not take channel id as input, which you are providing in your API call.
Slack provide different bot token for different teams.
Simply call api with bot token of team of which you require user list and you will get the list of members.
client = SlackClient(SLACK_BOT_TOKEN)
request = client.api_call("users.list")
if request['ok']:
for item in request['members']:
print item['name']
For more details about optional arguments you can provide with api call, refer documentation(https://api.slack.com/methods/users.list).
You probably can use the new conversations API to retrieve that (specifically conversations.members endpoint):
https://api.slack.com/methods/conversations.members