-
Notifications
You must be signed in to change notification settings - Fork 0
/
Makefile
74 lines (62 loc) · 3.39 KB
/
Makefile
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
# This is an auto documented Makefile. For more information see the following article
# @see http://marmelab.com/blog/2016/02/29/auto-documented-makefile.html
SHELL := /bin/bash
.SHELLFLAGS = -ec
.SILENT:
MAKEFLAGS += --silent
.ONESHELL:
.EXPORT_ALL_VARIABLES:
.DEFAULT_GOAL: help
.PHONY: help ## 🛟 To display this prompts. This will list all available targets with their documentation
help:
echo "❓ Use \`make <target>' where <target> is one of 👇"
grep -E '^\.PHONY: [a-zA-Z0-9_-]+ .*?##' $(MAKEFILE_LIST) | \
awk 'BEGIN {FS = "(: |##)"}; {printf "\033[36m%-30s\033[0m %s\n", $$2, $$3}'
echo "Tips 💡"
echo " - use tab for auto-completion"
echo " - use the dry run option '-n' to show what make is attempting to do. example: make -n NEXT_WEEK"
.PHONY:
check-notion-api-key:
ifndef NOTION_API_KEY
$(error Env variable "NOTION_API_KEY" is not defined)
endif
.PHONY:
check-notion-db:
ifndef NOTION_DB
$(error Env variable "NOTION_DB" is not defined)
endif
.PHONY: raw_db ## ⬇️ To fetch specific entries of WeeklyCuration database in my personal Notion db, eg. make NEXT_WEEK ISSUE="17 Jul. 2023"
raw_db: ISSUE:=10th of July 2023
raw_db: check-notion-api-key check-notion-db
echo "[⋆] Fetching entries of WeeklyCuration database for ${ISSUE}..."
curl -s -X POST 'https://api.notion.com/v1/databases/${NOTION_DB}/query' \
-H 'Authorization: Bearer '"$$NOTION_API_KEY"'' \
-H 'Notion-Version: 2022-06-28' \
-H "Content-Type: application/json" \
--data "$$(envsubst < ls-notion-database.json)" > raw_db.json
echo "[⋆] Newsletter entries are available @ file://$(PWD)/raw_db.json ..."
.PHONY: full_raw_db ## ⏬ To fetch all entries of WeeklyCuration database in my personal Notion db
full_raw_db: check-notion-api-key check-notion-db
echo "[⋆] Fetching entries of WeeklyCuration database for ${ISSUE}..."
curl -s -X POST 'https://api.notion.com/v1/databases/${NOTION_DB}/query' \
-H 'Authorization: Bearer '"$$NOTION_API_KEY"'' \
-H 'Notion-Version: 2022-06-28' \
-H "Content-Type: application/json" > full_raw_db.json
echo "[⋆] Newsletter entries are available @ file://$(PWD)/full_raw_db.json ..."
.PHONY: ls-newsletter-headers ## ❓ to list the newsletter headers that can be used as argument for raw_db target
ls-newsletter-headers: full_raw_db
cat full_raw_db.json | jq "[.results[].properties.NewsletterIssueDateFormat.formula.string] | unique"
.PHONY: cleanup ## 🧹 to clean up temporarily edited or downloaded file
cleanup:
echo "[⋆] 🧹 Emptying NEXT_WEEK.md file ..." && > NEXT_WEEK.md
echo "[⋆] 🧹 Removing JSON results files ..." && rm -f full_raw_db.json raw_db.json
echo "[⋆] 🧹🧹 $@ done ✅🧹"
.PHONY: NEXT_WEEK ## ⚙️ to transform JSON output from Notion API into Markdown content that can be copy/pasted in the README
NEXT_WEEK: raw_db
cat raw_db.json | \
jq "[.results[].properties | {Type: .Type.select.name, Category: .Category.select.name, Article: .Article.title[].text.content, Url: .URL.url, Subcategories: [.Subcategories.multi_select[].name]}]" | \
jq -r 'reduce .[] as $$item ({}; .[$$item.Category] += [$$item])' | \
jq -r 'map({(.[0].Category): map("- \(.Type) [\(.Article)](\(.Url)) | #" + (.Subcategories | join(" #")))}) | add ' | \
jq '.[] |= sort' | \
jq -r 'to_entries | sort_by(.key) | .[] | "### \(.key)\n\n\(.value | join("\n"))\n"' > NEXT_WEEK.md
echo "[⋆] Upcoming newsletter post is available @ file://$(PWD)/NEXT_WEEK.md ..."