Skip to content

Instantly share code, notes, and snippets.

@jeyraof
Last active September 9, 2024 14:47
Show Gist options
  • Save jeyraof/bbed8cce475ae0309bb9d8fa3b32c0eb to your computer and use it in GitHub Desktop.
Save jeyraof/bbed8cce475ae0309bb9d8fa3b32c0eb to your computer and use it in GitHub Desktop.
밀리의서재에 있는 책인가?

Usage

$ chmod +x millie.sh
$ brew install jq
or
$ apt-get install jq

1. Inline

$ echo '사피엔스' | ./millie.sh

response would be

{
  "found": [
    {
      "author": "유발 하라리 지음 / 조현욱 옮김",
      "content_name": "사피엔스",
      "book_brand": "김영사",
      "subtitle": "유인원에서 사이보그까지, 인간 역사의 대담하고 위대한 질문",
      "content_thumb_url": "https://img.millie.co.kr/200x/service/cover/7880427/bf4a0b00c57b486ba8031ff6b2758c03.jpg"
    }
  ],
  "notfound": []
}

2. Bulk

$ ./millie.sh < books.txt

response would be

{
  "found": [
    {
      "author": "EBS자본주의제작팀,정지은,고희정 지음",
      "content_name": "자본주의 사용설명서",
      "book_brand": "가나출판사",
      "subtitle": "EBS 다큐프라임",
      "content_thumb_url": "https://img.millie.co.kr/200x/service/cover/6482251/58397e7b7a6740a581079c34b8966bb1.jpg"
    },
    {
      "author": "제나마치오키 지음, 오수원 옮김",
      "content_name": "면역의 힘",
      "book_brand": "윌북",
      "subtitle": "살면서 마주하는 모든 면역의 과학",
      "content_thumb_url": "https://img.millie.co.kr/200x/service/cover/179491467/ea0664a5228645138ccaaf5b874ce1af.jpg"
    },
    {
      "author": "이태준",
      "content_name": "129 문장강화",
      "book_brand": "범우사",
      "subtitle": "",
      "content_thumb_url": "https://img.millie.co.kr/200x/service/cover/179591461/aaf7f0cf2d374e9390e9ed9dc19b65b8.jpg"
    }
  ],
  "notfound": [
    {
      "content_name": "나올리가 없는 책"
    }
  ]
}
자본주의
면역
문장강화
나올리가 없는 책
#!/bin/bash
API_URL="https://live-api.millie.co.kr/v3/search/content?keyword="
found_books=()
not_found_books=()
fetch_book_data() {
local keyword="$1"
local response=$(curl -s "${API_URL}${keyword}")
echo "$response"
}
parse_and_format() {
local response="$1"
local keyword="$2"
local total=$(echo "$response" | jq -r '.RESP_DATA.total // 0')
if ! [[ "$total" =~ ^[0-9]+$ ]] || [ "$total" -eq 0 ]; then
not_found_books+=("{\"content_name\": \"$keyword\"}")
return
fi
local author=$(echo "$response" | jq -r '.RESP_DATA.list[0].author')
local content_name=$(echo "$response" | jq -r '.RESP_DATA.list[0].content_name')
local book_brand=$(echo "$response" | jq -r '.RESP_DATA.list[0].book_brand')
local subtitle=$(echo "$response" | jq -r '.RESP_DATA.list[0].subtitle')
local content_thumb_url=$(echo "$response" | jq -r '.RESP_DATA.list[0].content_thumb_url')
local json_obj=$(jq -n \
--arg author "$author" \
--arg content_name "$content_name" \
--arg book_brand "$book_brand" \
--arg subtitle "$subtitle" \
--arg content_thumb_url "$content_thumb_url" \
'{author: $author, content_name: $content_name, book_brand: $book_brand, subtitle: $subtitle, content_thumb_url: $content_thumb_url}')
found_books+=("$json_obj")
}
while IFS= read -r book_name; do
if [ -n "$book_name" ]; then
response=$(fetch_book_data "$book_name")
parse_and_format "$response" "$book_name"
fi
done
found_json=$(printf '%s\n' "${found_books[@]}" | jq -s .)
notfound_json=$(printf '%s\n' "${not_found_books[@]}" | jq -s .)
final_output=$(jq -n \
--argjson found "$found_json" \
--argjson notfound "$notfound_json" \
'{found: $found, notfound: $notfound}')
echo "$final_output"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment