Trello API ~ simply getting the contents of a list? - python

I am trying to get the contents of a list on I a board I created to understand some things about my work flow.
The API seems quite complex, and I have been at it for hours. I have an API key, as well as a Secret key.
I tried the following from their docs: https://api.trello.com/1/lists/4eea4ffc91e31d174600004a/cards?key=[application_key]&token=[optional_auth_token]
However, I am not sure where these letters/numbers are coming from: 4eea4ffc91e31d174600004a.
I read the following page: https://developers.trello.com/apis (which gave me the link to the url above), but there is no info on how to get 4eea4ffc91e31d174600004a.
I simply want to visit a url that gives me json or something of that vein, with all the contents of a list (e.g. the cards + their names). Then I can visit that link programmatically and do my analysis.
Edit: Using the trello developer sandbox: https://developers.trello.com/sandbox/ I found the id of a list, which I substituted into 4eea4ffc91e31d174600004a, but now I get the following: Taco says “invalid token”, but what does he know? He's just a dog.
I used the Secret key as the token, but I guess that's not the token. So the question boils down to how can I get a token?
Thanks

So the full answer is:
To get the cards in a list, three things are required:
API Key
List ID
Token
(Secret key isn't needed anywhere)
To get the List ID, the simplest way to do that is to use the developer sandbox https://developers.trello.com/sandbox/ then hit 'get lists' + 'execute' and obtain the id of the list of interest.
Note if you have multiple boards, you will need to specify the board id here: Trello.get('/boards/[board_id]/lists', success, error);. You can get the board id by hitting 'get boards' + 'execute', then looking for the id of interest.
To get a token, you need to go here: https://trello.com/1/connect?key=[application_key]&name=MyApp&response_type=token
Then you can make the call:
https://api.trello.com/1/lists/[list_id]/cards?key=[application_key]&token=[optional_auth_token]

I recently worked on Trello data using its RESTful API and using Python's py-trello. As the question is about accessing Trello list and its cards, will limit my solution to list access using Trello RESTful API and py-trello.
1. Using Trello RESTful API:
Following you need to form your URI to access the desired list and its cards JSON
i) API Key: You can get the key from https://trello.com/app-key
ii) Token: On the above page itself, you'll find link to the Token generation. Posting the snap shot below for convenience-
iii) list id: you can find this in the board JSON
Using these 3 items, below is how the RESTful URI to access a list is going to look like:
list_json_url = "https://api.trello.com/1/lists/replace_this_with_ur_list_id?cards=all&key=replace_this_with_ur_api_key&token=replace_this_with_your_token
Below is how i'm loading above list JSON in my Python program :
with urllib.request.urlopen(list_json_url) as fj:
data = json.load(fj)
2. Using py-trello:
There can be better ways to do this using py-trello's filtering features, i have just taken this from my entire multi-boards analysis code and added a condition to match a list id:
from trello import TrelloClient
API_KEY = "XXXXXXX"
API_TOKEN = "XXXXXXXXXX"
client = TrelloClient(api_key=API_KEY, token=API_TOKEN)
for board in client.list_boards():
for l in board.list_lists():
if l.id = "ur_list_id":
#do your list analysis here

That is the list id which you can get by an API call to the boards
You'll find details here.
https://developers.trello.com/apis#boards

Go to link: https://trello.com/app-key
Click in TOKEN
Click in Allow
Copy your token
DONE

Related

api.get_user with Tweepy will not give description

I'm trying to use Tweepy (version 4.4.0) to get a user's description but it's seemingly not working:
u = api.get_user(username='XXXX', user_fields=['description'])
but the output of this is simply:
Response(data=<User id=123 name=XXX username=XXX>, includes={}, errors=[], meta={})
So it's getting me the name and id fine, but it's returning an empty for any user fields.
Note I've also tried with user_auth: 1, but I get 'Unauthorized: 401' - but from what I've seen around, I don't think user authentication is the problem here... but maybe it is?
Any advice would be great!
api seems to be an instance of tweepy.Client here.
From the relevant FAQ section in Tweepy's documentation:
Why am I not getting expansions or fields data with API v2 using Client?
If you are simply printing the objects and looking at that output, the string representations of API v2 models/objects only include the default attributes that are guaranteed to exist.
The objects themselves still include the relevant data, which you can access as attributes or by key, like a dictionary.
The user object being returned in the response should have a description field with the user's description.
You can access the description with: u.data.description
Keep in mind that the description may be blank some times, try with created_at to be sure it works.
Sample code for extracting description of any twitter user is as follows
import tweepy
auth = tweepy.OAuth2BearerHandler(os.environ.get("TWITTER_API_KEY"))
api = tweepy.API(auth)
user = api.get_user(screen_name="TechWiser", include_entities=False)
description = user._json['description']
user._json contain many other key, value pair that you can explore

Problem with getting tweet_fields from Twitter API 2.0 using Tweepy

I have a similar problem as in this question (Problem with getting user.fields from Twitter API 2.0)
but I am using Tweepy. When making the request with tweet_fields, the response is only giving me the default values. In another fuction where I use user_fields it works perfectly.
I followed this guide, specifically number 17 (https://dev.to/twitterdev/a-comprehensive-guide-for-using-the-twitter-api-v2-using-tweepy-in-python-15d9)
My function looks like this:
def get_user_tweets():
client = get_client()
tweets = client.get_users_tweets(id=get_user_id(), max_results=5)
ids = []
for tweet in tweets.data:
ids.append(str(tweet.id))
tweets_info = client.get_tweets(ids=ids, tweet_fields=["public_metrics"])
print(tweets_info)
This is my response (with the last tweets from elonmusk) also there is no error code or anything else
Response(data=[<Tweet id=1471419792770973699 text=#WholeMarsBlog I came to the US with no money & graduated with over $100k in debt, despite scholarships & working 2 jobs while at school>, <Tweet id=1471399837753135108 text=#TeslaOwnersEBay #PPathole #ScottAdamsSays #johniadarola #SenWarren It’s complicated, but hopefully out next quarter, along with Witcher. Lot of internal debate as to whether we should be putting effort towards generalized gaming emulation vs making individual games work well.>, <Tweet id=1471393851843792896 text=#PPathole #ScottAdamsSays #johniadarola #SenWarren Yeah!>, <Tweet id=1471338213549744130 text=link>, <Tweet id=1471325148435394566 text=#24_7TeslaNews #Tesla ❤️>], includes={}, errors=[], meta={})
I found this link: https://giters.com/tweepy/tweepy/issues/1670. According to it,
Response is a namedtuple. Here, within its data field, is a single Tweet object.
The string representation of a Tweet object will only ever include its ID and text. This was an intentional design choice, to reduce the excess of information that could be displayed when printing all the data as the string representation, as with models.Status. The ID and text are the only default / guaranteed fields, so the string representation remains consistent and unique, while still being concise. This design is used throughout the API v2 models.
To access the data of the Tweet object, you can use attributes or keys (like a dictionary) to access each field.
If you want all the data as a dictionary, you can use the data attribute/key.
In that case, to access public metrics, you could maybe try doing this instead:
tweets_info = client.get_tweets(ids=ids, tweet_fields=["public_metrics"])
for tweet in tweets_info.data:
print(tweet["id"])
print(tweet["public_metrics"])

transform JSON file to be usable

Long story short, i get the query from spotify api which is JSON that has data about newest albums. How do i get the specific info from that like let's say every band name or every album title. I've tried a lot of ways to get that info that i found on the internet and nothing seems to work for me and after couple of hours im kinda frustrated
JSON data is on jsfiddle
here is the request
endpoint = "https://api.spotify.com/v1/browse/new-releases"
lookup_url = f"{endpoint}"
r = requests.get(lookup_url, headers=headers)
print(r.json())
you can find the
When you make this request like the comments have mentioned you get a dictionary which you can then access the keys and values. For example if you want to get the album_type you could do the following:
print(data["albums"]["items"][0]["album_type"])
Since items contains a list you would need to get the first values 0 and then access the album_type.
Output:
single
Here is a link to the code I used with your json.
I suggest you look into how to deal with json data in python, this is a good place to start.
I copied the data from the jsfiddle link.
Now try the following code:
import ast
pyobj=ast.literal_eval(str_cop_from_src)
later you can try with keys
pyobj["albums"]["items"][0]["album_type"]
pyobj will be a python dictionary will all data.

Understanding Todoist-API

I am trying to use the Todoist-API for Python. I found the official docs on the internet and downloaded the GitHub-Repo. Unfortunately I don't get out how to add a new task.
I do the normal login:
api = todoist.TodoistAPI(XYZ)
api.sync
Then I try to add a new task:
item = api.items.add('Task1')
It tells me I have to give two arguments: name and project_id:
item = api.items.add('Task1', 128501470)
Does anyone know where I could get all my projects IDs? I just want to use the Inbox-Project (default).
I'm not very familiar with this specific API, but considering you're using this api: https://github.com/doist/todoist-python, you can probably do something like:
response = api.sync()
projects = response['projects']
for project in projects:
print(project['name'] + '-' + project['id'])
Basically printing all the names and id's
Just open Todoist in a web browser and look at the address bar, it's right after "project", I beleive you need to truncate the first three or 4 characters though, click through a few projects and you'll see the project id's change.
To add them to the inbox the easiest way is to do the following:
from todoist.api import TodoistAPI
apiToken = 'your token"
todoist: TodoistAPI = TodoistAPI(api_token)
response = todoist.add_item("Item1")
todoist.sync()
todoist.commit()
You will have to refresh either the web page or your app to immediately see the new item

tweepy get_user does not work with a list of user_ids

I am using tweepy library to pull informations about Twitter users. What I want is, given a list of Twitter ids, get the number of followers of each user that relates to the ids. The function looks like this.
infos = api.get_user(user_id=xxx)
return infos.followers_count
In the Twitter documentation, it says I can input a list of up to 100 user ids to get_user. Thing is, whatever I pass to that function other than a single id, I get the error
[{'message': 'Sorry, that page does not exist', 'code': 34}]
For example if I write:
infos = api.get_user(user_id=[user1,user2,user3])
I get the error. But if I write
infos = api.get_user(user_id=user1)
It works perfectly well. Did you encounter this problem before? Is the problem in Tweepy library? Should I use another library?
Thanks for your support
You need to use the function lookup_users for this, and the parameter is user_ids:
infos = api.lookup_users(user_ids=[user1,user2,user3])
https://github.com/tweepy/tweepy/blob/master/tweepy/api.py#L154

Categories