File size: 4,229 Bytes
3a61d67
96cc16a
3a61d67
 
 
96cc16a
 
3a61d67
 
96cc16a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3a61d67
 
96cc16a
 
 
 
0a81a06
96cc16a
3a61d67
 
82f8bba
3a61d67
 
96cc16a
 
0616eaf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
96cc16a
 
3a61d67
0a81a06
96cc16a
 
 
 
 
 
 
 
 
 
 
 
0a81a06
96cc16a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import os
import requests
from typing import Any, Optional
from smolagents.tools import Tool

class NYTBestSellerTool(Tool):
    name = "nyt_best_sellers_tool"
    description = (
        "Performs a search of the New York Times best seller list " 
        "for a specific genre and returns the best selling books.\n"
        "The possible options for genre are:\n"
        "combined-print-and-e-book-fiction, "
        "combined-print-and-e-book-nonfiction, "
        "hardcover-fiction, "
        "hardcover-nonfiction, "
        "trade-fiction-paperback, "
        "paperback-nonfiction, "
        "advice-how-to-and-miscellaneous, "
        "childrens-middle-grade-hardcover, "
        "picture-books, "
        "series-books, "
        "young-adult-hardcover, "
        "audio-fiction, "
        "audio-nonfiction, "
        "business-books, "
        "graphic-books-and-manga, "
        "mass-market-monthly, "
        "middle-grade-paperback-monthly, "
        "young-adult-paperback-monthly."
        )
    
    inputs = {
        'genre': 
            {'type': 'string', 'description': 'The genre to search'},
        'limit': 
            {'type': 'integer', 'description': 'The number of results to include', 'nullable': True}
        }
    output_type = "string"

    def __init__(self):
        super().__init__()
        self.api_prefix = "https://api.nytimes.com/svc/books/v3/lists/current"
        self.api_key = os.getenv("NYT_API_KEY")
        self.possible_genres = [
            "combined-print-and-e-book-fiction",
            "combined-print-and-e-book-nonfiction",
            "hardcover-fiction",
            "hardcover-nonfiction",
            "trade-fiction-paperback",
            "paperback-nonfiction",
            "advice-how-to-and-miscellaneous",
            "childrens-middle-grade-hardcover",
            "picture-books",
            "series-books",
            "young-adult-hardcover",
            "audio-fiction",
            "audio-nonfiction",
            "business-books",
            "graphic-books-and-manga",
            "mass-market-monthly",
            "middle-grade-paperback-monthly",
            "young-adult-paperback-monthly"
        ]

    def forward(self, genre: str, limit: int = 5) -> str:
        if isinstance(limit,str):
            limit = int(limit)

        if genre not in self.possible_genres:
            raise Exception(
                f"{genre} is not a valid genre. Please search by a genre from the list: {self.possible_genres}"
                )

        result = requests.get(
            f"https://api.nytimes.com/svc/books/v3/lists/{genre}.json",
            params={"api-key":self.api_key}
        )
        if result.status_code != 200:
            raise Exception("Error getting the best seller list. Please try again later.")
        
        book_results = result.json()['results']['books']

        book_result_str = ""
        for ix, book_result in enumerate(book_results):

            rank = book_result.get("rank")
            if rank:
                book_result_str += f"rank: {rank}\n"

            title = book_result.get("title")
            if title:
                book_result_str += f"title: {title}\n"

            author = book_result.get("author")
            if author:
                book_result_str += f"author: {author}\n"

            description = book_result.get("description")
            if description:
                book_result_str += f"description: {description}\n"

            primary_isbn10 = book_result.get("primary_isbn10")
            if primary_isbn10:
                book_result_str += f"primary_isbn10: {primary_isbn10}\n"

            primary_isbn13 = book_result.get("primary_isbn13")
            if primary_isbn13:
                book_result_str += f"primary_isbn13: {primary_isbn13}\n"

            buy_links = book_result.get("buy_links")
            if buy_links:
                book_result_str += f"buy_links: "
                for buy_link in buy_links:
                    book_result_str += f"{buy_link.get('name')} {buy_link.get('url')}\n"

            book_result_str += "=============================\n\n"

            if ix + 1 == limit:
                break
        return book_result_str