Guest User

Untitled

a guest
Oct 20th, 2024
244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.25 KB | None | 0 0
  1. import requests
  2. import csv
  3.  
  4. '''
  5. this script looks for a csv file "books.csv" formatted as so:
  6.     author,title
  7.     "Andy Weir","Artemis"
  8.     ..
  9. then, it creates another csv file "books_out.csv" with the added hardcover.app ids for each entry:
  10.     author,title,id
  11.     Andy Weir,Artemis,99422
  12.     ..
  13. then, it adds these books to the Want To Read list of whoever is the owner of the Bearer authorization
  14. enjoy!
  15. i take no responsibility this works for you and doesn't wreck your account or system, run at your own risk and/or peril <3
  16. ALSO, CHANGE THE auth VARIABLE TO YOUR OWN API KEY!!!
  17. '''
  18.  
  19. auth = "Bearer ..."
  20.  
  21. def get_book_id(title, author):
  22.     query = """
  23.    query bookSearch($title: String!, $author: String!) {
  24.        books(
  25.            order_by: {users_read_count: desc}
  26.            where: {_and: [{title: {_ilike: $title}}, {contributions: {author: {name: {_ilike: $author}}}}]}
  27.        ) {
  28.            users_read_count
  29.            title
  30.            image {
  31.                url
  32.            }
  33.            id
  34.            contributions {
  35.                author {
  36.                    name
  37.                }
  38.            }
  39.        }
  40.    }"""
  41.    
  42.     variables = {
  43.         "title": f"%{title}%",
  44.         "author": f"%{author}%"
  45.     }
  46.  
  47.     headers = {
  48.         "Content-Type": "application/json",
  49.         "Authorization": auth
  50.     }
  51.  
  52.     response = requests.post(
  53.         'https://api.hardcover.app/v1/graphql',
  54.         json={
  55.             "query": query,
  56.             "variables": variables
  57.         },
  58.         headers=headers
  59.     )
  60.     try:
  61.         book_found = response.json().get("data").get("books")[0]
  62.         return book_found.get("id") if book_found else -1
  63.     except:
  64.         return -1
  65.  
  66. with open("books.csv", mode='r', newline='', encoding='utf-8-sig') as infile:
  67.     reader = csv.DictReader(infile)
  68.     fieldnames = reader.fieldnames + ['id']
  69.     with open("books_out.csv", mode='w', newline='', encoding='utf-8-sig') as outfile:
  70.         writer = csv.DictWriter(outfile, fieldnames=fieldnames)
  71.         writer.writeheader()
  72.         for row in reader:
  73.             print(f"{row['title']} - {row['author']} ...", end="")
  74.             book_id = get_book_id(row['title'], row['author'])
  75.             if book_id != -1:
  76.                 print(book_id)
  77.                 row['id'] = book_id
  78.                 writer.writerow(row)
  79.             else:
  80.                 print("NO ID FOUND")
  81.  
  82. def wantToRead(id: int):
  83.     query = """
  84.    mutation wantToRead($id: Int!) {
  85.    insert_user_book(object: {
  86.        book_id: $id,
  87.        status_id: 1
  88.    }) {
  89.        id
  90.    }
  91.    }
  92.    """
  93.  
  94.     variables = {
  95.         "id": id
  96.     }
  97.  
  98.     headers = {
  99.         "Content-Type": "application/json",
  100.         "Authorization": auth
  101.     }
  102.  
  103.     response = requests.post(
  104.         'https://api.hardcover.app/v1/graphql',
  105.         json={
  106.             "query": query,
  107.             "variables": variables
  108.         },
  109.         headers=headers
  110.     )
  111.     print(response.json())
  112.  
  113. with open("books_out.csv", mode='r', newline='', encoding='utf-8-sig') as infile:
  114.         reader = csv.DictReader(infile)
  115.         for row in reader:
  116.               id = int(row["id"])
  117.               print(f"Adding {id} - {row['title']} by {row['author']}..")
  118.               wantToRead(id)
  119.  
  120.  
Advertisement
Add Comment
Please, Sign In to add comment