-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.js
108 lines (94 loc) · 2.77 KB
/
utils.js
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
const imagePromptFromLocalStorage = localStorage.getItem("imagePrompt")
const quotePromptFromLocalStorage = localStorage.getItem("quotePrompt")
const imageUrlFromLocalStorage = localStorage.getItem("imageUrl")
const quoteFromLocalStorage = localStorage.getItem("quote")
const quoteSpan = document.querySelector(".quote-span")
const quoteWrapper = document.querySelector(".quote-wrapper")
const nameSpan = document.querySelector(".name-span")
const loader = document.getElementById("loader")
function startLoading() {
nameSpan.style.display = "none"
quoteWrapper.style.display = "none"
loader.style.display = "block"
document.body.backgroundImage = ""
}
function stopLoading(name, url, quote) {
nameSpan.style.display = "inline"
quoteWrapper.style.display = "block"
loader.style.display = "none"
nameSpan.textContent = `${name} - ${getDate()}`
document.body.style.backgroundImage = `url(${url})`
quoteSpan.textContent = quote
}
export async function generateTextAndImage(
name,
favActivity,
favPlace,
temperature
) {
startLoading()
let url = await getImage(favPlace)
let quote = await getQuote(favActivity, favPlace, temperature)
stopLoading(name, url, quote)
return
}
function getDate() {
const date = new Date()
const monthIndex = date.getMonth()
const year = date.getFullYear()
const monthNames = [
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December",
]
const monthName = monthNames[monthIndex]
return `${monthName} ${year}`
}
async function getImage(query) {
const response = await fetch(
`https://apis.scrimba.com/unsplash/photos/random/?count=1&query=${query}`
)
if (response.ok) {
const data = await response.json()
const imageUrl = data[0].urls.full
return imageUrl
} else {
console.error(`Error: ${response.status}`)
}
}
async function getQuote(favActivity, favPlace, temperature) {
let quotePrompt = `Create a poetic phrase about ${favActivity} and ${favPlace} in the insightful, witty and satirical style of Oscar Wilde. Omit Oscar Wilde's name.`
if (quotePrompt === quotePromptFromLocalStorage) {
return quoteFromLocalStorage
}
localStorage.setItem("quotePrompt", quotePrompt)
let body = {
model: "text-davinci-003",
prompt: quotePrompt,
temperature: temperature,
max_tokens: 256,
top_p: 1,
frequency_penalty: 0,
presence_penalty: 0,
}
let res = await fetch("https://apis.scrimba.com/openai/v1/completions", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(body),
})
let response = await res.json()
let newQuote = response.choices[0].text
localStorage.setItem("quote", newQuote)
return newQuote
}