-
Notifications
You must be signed in to change notification settings - Fork 3
/
generate_readme.sh
147 lines (130 loc) · 4.21 KB
/
generate_readme.sh
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#!/bin/bash
# Bing Daily Image Downloader and README Generator
#
# This script automates the process of downloading the daily image from Bing,
# generating a description using OpenAI's GPT-4 model, and updating a README file.
#
# Usage:
# 1. Ensure you have the necessary dependencies installed: curl, wget, jq, base64
# 2. Set the OPENAI_API_KEY environment variable with your OpenAI API key
# 3. Run the script: ./generate_readme.sh
#
# The script will:
# - Check if today's Bing image has already been downloaded
# - If not, download the image and request a description from OpenAI
# - Generate or update the README.md file with the new image and description
#
# Note: This script is designed to be run daily, ideally through a scheduled task or CI/CD pipeline.
# Function to check if image exists
# Usage: if image_exists "ImageName"; then ...
image_exists() {
local image_name=$1
if [ -f "images/${image_name}.jpg" ]; then
return 0 # Image exists
else
return 1 # Image does not exist
fi
}
# Function to get image name without downloading
# Usage: image_name=$(get_image_name)
get_image_name() {
local tail=$(curl -s "https://www.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1" | grep -oP "th\?id=.*?jpg")
echo $(echo $tail | grep -oP 'OHR\.\K([a-zA-Z]+)')
}
# Function to download the image
# Usage: image_path=$(download_image "ImageName")
download_image() {
local image_name=$1
local tail=$(curl -s "https://www.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1" | grep -oP "th\?id=.*?jpg")
wget -O "images/${image_name}.jpg" https://bing.com/${tail}
echo "images/${image_name}.jpg"
}
# Function to encode image to base64
# Usage: base64_image=$(encode_image "path/to/image.jpg")
encode_image() {
local image_path=$1
base64 -i "$image_path"
}
# Function to create JSON payload for OpenAI API
# Usage: temp_json=$(create_json_payload "$base64_image")
create_json_payload() {
local base64_image=$1
local temp_json=$(mktemp)
cat << EOF > "$temp_json"
{
"model": "gpt-4o",
"messages": [
{
"role": "user",
"content": [
{
"type": "text",
"text": "Describe this image in one sentence."
},
{
"type": "image_url",
"image_url": {
"url": "data:image/jpeg;base64,$base64_image"
}
}
]
}
],
"max_tokens": 100
}
EOF
echo "$temp_json"
}
# Function to make API request to OpenAI
# Usage: response=$(make_api_request "$temp_json")
make_api_request() {
local temp_json=$1
curl -s -X POST \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-d @"$temp_json" \
https://api.openai.com/v1/chat/completions
}
# Function to extract content from OpenAI response
# Usage: content=$(extract_content "$response")
extract_content() {
local response=$1
echo $response | jq -r '.choices[0].message.content'
}
# Function to generate README.md
# Usage: generate_readme "$image_path" "$content"
generate_readme() {
local image_path=$1
local content=$2
local current_date=$(date -u +"%Y-%m-%d %H:%M:%S UTC")
cat << EOF > README.md
![Collect Bing.com daily images](https://github.com/counter2015/bing-daily-images/workflows/Collect%20Bing.com%20daily%20images/badge.svg)
## Latest image:
![]($image_path)
EOF
if [ ! -z "$content" -a "$content" != "null" ]; then
echo "$content" >> README.md
echo "" >> README.md
fi
cat << EOF >> README.md
Use GitHub Actions to download www.bing.com images.
Last update: $current_date
All images since 2020-05-10 [here](https://github.com/counter2015/bing-daily-images/tree/master/images)
EOF
echo "README.md has been generated."
}
# Main execution
image_name=$(get_image_name)
if image_exists "$image_name"; then
echo "Image already exists. Skipping download and API request."
else
image_path=$(download_image "$image_name")
base64_image=$(encode_image "$image_path")
temp_json=$(create_json_payload "$base64_image")
response=$(make_api_request "$temp_json")
echo "response: $response"
rm "$temp_json"
content=$(extract_content "$response")
echo "content: $content"
generate_readme "$image_path" "$content"
fi