forked from exitflynn/OSDPlaylist
-
Notifications
You must be signed in to change notification settings - Fork 3
227 lines (192 loc) · 8.3 KB
/
tf-apply.yaml
File metadata and controls
227 lines (192 loc) · 8.3 KB
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# name: Update playlist with Spotify Auth
# on:
# push:
# branches:
# - main
# jobs:
# spotify-auth-and-apply:
# runs-on: ubuntu-latest
# name: Apply
# steps:
# - name: Checkout repository
# uses: actions/checkout@v4
# - name: Setup Go
# uses: actions/setup-go@v5
# with:
# go-version: '1.18.0'
# - name: Install spotify_auth_proxy
# run: go install github.com/conradludgate/terraform-provider-spotify/spotify_auth_proxy@latest
# - name: Run spotify_auth_proxy and capture Auth URL
# env:
# SPOTIFY_CLIENT_ID: ${{ secrets.SPOTIFY_CLIENT_ID }}
# SPOTIFY_CLIENT_SECRET: ${{ secrets.SPOTIFY_CLIENT_SECRET }}
# SPOTIFY_CLIENT_REDIRECT_URI: ${{ secrets.SPOTIFY_CLIENT_REDIRECT_URI }}
# run: |
# spotify_auth_proxy > proxy_output.log 2>&1 &
# PROXY_PID=$!
# echo "PROXY_PID=$PROXY_PID" >> $GITHUB_ENV
# # Wait for the Auth URL and API Key to appear in the log
# while ! (grep -q "Auth URL:" proxy_output.log && grep -q "APIKey:" proxy_output.log); do
# sleep 1
# done
# auth_url=$(grep "Auth URL:" proxy_output.log | awk '{print $3}')
# api_key=$(grep "APIKey:" proxy_output.log | awk '{print $2}')
# echo "AUTH_URL=$auth_url" >> $GITHUB_ENV
# echo "SPOTIFY_API_KEY=$api_key" >> $GITHUB_ENV
# - name: Setup Node.js
# uses: actions/setup-node@v3
# with:
# node-version: '18'
# - name: Install Puppeteer
# run: npm install puppeteer
# - name: Perform Spotify login
# env:
# SPOTIFY_USERNAME: ${{ secrets.SPOTIFY_USERNAME }}
# SPOTIFY_PASSWORD: ${{ secrets.SPOTIFY_PASSWORD }}
# run: |
# node - <<EOF
# const puppeteer = require('puppeteer');
# (async () => {
# const browser = await puppeteer.launch({ headless: true });
# const page = await browser.newPage();
# try {
# await page.goto('${{ env.AUTH_URL }}');
# // Wait for login form and fill in credentials
# await page.waitForSelector('#login-username');
# await page.type('#login-username', process.env.SPOTIFY_USERNAME);
# await page.type('#login-password', process.env.SPOTIFY_PASSWORD);
# // Submit the form
# await page.click('#login-button');
# // Wait for redirect and authorization
# await page.waitForNavigation({ waitUntil: 'networkidle0' });
# // Check for successful authorization
# const content = await page.content();
# if (content.includes('Authorization successful')) {
# console.log('Spotify authorization successful');
# } else {
# throw new Error('Authorization not successful');
# }
# } catch (error) {
# console.error('Error during Spotify login:', error);
# process.exit(1);
# } finally {
# await browser.close();
# }
# })();
# EOF
# - name: Run Terraform Apply
# run: |
# terraform init
# terraform apply -var="SPOTIFY_API_KEY=${{ env.SPOTIFY_API_KEY }}" -auto-approve
name: Update playlist with Spotify Auth
on:
push:
branches:
- main
jobs:
spotify-auth-and-apply:
runs-on: ubuntu-latest
name: Apply
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Setup Go
uses: actions/setup-go@v5
with:
go-version: '1.18.0'
- name: Install spotify_auth_proxy
run: go install github.com/conradludgate/terraform-provider-spotify/spotify_auth_proxy@latest
- name: Run spotify_auth_proxy and capture Auth URL
env:
SPOTIFY_CLIENT_ID: ${{ secrets.SPOTIFY_CLIENT_ID }}
SPOTIFY_CLIENT_SECRET: ${{ secrets.SPOTIFY_CLIENT_SECRET }}
SPOTIFY_CLIENT_REDIRECT_URI: ${{ secrets.SPOTIFY_CLIENT_REDIRECT_URI }}
run: |
spotify_auth_proxy > proxy_output.log 2>&1 &
PROXY_PID=$!
echo "PROXY_PID=$PROXY_PID" >> $GITHUB_ENV
while ! (grep -q "Auth URL:" proxy_output.log && grep -q "APIKey:" proxy_output.log); do
sleep 1
done
auth_url=$(grep "Auth URL:" proxy_output.log | awk '{print $3}')
api_key=$(grep "APIKey:" proxy_output.log | awk '{print $2}')
echo "AUTH_URL=$auth_url" >> $GITHUB_ENV
echo "SPOTIFY_API_KEY=$api_key" >> $GITHUB_ENV
- name: Setup Node.js
uses: actions/setup-node@v3
with:
node-version: '18'
- name: Install Puppeteer
run: npm install puppeteer
- name: Perform Spotify login and authorization
env:
SPOTIFY_USERNAME: ${{ secrets.SPOTIFY_USERNAME }}
SPOTIFY_PASSWORD: ${{ secrets.SPOTIFY_PASSWORD }}
run: |
node - <<EOF
const puppeteer = require('puppeteer');
function getRandomDelay(min, max) {
return Math.floor(Math.random() * (max - min + 1) + min);
}
function delay(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
async function typeWithRandomDelay(page, selector, text) {
for (const char of text) {
await page.type(selector, char, { delay: getRandomDelay(50, 150) });
}
}
async function waitForSelectorWithTimeout(page, selector, timeout) {
try {
await page.waitForSelector(selector, { visible: true, timeout: timeout });
return true;
} catch (error) {
if (error.name === 'TimeoutError') {
return false;
}
throw error;
}
}
(async () => {
const browser = await puppeteer.launch({
headless: true,
args: ['--no-sandbox', '--disable-setuid-sandbox', '--window-size=1920,1080'],
defaultViewport: { width: 1920, height: 1080 }
});
const page = await browser.newPage();
await page.setUserAgent('Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36');
try {
await page.goto('${{ env.AUTH_URL }}', { waitUntil: 'networkidle0' });
await page.waitForSelector('#login-username', { visible: true });
await typeWithRandomDelay(page, '#login-username', process.env.SPOTIFY_USERNAME);
await delay(getRandomDelay(500, 1500));
await typeWithRandomDelay(page, '#login-password', process.env.SPOTIFY_PASSWORD);
await delay(getRandomDelay(1000, 2000));
await page.click('#login-button');
// Wait for either the authorization button or successful authorization
const authAcceptSelector = '#auth-accept';
const authSuccessTimeout = 20000; // 20 seconds
const isAuthAcceptPresent = await waitForSelectorWithTimeout(page, authAcceptSelector, authSuccessTimeout);
if (isAuthAcceptPresent) {
console.log('Permission request detected. Accepting...');
await page.click(authAcceptSelector);
await page.waitForNavigation({ waitUntil: 'networkidle0', timeout: 30000 });
}
const content = await page.content();
if (content.includes('Authorization successful') || content.includes('callback?code=')) {
console.log('Spotify authorization successful');
} else {
throw new Error('Authorization not successful');
}
} catch (error) {
console.error('Error during Spotify login or authorization:', error);
process.exit(1);
} finally {
await browser.close();
}
})();
EOF
- name: Run Terraform Apply
run: |
terraform init
terraform apply -var="SPOTIFY_API_KEY=${{ env.SPOTIFY_API_KEY }}" -auto-approve