Using Python API to get all comments on a project

I am trying to use the Asana API with python to pull all the comments for all my projects. I understand from asana support that to do this I need to get_story. How do I get a story gid? I don’t see anything in Asana that says story to pull from. I do have the project id.

I’m currently using the below but am open to suggestions:
client.stories.get_story(‘GID’, opt_pretty=True)

Hi @anon18191098 and welcome to the forum!

Point of clarification: are you referring to project conversations, or comments on individual tasks?

comments on individual tasks. I exported the projects to CSV and have all the tasks, and associated fields, but I want to get the comments that go along with them.

Gotcha.

The get_story method requires a story gid, as you noted, and only gets you one specific story.

What you want to use instead is get_stories_for_task. You’ll loop through each task that you have in your CSV list, and for each one, call that method to retrieve all of that task’s stories.

1 Like

I have a CSV list with a task ID column, it has over 8000 task IDs. I need to export all the comments/stories for these tasks and was looking at using the get_stories_for_task API - I’ve been able to input 1 task ID and return the data but don’t know how to input multiple task ID into the task_gid or how to “loop through each task that you have in your CSV list” as the previous reply mentions. Can someone please provide instructions on how to do this?

Hi @Sophie_Grosvenor and welcome to the forum!

That would be done in program code; are you a programmer (and if so, in what language) or do you have access to one that could do this?

The specifics of the loop code would be dependent on the programming language being used.

2 Likes

Hi @Sophie_Grosvenor,

“looping through” is achieved with the keyword “for” in Python.

You can use the standard csv library to read data from CSV files.

You can write code like:

import asana
import csv

# Asana config here

with open('file_name.csv') as csv_file:
    csv_reader = csv.reader(csv_file)
    for row in csv_reader:
        task_gid = row[0]
        try:
            api_response = stories_api_instance.get_stories_for_task(task_gid, opts)
            for data in api_response:
                your_function_here(data)
        except ApiException as e:
            print("Exception when calling StoriesApi->get_stories_for_task: %s\n" % e)

Or, you can loop through the tasks from an Asana project directly like this nested API calls:

import asana

# Asana config here

try:
    tasks_response = tasks_api_instance.get_tasks_for_project(project_gid, opts)
    for task_data in tasks_response:
        try:
            task_gid = task_data.gid
            stories_response = stories_api_instance.get_stories_for_task(task_gid, opts)
            for stories_data in stories_response:
                your_function_here(data)
        except ApiException as e:
            print("Exception when calling StoriesApi->get_stories_for_task: %s\n" % e)
except ApiException as e:
    print("Exception when calling TasksApi->get_tasks_for_project: %s\n" % e)

Disclaimer: I haven’t written production Python codes for years. The above code is just general guideline and I can’t guarantee that they work.
Asana’s documentation has improved significantly in the meantime! I wish you a happy coding!