├── .dockerignore ├── .gitignore ├── Cargo.lock ├── Cargo.toml ├── Dockerfile ├── LICENSE ├── Makefile ├── README.md ├── data └── downloads │ └── .gitkeep ├── docker-compose.yml ├── schema.sql ├── src ├── albums.rs ├── bin │ └── spotify_backup.rs ├── cli │ └── mod.rs ├── config.rs ├── lib.rs ├── playlists.rs ├── serialize.rs ├── server │ ├── daemon.rs │ ├── db.rs │ └── mod.rs └── spotify │ ├── auth.rs │ ├── client.rs │ └── mod.rs ├── static ├── fonts │ ├── roboto-v20-latin-300.woff │ ├── roboto-v20-latin-300.woff2 │ ├── roboto-v20-latin-300italic.woff │ ├── roboto-v20-latin-300italic.woff2 │ ├── roboto-v20-latin-700.woff │ ├── roboto-v20-latin-700.woff2 │ ├── roboto-v20-latin-700italic.woff │ └── roboto-v20-latin-700italic.woff2 ├── miligram.css └── roboto.css ├── templates ├── backup.html ├── base.html └── index.html └── tests └── full_backup.rs /.dockerignore: -------------------------------------------------------------------------------- 1 | target/ 2 | !target/release/spotify_backup 3 | .git 4 | 5 | 6 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | **/*.rs.bk 3 | .idea 4 | .spotify_token_cache.json 5 | backup-requests.db 6 | data/downloads/*.json 7 | -------------------------------------------------------------------------------- /Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "spotify-backup" 3 | version = "0.1.0" 4 | authors = ["Simão Mata "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | serde = { version = "1.0", features = ["derive"] } 11 | rspotify = "0.8" 12 | serde_json = "1.0" 13 | actix-rt = "1.0" 14 | actix-web = "2.0" 15 | actix-files = "0.2" 16 | actix-session = "0.3" 17 | log = "0.4" 18 | pretty_env_logger = "0.3" 19 | futures = "0.3" 20 | futures-util = "0.3" 21 | r2d2 = "0.8" 22 | r2d2_sqlite = "0.12" 23 | rusqlite = { version = "0.20", features = ["uuid", "functions"] } 24 | uuid = { version = "0.8", features = ["serde", "v4"] } 25 | time = "0.1" 26 | failure = "0.1.6" 27 | toml = "0.5" 28 | 29 | [dependencies.tera] 30 | version = "1" 31 | default-features = false 32 | 33 | [dev-dependencies] 34 | mockall = "0.5.0" 35 | tempfile = "3.1.0" 36 | lazy_static = "1.4.0" 37 | 38 | [patch.crates-io] 39 | rspotify = { git = "https://github.com/ramsayleung/rspotify" } 40 | -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | FROM debian:stable-slim 2 | 3 | RUN apt-get update && apt-get upgrade --yes && apt-get install --yes libsqlite3-dev libssl-dev ca-certificates 4 | 5 | WORKDIR /opt/spotify-backup 6 | 7 | ENV PATH=${PATH}:/opt/spotify-backup 8 | 9 | COPY target/release/spotify_backup /opt/spotify-backup/spotify-backup 10 | 11 | COPY static /opt/spotify-backup/static 12 | COPY templates /opt/spotify-backup/templates 13 | 14 | EXPOSE 8000 15 | 16 | CMD ["/opt/spotify-backup/spotify-backup"] 17 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Mozilla Public License Version 2.0 2 | ================================== 3 | 4 | 1. Definitions 5 | -------------- 6 | 7 | 1.1. "Contributor" 8 | means each individual or legal entity that creates, contributes to 9 | the creation of, or owns Covered Software. 10 | 11 | 1.2. "Contributor Version" 12 | means the combination of the Contributions of others (if any) used 13 | by a Contributor and that particular Contributor's Contribution. 14 | 15 | 1.3. "Contribution" 16 | means Covered Software of a particular Contributor. 17 | 18 | 1.4. "Covered Software" 19 | means Source Code Form to which the initial Contributor has attached 20 | the notice in Exhibit A, the Executable Form of such Source Code 21 | Form, and Modifications of such Source Code Form, in each case 22 | including portions thereof. 23 | 24 | 1.5. "Incompatible With Secondary Licenses" 25 | means 26 | 27 | (a) that the initial Contributor has attached the notice described 28 | in Exhibit B to the Covered Software; or 29 | 30 | (b) that the Covered Software was made available under the terms of 31 | version 1.1 or earlier of the License, but not also under the 32 | terms of a Secondary License. 33 | 34 | 1.6. "Executable Form" 35 | means any form of the work other than Source Code Form. 36 | 37 | 1.7. "Larger Work" 38 | means a work that combines Covered Software with other material, in 39 | a separate file or files, that is not Covered Software. 40 | 41 | 1.8. "License" 42 | means this document. 43 | 44 | 1.9. "Licensable" 45 | means having the right to grant, to the maximum extent possible, 46 | whether at the time of the initial grant or subsequently, any and 47 | all of the rights conveyed by this License. 48 | 49 | 1.10. "Modifications" 50 | means any of the following: 51 | 52 | (a) any file in Source Code Form that results from an addition to, 53 | deletion from, or modification of the contents of Covered 54 | Software; or 55 | 56 | (b) any new file in Source Code Form that contains any Covered 57 | Software. 58 | 59 | 1.11. "Patent Claims" of a Contributor 60 | means any patent claim(s), including without limitation, method, 61 | process, and apparatus claims, in any patent Licensable by such 62 | Contributor that would be infringed, but for the grant of the 63 | License, by the making, using, selling, offering for sale, having 64 | made, import, or transfer of either its Contributions or its 65 | Contributor Version. 66 | 67 | 1.12. "Secondary License" 68 | means either the GNU General Public License, Version 2.0, the GNU 69 | Lesser General Public License, Version 2.1, the GNU Affero General 70 | Public License, Version 3.0, or any later versions of those 71 | licenses. 72 | 73 | 1.13. "Source Code Form" 74 | means the form of the work preferred for making modifications. 75 | 76 | 1.14. "You" (or "Your") 77 | means an individual or a legal entity exercising rights under this 78 | License. For legal entities, "You" includes any entity that 79 | controls, is controlled by, or is under common control with You. For 80 | purposes of this definition, "control" means (a) the power, direct 81 | or indirect, to cause the direction or management of such entity, 82 | whether by contract or otherwise, or (b) ownership of more than 83 | fifty percent (50%) of the outstanding shares or beneficial 84 | ownership of such entity. 85 | 86 | 2. License Grants and Conditions 87 | -------------------------------- 88 | 89 | 2.1. Grants 90 | 91 | Each Contributor hereby grants You a world-wide, royalty-free, 92 | non-exclusive license: 93 | 94 | (a) under intellectual property rights (other than patent or trademark) 95 | Licensable by such Contributor to use, reproduce, make available, 96 | modify, display, perform, distribute, and otherwise exploit its 97 | Contributions, either on an unmodified basis, with Modifications, or 98 | as part of a Larger Work; and 99 | 100 | (b) under Patent Claims of such Contributor to make, use, sell, offer 101 | for sale, have made, import, and otherwise transfer either its 102 | Contributions or its Contributor Version. 103 | 104 | 2.2. Effective Date 105 | 106 | The licenses granted in Section 2.1 with respect to any Contribution 107 | become effective for each Contribution on the date the Contributor first 108 | distributes such Contribution. 109 | 110 | 2.3. Limitations on Grant Scope 111 | 112 | The licenses granted in this Section 2 are the only rights granted under 113 | this License. No additional rights or licenses will be implied from the 114 | distribution or licensing of Covered Software under this License. 115 | Notwithstanding Section 2.1(b) above, no patent license is granted by a 116 | Contributor: 117 | 118 | (a) for any code that a Contributor has removed from Covered Software; 119 | or 120 | 121 | (b) for infringements caused by: (i) Your and any other third party's 122 | modifications of Covered Software, or (ii) the combination of its 123 | Contributions with other software (except as part of its Contributor 124 | Version); or 125 | 126 | (c) under Patent Claims infringed by Covered Software in the absence of 127 | its Contributions. 128 | 129 | This License does not grant any rights in the trademarks, service marks, 130 | or logos of any Contributor (except as may be necessary to comply with 131 | the notice requirements in Section 3.4). 132 | 133 | 2.4. Subsequent Licenses 134 | 135 | No Contributor makes additional grants as a result of Your choice to 136 | distribute the Covered Software under a subsequent version of this 137 | License (see Section 10.2) or under the terms of a Secondary License (if 138 | permitted under the terms of Section 3.3). 139 | 140 | 2.5. Representation 141 | 142 | Each Contributor represents that the Contributor believes its 143 | Contributions are its original creation(s) or it has sufficient rights 144 | to grant the rights to its Contributions conveyed by this License. 145 | 146 | 2.6. Fair Use 147 | 148 | This License is not intended to limit any rights You have under 149 | applicable copyright doctrines of fair use, fair dealing, or other 150 | equivalents. 151 | 152 | 2.7. Conditions 153 | 154 | Sections 3.1, 3.2, 3.3, and 3.4 are conditions of the licenses granted 155 | in Section 2.1. 156 | 157 | 3. Responsibilities 158 | ------------------- 159 | 160 | 3.1. Distribution of Source Form 161 | 162 | All distribution of Covered Software in Source Code Form, including any 163 | Modifications that You create or to which You contribute, must be under 164 | the terms of this License. You must inform recipients that the Source 165 | Code Form of the Covered Software is governed by the terms of this 166 | License, and how they can obtain a copy of this License. You may not 167 | attempt to alter or restrict the recipients' rights in the Source Code 168 | Form. 169 | 170 | 3.2. Distribution of Executable Form 171 | 172 | If You distribute Covered Software in Executable Form then: 173 | 174 | (a) such Covered Software must also be made available in Source Code 175 | Form, as described in Section 3.1, and You must inform recipients of 176 | the Executable Form how they can obtain a copy of such Source Code 177 | Form by reasonable means in a timely manner, at a charge no more 178 | than the cost of distribution to the recipient; and 179 | 180 | (b) You may distribute such Executable Form under the terms of this 181 | License, or sublicense it under different terms, provided that the 182 | license for the Executable Form does not attempt to limit or alter 183 | the recipients' rights in the Source Code Form under this License. 184 | 185 | 3.3. Distribution of a Larger Work 186 | 187 | You may create and distribute a Larger Work under terms of Your choice, 188 | provided that You also comply with the requirements of this License for 189 | the Covered Software. If the Larger Work is a combination of Covered 190 | Software with a work governed by one or more Secondary Licenses, and the 191 | Covered Software is not Incompatible With Secondary Licenses, this 192 | License permits You to additionally distribute such Covered Software 193 | under the terms of such Secondary License(s), so that the recipient of 194 | the Larger Work may, at their option, further distribute the Covered 195 | Software under the terms of either this License or such Secondary 196 | License(s). 197 | 198 | 3.4. Notices 199 | 200 | You may not remove or alter the substance of any license notices 201 | (including copyright notices, patent notices, disclaimers of warranty, 202 | or limitations of liability) contained within the Source Code Form of 203 | the Covered Software, except that You may alter any license notices to 204 | the extent required to remedy known factual inaccuracies. 205 | 206 | 3.5. Application of Additional Terms 207 | 208 | You may choose to offer, and to charge a fee for, warranty, support, 209 | indemnity or liability obligations to one or more recipients of Covered 210 | Software. However, You may do so only on Your own behalf, and not on 211 | behalf of any Contributor. You must make it absolutely clear that any 212 | such warranty, support, indemnity, or liability obligation is offered by 213 | You alone, and You hereby agree to indemnify every Contributor for any 214 | liability incurred by such Contributor as a result of warranty, support, 215 | indemnity or liability terms You offer. You may include additional 216 | disclaimers of warranty and limitations of liability specific to any 217 | jurisdiction. 218 | 219 | 4. Inability to Comply Due to Statute or Regulation 220 | --------------------------------------------------- 221 | 222 | If it is impossible for You to comply with any of the terms of this 223 | License with respect to some or all of the Covered Software due to 224 | statute, judicial order, or regulation then You must: (a) comply with 225 | the terms of this License to the maximum extent possible; and (b) 226 | describe the limitations and the code they affect. Such description must 227 | be placed in a text file included with all distributions of the Covered 228 | Software under this License. Except to the extent prohibited by statute 229 | or regulation, such description must be sufficiently detailed for a 230 | recipient of ordinary skill to be able to understand it. 231 | 232 | 5. Termination 233 | -------------- 234 | 235 | 5.1. The rights granted under this License will terminate automatically 236 | if You fail to comply with any of its terms. However, if You become 237 | compliant, then the rights granted under this License from a particular 238 | Contributor are reinstated (a) provisionally, unless and until such 239 | Contributor explicitly and finally terminates Your grants, and (b) on an 240 | ongoing basis, if such Contributor fails to notify You of the 241 | non-compliance by some reasonable means prior to 60 days after You have 242 | come back into compliance. Moreover, Your grants from a particular 243 | Contributor are reinstated on an ongoing basis if such Contributor 244 | notifies You of the non-compliance by some reasonable means, this is the 245 | first time You have received notice of non-compliance with this License 246 | from such Contributor, and You become compliant prior to 30 days after 247 | Your receipt of the notice. 248 | 249 | 5.2. If You initiate litigation against any entity by asserting a patent 250 | infringement claim (excluding declaratory judgment actions, 251 | counter-claims, and cross-claims) alleging that a Contributor Version 252 | directly or indirectly infringes any patent, then the rights granted to 253 | You by any and all Contributors for the Covered Software under Section 254 | 2.1 of this License shall terminate. 255 | 256 | 5.3. In the event of termination under Sections 5.1 or 5.2 above, all 257 | end user license agreements (excluding distributors and resellers) which 258 | have been validly granted by You or Your distributors under this License 259 | prior to termination shall survive termination. 260 | 261 | ************************************************************************ 262 | * * 263 | * 6. Disclaimer of Warranty * 264 | * ------------------------- * 265 | * * 266 | * Covered Software is provided under this License on an "as is" * 267 | * basis, without warranty of any kind, either expressed, implied, or * 268 | * statutory, including, without limitation, warranties that the * 269 | * Covered Software is free of defects, merchantable, fit for a * 270 | * particular purpose or non-infringing. The entire risk as to the * 271 | * quality and performance of the Covered Software is with You. * 272 | * Should any Covered Software prove defective in any respect, You * 273 | * (not any Contributor) assume the cost of any necessary servicing, * 274 | * repair, or correction. This disclaimer of warranty constitutes an * 275 | * essential part of this License. No use of any Covered Software is * 276 | * authorized under this License except under this disclaimer. * 277 | * * 278 | ************************************************************************ 279 | 280 | ************************************************************************ 281 | * * 282 | * 7. Limitation of Liability * 283 | * -------------------------- * 284 | * * 285 | * Under no circumstances and under no legal theory, whether tort * 286 | * (including negligence), contract, or otherwise, shall any * 287 | * Contributor, or anyone who distributes Covered Software as * 288 | * permitted above, be liable to You for any direct, indirect, * 289 | * special, incidental, or consequential damages of any character * 290 | * including, without limitation, damages for lost profits, loss of * 291 | * goodwill, work stoppage, computer failure or malfunction, or any * 292 | * and all other commercial damages or losses, even if such party * 293 | * shall have been informed of the possibility of such damages. This * 294 | * limitation of liability shall not apply to liability for death or * 295 | * personal injury resulting from such party's negligence to the * 296 | * extent applicable law prohibits such limitation. Some * 297 | * jurisdictions do not allow the exclusion or limitation of * 298 | * incidental or consequential damages, so this exclusion and * 299 | * limitation may not apply to You. * 300 | * * 301 | ************************************************************************ 302 | 303 | 8. Litigation 304 | ------------- 305 | 306 | Any litigation relating to this License may be brought only in the 307 | courts of a jurisdiction where the defendant maintains its principal 308 | place of business and such litigation shall be governed by laws of that 309 | jurisdiction, without reference to its conflict-of-law provisions. 310 | Nothing in this Section shall prevent a party's ability to bring 311 | cross-claims or counter-claims. 312 | 313 | 9. Miscellaneous 314 | ---------------- 315 | 316 | This License represents the complete agreement concerning the subject 317 | matter hereof. If any provision of this License is held to be 318 | unenforceable, such provision shall be reformed only to the extent 319 | necessary to make it enforceable. Any law or regulation which provides 320 | that the language of a contract shall be construed against the drafter 321 | shall not be used to construe this License against a Contributor. 322 | 323 | 10. Versions of the License 324 | --------------------------- 325 | 326 | 10.1. New Versions 327 | 328 | Mozilla Foundation is the license steward. Except as provided in Section 329 | 10.3, no one other than the license steward has the right to modify or 330 | publish new versions of this License. Each version will be given a 331 | distinguishing version number. 332 | 333 | 10.2. Effect of New Versions 334 | 335 | You may distribute the Covered Software under the terms of the version 336 | of the License under which You originally received the Covered Software, 337 | or under the terms of any subsequent version published by the license 338 | steward. 339 | 340 | 10.3. Modified Versions 341 | 342 | If you create software not governed by this License, and you want to 343 | create a new license for such software, you may create and use a 344 | modified version of this License if you rename the license and remove 345 | any references to the name of the license steward (except to note that 346 | such modified license differs from this License). 347 | 348 | 10.4. Distributing Source Code Form that is Incompatible With Secondary 349 | Licenses 350 | 351 | If You choose to distribute Source Code Form that is Incompatible With 352 | Secondary Licenses under the terms of this version of the License, the 353 | notice described in Exhibit B of this License must be attached. 354 | 355 | Exhibit A - Source Code Form License Notice 356 | ------------------------------------------- 357 | 358 | This Source Code Form is subject to the terms of the Mozilla Public 359 | License, v. 2.0. If a copy of the MPL was not distributed with this 360 | file, You can obtain one at http://mozilla.org/MPL/2.0/. 361 | 362 | If it is not possible or desirable to put the notice in a particular 363 | file, then You may include the notice in a location (such as a LICENSE 364 | file in a relevant directory) where a recipient would be likely to look 365 | for such a notice. 366 | 367 | You may add additional accurate notices of copyright ownership. 368 | 369 | Exhibit B - "Incompatible With Secondary Licenses" Notice 370 | --------------------------------------------------------- 371 | 372 | This Source Code Form is "Incompatible With Secondary Licenses", as 373 | defined by the Mozilla Public License, v. 2.0. 374 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | all: spotify-backup.docker.tar.zst 2 | 3 | db: 4 | sqlite3 data/backup-requests.db < schema.sql 5 | 6 | unit-test: 7 | cargo test -- --skip full_backup 8 | 9 | target/release/spotify_backup: src/ 10 | cargo build --release 11 | 12 | spotify-backup.docker.tar.zst: Dockerfile target/release/spotify_backup 13 | docker build . -t simao/spotify-backup:latest 14 | docker save simao/spotify-backup:latest | zstdmt > spotify-backup.docker.tar.zst 15 | 16 | push: spotify-backup.docker.tar.zst 17 | rsync -a --progress spotify-backup.docker.tar.zst docker-compose.yml sm-data@0io.eu:spotify-backup/ 18 | e rsync -a --progress spotify-backup.example.toml sm-data@0io.eu:spotify-backup/spotify-backup.toml 19 | 20 | install: push 21 | ssh simao@0io.eu "zstdcat /home/sm-data/spotify-backup/spotify-backup.docker.tar.zst | docker image load" 22 | 23 | docker-run: docker 24 | docker run -it --publish 8000:8000 --volume $(pwd):/tmp/spotify-backup -e DATA_DIR=/tmp/spotify-backup -e RUST_LOG=info,spotify_backup=debug simao/spotify-backup:latest server 25 | 26 | clean: 27 | rm spotify-backup.docker.tar.zst 28 | 29 | .PHONY: docker docker-run install push 30 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Spotify Backup Tool 2 | 3 | Tool to backup your spotify albums and playlists into a single json file. This is useful if you just want to backup your data or want to run some analysis on the songs you have on your library. 4 | 5 | This app can run in two modes: 6 | 7 | 1. A CLI tool that prints your spotify data to stdout 8 | 9 | 2. A web app that accepts that redirects the user to spotify to get authorization and then provides the user with a json backup of the data. 10 | 11 | A working instance of this app is running at [0io.eu/spotify-backup](https://0io.eu/spotify-backup/). 12 | 13 | ## Running the CLI tool 14 | 15 | You'll need to create an application with [Spotify](https://developer.spotify.com/). You'll need to add `http://localhost:8000` as a callback url. Get a client id and client secret and then run this app with: 16 | 17 | CLIENT_ID= CLIENT_SECRET= RUST_LOG=info,spotify_backup=debug cargo run > backup.json 18 | 19 | Follow the instructions to allow the app to access your data. You can then delete your Spotify App your just unauthorize your user. 20 | 21 | ## Running the web tool 22 | 23 | You'll need to run both the frontend and backend app. You can have a `env` file that exports `CLIENT_ID` and `CLIENT_SECRET`. You can also change some settings like the base uri used by your app, setup in the [dev console](https://developer.spotify.com/), in `spotify-backup.toml`, there is an example file in the root directory. 24 | 25 | source env 26 | RUST_LOG=info,spotify_backup=info cargo -- server 27 | 28 | In a separate process run: 29 | 30 | source env 31 | RUST_LOG=info,spotify_backup=info cargo -- worker 32 | 33 | A Dockerfile and a docker-compose file are provided but you'll need to build your own images. 34 | -------------------------------------------------------------------------------- /data/downloads/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/simao/spotify-backup/5aa3b27fd4af52e8205a2bfa7e1c986c24b282f8/data/downloads/.gitkeep -------------------------------------------------------------------------------- /docker-compose.yml: -------------------------------------------------------------------------------- 1 | version: '3' 2 | services: 3 | frontend: 4 | image: simao/spotify-backup:latest 5 | container_name: spotify-backup-frontend 6 | restart: always 7 | environment: 8 | - CLIENT_ID=dccc26891bde4aea9b1ff3ee8ca6abcb 9 | - CLIENT_SECRET=d68dd4433bb94bde933acde5ae790702 10 | - RUST_LOG=info,spotify_backup=debug 11 | command: spotify-backup server 12 | volumes: 13 | - /home/sm-data/spotify-backup/data:/opt/spotify-backup/data 14 | - /home/sm-data/spotify-backup/spotify-backup.toml:/opt/spotify-backup/spotify-backup.toml 15 | # - /home/simao/code/spotify-backup/data:/opt/spotify-backup/data 16 | # - /home/simao/code/spotify-backup/spotify-backup.example.toml:/opt/spotify-backup/spotify-backup.toml 17 | ports: 18 | - "5555:8000" 19 | 20 | backend: 21 | image: simao/spotify-backup:latest 22 | container_name: spotify-backup-backend 23 | restart: always 24 | volumes: 25 | - /home/sm-data/spotify-backup/data:/opt/spotify-backup/data 26 | - /home/sm-data/spotify-backup/spotify-backup.toml:/opt/spotify-backup/spotify-backup.toml 27 | # - /home/simao/code/spotify-backup/data:/opt/spotify-backup/data 28 | # - /home/simao/code/spotify-backup/spotify-backup.example.toml:/opt/spotify-backup/spotify-backup.toml 29 | command: spotify-backup daemon 30 | environment: 31 | - CLIENT_ID=dccc26891bde4aea9b1ff3ee8ca6abcb 32 | - CLIENT_SECRET=d68dd4433bb94bde933acde5ae790702 33 | - RUST_LOG=info,spotify_backup=info 34 | -------------------------------------------------------------------------------- /schema.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE backup_requests ( 2 | id TEXT PRIMARY KEY, 3 | token TEXT NOT NULL, 4 | created_at TEXT NOT NULL, 5 | file TEXT, 6 | last_error TEXT, 7 | status TEXT NOT NULL 8 | ) 9 | ; 10 | -------------------------------------------------------------------------------- /src/albums.rs: -------------------------------------------------------------------------------- 1 | use crate::serialize::*; 2 | use crate::spotify::client::*; 3 | use failure::Error; 4 | 5 | pub fn backup_albums(spotify: &dyn SpotifyClient) -> Result, Error> { 6 | let mut parsed_albums = vec![]; 7 | let mut offset = 0; 8 | 9 | loop { 10 | let albums = spotify.saved_albums(Some(50), Some(offset))?; 11 | 12 | parsed_albums.extend(albums.items); 13 | 14 | if albums.next.is_none() { 15 | break; 16 | } else { 17 | offset = albums.offset + 50; 18 | } 19 | } 20 | 21 | Ok(parsed_albums) 22 | } 23 | 24 | #[cfg(test)] 25 | mod tests { 26 | use super::*; 27 | 28 | use mockall::predicate::*; 29 | use mockall::*; 30 | use rspotify::spotify::model::page::Page; 31 | use std::iter; 32 | 33 | mock! { 34 | pub SpotifyClientM { } 35 | trait SpotifyClient { 36 | fn saved_albums( 37 | &self, 38 | limit: Option, 39 | offset: Option 40 | ) -> Result, Error>; 41 | 42 | fn playlists( 43 | &self, 44 | user_id: &str, 45 | limit: Option, 46 | offset: Option, 47 | ) -> Result, Error>; 48 | 49 | fn playlist( 50 | &self, 51 | playlist_id: &PlaylistId, 52 | ) -> Result<(Playlist, Page), Error>; 53 | 54 | fn playlist_tracks( 55 | &self, 56 | user_id: &str, 57 | playlist_id: &PlaylistId, 58 | limit: Option, 59 | offset: Option, 60 | ) -> Result, Error>; 61 | } 62 | } 63 | 64 | fn new_album() -> Album { 65 | let fake_artist = Artist { 66 | name: "Fernando Pessoa".into(), 67 | }; 68 | 69 | Album { 70 | title: "Livro do desassossego".into(), 71 | artists: vec![fake_artist], 72 | album_type: None, 73 | release_date: None, 74 | } 75 | } 76 | 77 | fn new_page(size: Option, next: Option) -> Page { 78 | let albums: Vec = iter::repeat_with(|| new_album()) 79 | .take(size.unwrap_or(1)) 80 | .collect(); 81 | 82 | Page { 83 | href: "https://...".into(), 84 | items: albums, 85 | limit: 1, 86 | offset: 0, 87 | previous: None, 88 | total: 1, 89 | next, 90 | } 91 | } 92 | 93 | #[test] 94 | fn test_backup_albums() { 95 | let mut mock = MockSpotifyClientM::new(); 96 | 97 | mock.expect_saved_albums() 98 | .with(eq(Some(50)), eq(Some(0))) 99 | .times(1) 100 | .returning(|_, _| Ok(new_page(None, None))); 101 | 102 | let backed_up_albums = backup_albums(&mock).unwrap(); 103 | 104 | let p = new_page(None, None); 105 | 106 | assert_eq!(backed_up_albums, p.items); 107 | } 108 | 109 | #[test] 110 | fn test_backup_multiple_pages() { 111 | let mut mock = MockSpotifyClientM::new(); 112 | 113 | mock.expect_saved_albums() 114 | .with(eq(Some(50)), eq(Some(0))) 115 | .times(1) 116 | .returning(|_, _| Ok(new_page(Some(50), Some("next".into())))); 117 | 118 | mock.expect_saved_albums() 119 | .with(eq(Some(50)), eq(Some(50))) 120 | .times(1) 121 | .returning(|_, _| Ok(new_page(Some(3), None))); 122 | 123 | let backed_up_albums = backup_albums(&mock).unwrap(); 124 | 125 | let p = new_page(Some(53), None); 126 | 127 | assert_eq!(backed_up_albums, p.items); 128 | } 129 | } 130 | -------------------------------------------------------------------------------- /src/bin/spotify_backup.rs: -------------------------------------------------------------------------------- 1 | use std::env; 2 | use spotify_backup::config::Config; 3 | 4 | pub fn main() -> (){ 5 | pretty_env_logger::init(); 6 | 7 | let args: Vec = env::args().collect(); 8 | 9 | let config = Config::load().expect("could not load config"); 10 | 11 | log::debug!("using config: {}", config); 12 | 13 | if args.len() == 2 && args[1] == "server" { 14 | spotify_backup::server::server(config).unwrap(); 15 | } else if args.len() == 2 && args[1] == "daemon" { 16 | spotify_backup::server::daemon::daemon(config); 17 | } else { 18 | spotify_backup::cli::cli(); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/cli/mod.rs: -------------------------------------------------------------------------------- 1 | use rspotify::spotify::util::get_token; 2 | use crate::spotify::*; 3 | use crate::backup_fn::DefaultBackup; 4 | use std::path::PathBuf; 5 | 6 | pub fn cli() { 7 | // Needs env file 8 | let mut oauth = build_spotify_oauth("http://localhost:8000", PathBuf::from("./spotify_token_cache.json")); 9 | 10 | match get_token(&mut oauth) { 11 | Some(token_info) => { 12 | let backup = DefaultBackup::run_backup(token_info).unwrap(); 13 | 14 | let serialized = serde_json::to_string_pretty(&backup).unwrap(); 15 | 16 | println!("{}", serialized); 17 | 18 | log::info!( 19 | "Saved {} albums, {} playlists", 20 | backup.albums.len(), 21 | backup.playlists.len() 22 | ); 23 | } 24 | None => log::error!("auth failed"), 25 | }; 26 | } 27 | -------------------------------------------------------------------------------- /src/config.rs: -------------------------------------------------------------------------------- 1 | use std::path::PathBuf; 2 | use actix_web::http::Uri; 3 | use serde::{Serialize, Deserialize, Serializer, Deserializer}; 4 | use std::env; 5 | use std::fmt::Display; 6 | use failure::_core::fmt::{Formatter, Error}; 7 | use std::io::Read; 8 | 9 | #[derive(Debug, Serialize, Deserialize)] 10 | pub struct Config { 11 | data_dir: PathBuf, 12 | pub worker_count: u32, 13 | base_uri: ConfigUri, 14 | } 15 | 16 | #[derive(Debug)] 17 | struct ConfigUri(Uri); 18 | 19 | 20 | impl Default for Config { 21 | fn default() -> Self { 22 | let data_dir = PathBuf::from(env::var("DATA_DIR").ok().unwrap_or("./data".to_owned())); 23 | let uri: Uri = env::var("BASE_URL").ok().unwrap_or("http://localhost:8000/".into()).parse::().expect("Could not parse BASE_URL"); 24 | let worker_count = env::var("WORKER_COUNT").unwrap_or("1".into()).parse::().expect("Could not parse WORKER_COUNT"); 25 | 26 | Config { 27 | data_dir, 28 | worker_count, 29 | base_uri: ConfigUri(uri) 30 | } 31 | } 32 | } 33 | 34 | impl Serialize for ConfigUri { 35 | fn serialize(&self, serializer: S) -> Result<::Ok, ::Error> where 36 | S: Serializer { 37 | serializer.serialize_str(&self.0.to_string()) 38 | } 39 | } 40 | 41 | impl<'de> Deserialize<'de> for ConfigUri { 42 | fn deserialize(deserializer: D) -> Result where 43 | D: Deserializer<'de> { 44 | let s: String = serde::Deserialize::deserialize(deserializer)?; 45 | s.parse::() 46 | .map(|u| ConfigUri(u)) 47 | .map_err(|err| serde::de::Error::custom(format!("Invalid value for base_uri : {}", err))) 48 | } 49 | } 50 | 51 | impl Config { 52 | pub fn load() -> Result { 53 | let config_path = PathBuf::from("./spotify-backup.toml"); 54 | 55 | match std::fs::File::open(config_path) { 56 | Ok(mut cfg) => { 57 | let mut c = String::new(); 58 | cfg.read_to_string(&mut c)?; 59 | toml::from_str(&c).map_err(failure::Error::from) 60 | }, 61 | Err(ref e) if e.kind() == std::io::ErrorKind::NotFound => { 62 | Ok(Config::default()) 63 | }, 64 | Err(err) => Err(err.into()) 65 | } 66 | } 67 | 68 | pub fn token_cache_path(&self) -> PathBuf { self.data_dir.join(".spotify_token_cache.json") } 69 | 70 | pub fn downloads_path(&self) -> PathBuf { self.data_dir.join("downloads") } 71 | 72 | pub fn db_path(&self) -> PathBuf { self.data_dir.join("backup-requests.db") } 73 | 74 | pub fn base_uri(&self) -> Uri { self.base_uri.0.clone() } 75 | } 76 | 77 | impl Display for Config { 78 | fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> { 79 | write!(f, "data_dir={:?}, worker_count={}, token_cache_path={:?}, downloads_path={:?}, db_path={:?}, base_uri={}", 80 | self.data_dir, 81 | self.worker_count, 82 | self.token_cache_path(), 83 | self.downloads_path(), 84 | self.db_path(), 85 | self.base_uri() 86 | ) 87 | } 88 | } -------------------------------------------------------------------------------- /src/lib.rs: -------------------------------------------------------------------------------- 1 | use rspotify::spotify::client::Spotify; 2 | use failure::Error; 3 | 4 | mod playlists; 5 | mod albums; 6 | mod serialize; 7 | mod spotify; 8 | 9 | pub mod server; 10 | pub mod cli; 11 | pub mod config; 12 | 13 | pub mod backup_fn { 14 | use super::*; 15 | use rspotify::spotify::oauth2::{SpotifyClientCredentials, TokenInfo}; 16 | use crate::serialize::*; 17 | 18 | pub trait BackupFn { 19 | fn apply(&self, token_info: TokenInfo) -> Result; 20 | } 21 | 22 | pub struct DefaultBackup; 23 | 24 | impl DefaultBackup { 25 | pub fn run_backup(token_info: TokenInfo) -> Result { 26 | let client_credential = SpotifyClientCredentials::default() 27 | .token_info(token_info) 28 | .build(); 29 | 30 | let spotify = Spotify::default() 31 | .client_credentials_manager(client_credential) 32 | .build(); 33 | 34 | let user_id = spotify.me()?.id; 35 | 36 | Self::full_backup(&user_id, &spotify) 37 | } 38 | 39 | pub fn full_backup(user_id: &str, spotify: &Spotify) -> Result { 40 | let albums = albums::backup_albums(spotify)?; 41 | 42 | let playlists = playlists::backup_playlists(&user_id, spotify)?; 43 | 44 | Ok(Backup { 45 | albums, 46 | playlists, 47 | }) 48 | } 49 | } 50 | 51 | impl BackupFn for DefaultBackup { 52 | fn apply(&self, token_info: TokenInfo) -> Result { 53 | DefaultBackup::run_backup(token_info) 54 | } 55 | } 56 | } 57 | 58 | -------------------------------------------------------------------------------- /src/playlists.rs: -------------------------------------------------------------------------------- 1 | use rspotify::spotify::model::page::Page; 2 | use failure::Error; 3 | 4 | use crate::serialize::*; 5 | use crate::spotify::client::*; 6 | 7 | fn extract_playlists( 8 | user_id: &str, 9 | spotify: &dyn SpotifyClient, 10 | dest: &mut Vec, 11 | playlists: Vec<(Playlist, Page)>, 12 | ) -> Result<(), Error> { 13 | for (p, page) in &playlists { 14 | log::debug!("Parsing playlist {:?}", p.name); 15 | 16 | let mut tracks = vec![]; 17 | let mut next_page: Page = page.clone(); 18 | 19 | loop { 20 | tracks.append(&mut next_page.items); 21 | 22 | let offset = next_page.offset + next_page.limit; 23 | 24 | if next_page.next.is_some() { 25 | next_page = spotify 26 | .playlist_tracks(user_id, &p.id, Some(next_page.limit), Some(offset))?; 27 | } else { 28 | break; 29 | } 30 | } 31 | 32 | let track_count = tracks.len(); 33 | 34 | let parsed_playlist = Playlist { 35 | id: p.id.clone(), 36 | name: p.name.clone(), 37 | tracks, 38 | track_count, 39 | }; 40 | 41 | dest.push(parsed_playlist); 42 | } 43 | 44 | Ok(()) 45 | } 46 | 47 | fn get_full_playlists( 48 | spotify: &dyn SpotifyClient, 49 | playlist_ids: Vec, 50 | ) -> Result)>, Error> { 51 | playlist_ids 52 | .iter() 53 | .map(|p| spotify.playlist(&p)) 54 | .collect() 55 | } 56 | 57 | pub fn backup_playlists(user_id: &str, spotify: &dyn SpotifyClient) -> Result, Error> { 58 | let mut parsed_playlists: Vec = vec![]; 59 | let mut offset = 0; 60 | 61 | loop { 62 | let playlists = spotify.playlists(user_id, Some(50), Some(offset))?; 63 | let full_playlists = get_full_playlists(spotify, playlists.items)?; 64 | extract_playlists(user_id, spotify, &mut parsed_playlists, full_playlists)?; 65 | 66 | if playlists.next.is_none() { 67 | break; 68 | } else { 69 | offset = offset + 50; 70 | } 71 | } 72 | 73 | Ok(parsed_playlists) 74 | } 75 | 76 | #[cfg(test)] 77 | mod tests { 78 | use super::*; 79 | use mockall::predicate::*; 80 | use mockall::*; 81 | use rspotify::spotify::model::page::Page; 82 | 83 | mock! { 84 | pub SpotifyClientM { } 85 | trait SpotifyClient { 86 | fn saved_albums( 87 | &self, 88 | limit: Option, 89 | offset: Option 90 | ) -> Result, Error>; 91 | 92 | fn playlists( 93 | &self, 94 | user_id: &str, 95 | limit: Option, 96 | offset: Option, 97 | ) -> Result, Error>; 98 | 99 | fn playlist( 100 | &self, 101 | playlist_id: &PlaylistId, 102 | ) -> Result<(Playlist, Page), Error>; 103 | 104 | fn playlist_tracks( 105 | &self, 106 | user_id: &str, 107 | playlist_id: &PlaylistId, 108 | limit: Option, 109 | offset: Option, 110 | ) -> Result, Error>; 111 | } 112 | } 113 | 114 | fn new_page(items: Vec, offset: u32, total: u32, next: Option) -> Page { 115 | Page { 116 | href: "https://...".into(), 117 | items, 118 | limit: 50, 119 | offset, 120 | previous: None, 121 | total, 122 | next, 123 | } 124 | } 125 | 126 | fn new_track(name: &str) -> Track { 127 | let artist = Artist { 128 | name: format!("Artist: {}", name), 129 | }; 130 | 131 | let album = Album { 132 | title: format!("Album: {}", name), 133 | artists: vec![artist], 134 | album_type: None, 135 | release_date: None, 136 | }; 137 | 138 | Track { 139 | name: format!("Track: {}", name), 140 | album, 141 | } 142 | } 143 | 144 | #[test] 145 | fn test_backup_playlists_empty() { 146 | let mut mock = MockSpotifyClientM::new(); 147 | 148 | mock.expect_playlists() 149 | .with(eq("myuser"), eq(Some(50)), eq(Some(0))) 150 | .times(1) 151 | .returning(|_, _, _| Ok(new_page(vec![], 0, 0, None))); 152 | 153 | let backup = backup_playlists("myuser", &mock).unwrap(); 154 | assert_eq!(backup.len(), 0) 155 | } 156 | 157 | #[test] 158 | fn test_backup_playlists_single_playlist() { 159 | let mut mock = MockSpotifyClientM::new(); 160 | 161 | mock.expect_playlists() 162 | .with(eq("myuser"), eq(Some(50)), eq(Some(0))) 163 | .times(1) 164 | .returning(|_, _, _| Ok(new_page(vec!["playlist-id-01".into()], 0, 1, None))); 165 | 166 | mock.expect_playlist_tracks() 167 | .with( 168 | eq("myuser"), 169 | eq("playlist-id-01".to_owned()), 170 | eq(Some(50)), 171 | eq(Some(50)), 172 | ) 173 | .times(1) 174 | .returning(|_, _, _, _| { 175 | let track = new_track("track from `playlist_tracks`"); 176 | Ok(new_page(vec![track], 1, 1, None)) 177 | }); 178 | 179 | mock.expect_playlist() 180 | .with(eq("playlist-id-01".to_owned())) 181 | .times(1) 182 | .returning(|_| { 183 | let playlist = Playlist { 184 | id: "playlist-id-01".into(), 185 | name: "Playlist 01".into(), 186 | tracks: vec![], 187 | track_count: 0, 188 | }; 189 | 190 | let track = new_track("My Track"); 191 | let tracks_page = 192 | new_page(vec![track], 0, 1, Some("http://some-other-page".to_owned())); 193 | 194 | Ok((playlist, tracks_page)) 195 | }); 196 | 197 | let backup = backup_playlists("myuser", &mock).unwrap(); 198 | assert_eq!(backup.len(), 1); 199 | 200 | let playlist = backup.get(0).unwrap(); 201 | assert_eq!(playlist.id, "playlist-id-01".to_owned()); 202 | assert_eq!(playlist.name, "Playlist 01".to_owned()); 203 | assert_eq!(playlist.tracks.len(), 2); 204 | assert_eq!(playlist.track_count, 2); 205 | 206 | let track = backup.get(0).unwrap().tracks.get(0).unwrap(); 207 | assert_eq!(track.name, "Track: My Track"); 208 | assert_eq!(track.album.title, "Album: My Track"); 209 | 210 | let track02 = backup.get(0).unwrap().tracks.get(1).unwrap(); 211 | assert_eq!(track02.name, "Track: track from `playlist_tracks`"); 212 | assert_eq!(track02.album.title, "Album: track from `playlist_tracks`"); 213 | } 214 | 215 | #[test] 216 | fn test_backup_playlists_multiple_pages() { 217 | let mut mock = MockSpotifyClientM::new(); 218 | 219 | mock.expect_playlists() 220 | .with(eq("myuser"), eq(Some(50)), eq(Some(0))) 221 | .times(1) 222 | .returning(|_, _, _| { 223 | Ok(new_page( 224 | vec!["playlist-id-01".into()], 225 | 0, 226 | 1, 227 | Some("https://second-playlist-page".into()), 228 | )) 229 | }); 230 | 231 | mock.expect_playlists() 232 | .with(eq("myuser"), eq(Some(50)), eq(Some(50))) 233 | .times(1) 234 | .returning(|_, _, _| Ok(new_page(vec!["playlist-id-02".into()], 0, 1, None))); 235 | 236 | mock.expect_playlist().times(2).returning(|id| { 237 | let playlist = Playlist { 238 | id: id.into(), 239 | name: format!("Playlist: {}", id), 240 | tracks: vec![], 241 | track_count: 0, 242 | }; 243 | 244 | let track = new_track(&format!("My Track: {}", id)); 245 | let tracks_page = new_page(vec![track], 0, 1, None); 246 | 247 | Ok((playlist, tracks_page)) 248 | }); 249 | 250 | let backup = backup_playlists("myuser", &mock).unwrap(); 251 | assert_eq!(backup.len(), 2); 252 | 253 | let mut playlist = backup.get(0).unwrap(); 254 | assert_eq!(playlist.id, "playlist-id-01"); 255 | assert_eq!(playlist.name, "Playlist: playlist-id-01"); 256 | assert_eq!(playlist.tracks.len(), 1); 257 | assert_eq!(playlist.track_count, 1); 258 | 259 | playlist = backup.get(1).unwrap(); 260 | assert_eq!(playlist.id, "playlist-id-02"); 261 | assert_eq!(playlist.name, "Playlist: playlist-id-02"); 262 | assert_eq!(playlist.tracks.len(), 1); 263 | assert_eq!(playlist.track_count, 1); 264 | } 265 | } 266 | -------------------------------------------------------------------------------- /src/serialize.rs: -------------------------------------------------------------------------------- 1 | use rspotify::spotify::senum::AlbumType; 2 | use serde::Serialize; 3 | 4 | pub type PlaylistId = String; 5 | #[derive(Serialize, Debug, Clone)] 6 | pub struct Backup { 7 | pub albums: Vec, 8 | pub playlists: Vec, 9 | } 10 | 11 | #[derive(Serialize, Debug, Clone, PartialEq)] 12 | pub struct Artist { 13 | pub name: String, 14 | } 15 | 16 | #[derive(Serialize, Debug, Clone)] 17 | pub struct Album { 18 | pub title: String, 19 | pub artists: Vec, 20 | pub album_type: Option, 21 | pub release_date: Option, 22 | } 23 | 24 | #[derive(Serialize, Debug, Clone)] 25 | pub struct Track { 26 | pub name: String, 27 | pub album: Album, 28 | } 29 | 30 | #[derive(Serialize, Debug, Clone)] 31 | pub struct Playlist { 32 | #[serde(skip)] 33 | pub id: PlaylistId, 34 | pub name: String, 35 | pub tracks: Vec, 36 | pub track_count: usize, 37 | } 38 | 39 | 40 | impl PartialEq for Album { 41 | fn eq(&self, other: &Self) -> bool { 42 | self.title == other.title && 43 | self.artists == other.artists && 44 | self.album_type.as_ref().map(|a| a.as_str()) == other.album_type.as_ref().map(|a| a.as_str()) && 45 | self.release_date == other.release_date 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /src/server/daemon.rs: -------------------------------------------------------------------------------- 1 | use r2d2_sqlite::SqliteConnectionManager; 2 | use std::thread; 3 | use failure::Error; 4 | 5 | use super::db; 6 | use crate::serialize::Backup; 7 | use crate::backup_fn::*; 8 | use crate::backup_fn::BackupFn; 9 | use crate::config::Config; 10 | use std::path::PathBuf; 11 | use std::fs::File; 12 | use std::io::prelude::*; 13 | use crate::server::db::BackupRequest; 14 | use uuid::Uuid; 15 | use log; 16 | 17 | fn write_backup(req: &BackupRequest, backup: &Backup, backups_dir: &PathBuf) -> Result { 18 | let path = PathBuf::from(format!("{}/{}.json", backups_dir.display(), req.id)); 19 | let mut file = File::create(&path)?; 20 | let json = serde_json::to_string(backup)?; 21 | file.write_all(json.as_bytes())?; 22 | Ok(path) 23 | } 24 | 25 | fn process_backup_request(pool: db::Pool, backup_fn: impl BackupFn, req: &BackupRequest, backups_dir: &PathBuf) -> Result { 26 | log::info!("Starting backup {}", req.id); 27 | 28 | if req.time_created < (time::now() - time::Duration::hours(1)).to_timespec() { 29 | log::warn!("Pending backup is too old, setting error"); 30 | failure::bail!("pending backup is too old") 31 | } else { 32 | let backup = backup_fn.apply(req.token.clone())?; 33 | let file = write_backup(&req, &backup, backups_dir)?; 34 | db::backup_request::set_executed(pool.get()?, req.id, &file)?; 35 | log::info!("Completed backup {} saved to {:?}", req.id, file); 36 | Ok(req.id) 37 | } 38 | } 39 | 40 | fn process_oldest_backup_request(pool: db::Pool, backup_fn: impl BackupFn, backups_dir: &PathBuf, thread_id: u32, total_thread_count: u32) -> Result,Error> { 41 | match db::backup_request::oldest_pending(pool.get()?, thread_id, total_thread_count) { 42 | Ok(Some(req)) => { 43 | log::info!("Found new backup request {}", req.id); 44 | 45 | if let Err(err) = process_backup_request(pool.clone(), backup_fn, &req, backups_dir) { 46 | if let Err(save_err) = db::backup_request::set_error(pool.get()?, req.id, &format!("{}", err)) { 47 | log::error!("Could not set error on backup request: {}", save_err) 48 | } 49 | Err(err) 50 | } else { 51 | Ok(Some(req.id)) 52 | } 53 | }, 54 | Ok(None) => 55 | Ok(None), 56 | Err(err) => 57 | Err(err) 58 | } 59 | } 60 | 61 | fn delete_executed(pool: db::Pool) -> Result, Error> { 62 | let executed = db::backup_request::find_executed(pool.get()?)?; 63 | 64 | for req in executed.iter() { 65 | log::info!("Found executed backup {}", req.id); 66 | 67 | if let Some(file) = &req.file { 68 | std::fs::remove_file(file)?; 69 | log::info!("deleted expired backup file: {:?}", file); 70 | } 71 | 72 | db::backup_request::set_completed(pool.get()?, req.id, req.last_error.is_some())?; 73 | } 74 | 75 | Ok(executed.into_iter().map(|e| e.id).collect()) 76 | } 77 | 78 | pub fn daemon(config: Config) -> () { 79 | let manager = SqliteConnectionManager::file(&config.db_path()); 80 | let pool = db::Pool::new(manager).unwrap(); 81 | 82 | let num_threads = config.worker_count; 83 | let mut workers = vec![]; 84 | 85 | for tid in 0..num_threads { 86 | log::info!("Starting thread {}/{}", tid, num_threads); 87 | 88 | let pool = pool.clone(); 89 | let backups_dir = config.downloads_path().clone(); 90 | 91 | workers.push(thread::spawn(move || { 92 | loop { 93 | let pool = pool.clone(); 94 | 95 | match process_oldest_backup_request(pool, DefaultBackup, &backups_dir, tid, num_threads) { 96 | Ok(Some(id)) => { 97 | log::info!("Finished backup processing for {}", id); 98 | }, 99 | Ok(None) => { 100 | log::debug!("No pending backups, checking later"); 101 | thread::sleep(std::time::Duration::from_secs(1)) 102 | }, 103 | Err(err) => { 104 | log::error!("Could not process backup: {}, {:?}", err, err); 105 | thread::sleep(std::time::Duration::from_secs(5)); 106 | } 107 | } 108 | } 109 | })); 110 | } 111 | 112 | let cleanup_thread = thread::spawn(move || { 113 | loop { 114 | match delete_executed(pool.clone()) { 115 | Ok(_) => 116 | log::debug!("Processed executed/error backups"), 117 | Err(err) => { 118 | log::error!("Could not process executed/error backups : {}, {:?}", err, err) 119 | } 120 | } 121 | 122 | thread::sleep(std::time::Duration::from_secs(5)); 123 | } 124 | }); 125 | 126 | for w in workers { 127 | w.join().unwrap(); 128 | } 129 | 130 | cleanup_thread.join().unwrap(); 131 | } 132 | 133 | 134 | 135 | #[cfg(test)] 136 | mod tests { 137 | use super::*; 138 | use rspotify::spotify::oauth2::TokenInfo; 139 | use db::backup_request::tests::new_db; 140 | use tempfile::tempdir; 141 | use lazy_static::lazy_static; 142 | use crate::server::db::backup_request::{find_executed, find_with_status}; 143 | 144 | lazy_static! { 145 | static ref TEST_BACKUP_DIR: PathBuf = tempdir().unwrap().into_path(); 146 | } 147 | 148 | struct EmptyBackup; 149 | 150 | impl BackupFn for EmptyBackup { 151 | fn apply(&self, _token_info: TokenInfo) -> Result { 152 | Ok( 153 | Backup { 154 | albums: vec![], 155 | playlists: vec![] 156 | } 157 | ) 158 | } 159 | } 160 | 161 | struct ErrorBackup; 162 | 163 | impl BackupFn for ErrorBackup { 164 | fn apply(&self, _token_info: TokenInfo) -> Result { 165 | failure::bail!("[test] error backup") 166 | } 167 | } 168 | 169 | fn new_req() -> BackupRequest { 170 | BackupRequest { 171 | id: Uuid::new_v4(), 172 | token: TokenInfo::default(), 173 | time_created: time::get_time(), 174 | file: None, 175 | last_error: None, 176 | } 177 | } 178 | 179 | #[test] 180 | fn test_processes_existing_backup_request() -> Result<(), Error> { 181 | let pool = new_db()?; 182 | let req = new_req(); 183 | db::backup_request::create(pool.get()?, &req)?; 184 | 185 | let existing = process_oldest_backup_request(pool.clone(), EmptyBackup, &TEST_BACKUP_DIR, 0, 1)?; 186 | 187 | assert_eq!(existing.unwrap(), req.id); 188 | 189 | db::backup_request::tests::set_old(pool.get()?, req.id, None)?; 190 | 191 | let all_executed = db::backup_request::find_executed(pool.get()?)?; 192 | let executed = all_executed.first().unwrap(); 193 | 194 | assert_eq!(executed.id, req.id); 195 | assert!(executed.file.is_some()); 196 | assert!(executed.last_error.is_none()); 197 | 198 | Ok(()) 199 | } 200 | 201 | #[test] 202 | fn test_writes_backup_to_file() -> Result<(), Error> { 203 | let pool = new_db()?; 204 | let req = new_req(); 205 | db::backup_request::create(pool.get()?, &req)?; 206 | 207 | let existing = process_backup_request(pool.clone(), EmptyBackup, &req, &TEST_BACKUP_DIR)?; 208 | 209 | assert_eq!(existing, req.id); 210 | 211 | let db_saved = db::backup_request::find_with_status(pool.get()?, req.id)?.unwrap(); 212 | 213 | let f = std::fs::File::open(db_saved.0.file.unwrap())?; 214 | let s: serde_json::Value = serde_json::from_reader(f)?; 215 | 216 | let backup = s.as_object().unwrap(); 217 | 218 | assert_eq!(backup.get("albums").unwrap().as_array().unwrap().len() , 0); 219 | assert_eq!(backup.get("playlists").unwrap().as_array().unwrap().len() , 0); 220 | 221 | Ok(()) 222 | } 223 | 224 | #[test] 225 | fn test_sets_error_if_backup_fails() -> Result<(), Error> { 226 | let pool = new_db()?; 227 | let req = new_req(); 228 | db::backup_request::create(pool.get()?, &req)?; 229 | 230 | let res = process_oldest_backup_request(pool.clone(), ErrorBackup, &TEST_BACKUP_DIR, 0, 1); 231 | db::backup_request::tests::set_old(pool.get()?, req.id, None)?; 232 | let err_msg = format!("{}", res.err().unwrap()); 233 | 234 | assert_eq!(err_msg, "[test] error backup"); 235 | 236 | let all_executed = db::backup_request::find_executed(pool.get()?)?; 237 | let executed = all_executed.first().unwrap(); 238 | 239 | assert_eq!(executed.id, req.id); 240 | assert!(executed.file.is_none()); 241 | assert_eq!(executed.last_error.as_ref().unwrap(), "[test] error backup"); 242 | 243 | Ok(()) 244 | } 245 | 246 | #[test] 247 | fn test_ok_if_no_backups() -> Result<(), Error> { 248 | let pool = new_db()?; 249 | let existing = process_oldest_backup_request(pool, DefaultBackup, &TEST_BACKUP_DIR, 0, 1)?; 250 | assert_eq!(existing, None); 251 | Ok(()) 252 | } 253 | 254 | #[test] 255 | fn test_deletes_success_expired_backups() -> Result<(), Error> { 256 | let pool = new_db()?; 257 | let req = new_req(); 258 | db::backup_request::create(pool.get()?, &req)?; 259 | 260 | process_backup_request(pool.clone(), EmptyBackup, &req, &TEST_BACKUP_DIR)?; 261 | let executed = db::backup_request::find_with_status(pool.get()?, req.id)?.unwrap().0; 262 | db::backup_request::tests::set_old(pool.get()?, req.id, None)?; 263 | 264 | let before = find_executed(pool.get()?)?; 265 | assert_eq!(before.first().unwrap().id, req.id); 266 | 267 | let deleted = delete_executed(pool.clone())?; 268 | assert_eq!(*deleted.first().unwrap(), req.id); 269 | assert!(!executed.file.unwrap().exists()); 270 | 271 | let after = find_executed(pool.get()?)?; 272 | assert!(after.is_empty()); 273 | 274 | Ok(()) 275 | } 276 | 277 | #[test] 278 | fn test_deletes_error_expired_backups() -> Result<(), Error> { 279 | let pool = new_db()?; 280 | let req = new_req(); 281 | db::backup_request::create(pool.get()?, &req)?; 282 | 283 | let _ = process_oldest_backup_request(pool.clone(), ErrorBackup, &TEST_BACKUP_DIR, 0, 1); 284 | db::backup_request::tests::set_old(pool.get()?, req.id, None)?; 285 | 286 | let before = find_executed(pool.get()?)?; 287 | assert_eq!(before.first().unwrap().id, req.id); 288 | 289 | let deleted = delete_executed(pool.clone())?; 290 | assert_eq!(*deleted.first().unwrap(), req.id); 291 | 292 | let after = find_executed(pool.get()?)?; 293 | assert!(after.is_empty()); 294 | 295 | Ok(()) 296 | } 297 | 298 | #[test] 299 | fn test_does_not_run_very_old_backups() -> Result<(), Error> { 300 | let pool = new_db()?; 301 | let req = new_req(); 302 | db::backup_request::create(pool.get()?, &req)?; 303 | 304 | db::backup_request::tests::set_old(pool.get()?, req.id, Some(time::Duration::hours(2)))?; 305 | 306 | let _ = process_oldest_backup_request(pool.clone(), EmptyBackup, &TEST_BACKUP_DIR, 0, 1); 307 | 308 | let (after_req, after_status) = find_with_status(pool.get()?, req.id)?.unwrap(); 309 | assert_eq!(after_status, db::RequestStatus::Error); 310 | assert_eq!(after_req.last_error.unwrap(), "pending backup is too old"); 311 | 312 | let after = find_executed(pool.get()?)?; 313 | assert_eq!(after.first().unwrap().id, req.id); 314 | 315 | Ok(()) 316 | } 317 | } 318 | -------------------------------------------------------------------------------- /src/server/db.rs: -------------------------------------------------------------------------------- 1 | use uuid::Uuid; 2 | use rspotify::spotify::oauth2::TokenInfo; 3 | use time::Timespec; 4 | use std::path::PathBuf; 5 | 6 | pub type Pool = r2d2::Pool; 7 | pub type Connection = r2d2::PooledConnection; 8 | 9 | #[derive(Debug)] 10 | pub struct BackupRequest { 11 | pub id: Uuid, 12 | pub token: TokenInfo, 13 | pub time_created: Timespec, 14 | pub file: Option, 15 | pub last_error: Option, 16 | } 17 | 18 | // Pending +--> Executed --> timeout ----> CompletedOk 19 | // | 20 | // +--> Error --> timeout ----> CompletedError 21 | #[derive(Debug, serde::Serialize, PartialEq, Clone)] 22 | pub enum RequestStatus { 23 | Pending, 24 | Executed, 25 | Error, 26 | CompletedOk, 27 | CompletedError, 28 | } 29 | 30 | impl RequestStatus { 31 | pub fn executed(&self) -> bool { 32 | match self { 33 | Self::Executed | Self::Error => true, 34 | _ => false 35 | } 36 | } 37 | 38 | pub fn completed(&self) -> bool { 39 | match self { 40 | Self::CompletedOk | Self::CompletedError => true, 41 | _ => false 42 | } 43 | } 44 | } 45 | 46 | 47 | pub mod backup_request { 48 | use super::Connection; 49 | use super::BackupRequest; 50 | use rspotify::spotify::oauth2::TokenInfo; 51 | use failure::Error; 52 | use uuid::Uuid; 53 | use rusqlite::types::{FromSql, FromSqlError, ValueRef, ToSqlOutput}; 54 | use rusqlite::{OptionalExtension, ToSql}; 55 | use std::path::PathBuf; 56 | use rusqlite::params; 57 | use std::collections::hash_map::DefaultHasher; 58 | use std::hash::{Hash, Hasher}; 59 | use crate::server::db::RequestStatus; 60 | 61 | fn from_row(row: &rusqlite::Row) -> Result { 62 | let id: SqlUuid = row.get(0)?; 63 | let token: SqlTokenInfo = row.get(1)?; 64 | let path: Option = row.get(3)?; 65 | 66 | Ok(BackupRequest { 67 | id: id.0, 68 | token: token.0, 69 | time_created: row.get(2)?, 70 | file: path.map(|p| p.0), 71 | last_error: row.get(4)? 72 | }) 73 | } 74 | 75 | pub fn create(c: Connection, req: &BackupRequest) -> Result { 76 | let oauth_json = SqlTokenInfo(req.token.clone()); 77 | 78 | c.execute("INSERT INTO backup_requests (id, token, status, created_at) VALUES (?1, ?2, ?3, ?4)", 79 | params![req.id.to_string(), &oauth_json, &RequestStatus::Pending, &time::get_time()]) 80 | .map(|_| req.id) 81 | .map_err(Error::from) 82 | } 83 | 84 | pub fn set_error(c: Connection, id: Uuid, error: &str) -> Result<(), Error> { 85 | let count = c.execute("UPDATE backup_requests set last_error = ?, status = ? WHERE id = ?", 86 | params![error, RequestStatus::Error, id.to_string()]) 87 | .map_err(Error::from)?; 88 | 89 | if count > 0 { 90 | Ok(()) 91 | } else { 92 | failure::bail!("could not set error on BackupRequest, updated 0 rows") 93 | } 94 | } 95 | 96 | pub fn set_completed(c: Connection, id: Uuid, with_error: bool) -> Result<(), Error> { 97 | let final_status = if with_error { 98 | RequestStatus::CompletedError 99 | } else { 100 | RequestStatus::CompletedOk 101 | }; 102 | 103 | let count = c.execute("UPDATE backup_requests set status = ?, file = NULL, token = ? WHERE id = ?", 104 | params![final_status, SqlTokenInfo(TokenInfo::default()), id.to_string()]) 105 | .map_err(Error::from)?; 106 | 107 | if count > 0 { 108 | Ok(()) 109 | } else { 110 | failure::bail!("could set BackupRequest to executed, updated 0 rows") 111 | } 112 | } 113 | 114 | pub fn set_executed(c: Connection, id: Uuid, file: &PathBuf) -> Result<(), Error> { 115 | let count = c.execute("UPDATE backup_requests set file = ?, status = ? WHERE id = ?", 116 | params![file.to_str(), RequestStatus::Executed, id.to_string()]) 117 | .map_err(Error::from)?; 118 | 119 | if count > 0 { 120 | Ok(()) 121 | } else { 122 | failure::bail!("could complete BackupRequest, updated 0 rows") 123 | } 124 | } 125 | 126 | fn add_thread_id_function(c: &Connection, total_thread_count: u32) -> Result<(), Error> { 127 | c.create_scalar_function("sb_thread_id", 1, true, move |ctx| { 128 | assert_eq!(ctx.len(), 1, "called with unexpected number of arguments"); 129 | 130 | let data = ctx.get::(0)?; 131 | 132 | let mut hasher = DefaultHasher::new(); 133 | data.hash(&mut hasher); 134 | let hash = hasher.finish(); 135 | 136 | Ok((hash % total_thread_count as u64) as i64) 137 | }).map_err(Error::from) 138 | } 139 | 140 | pub fn oldest_pending(c: Connection, thread_id: u32, total_thread_count: u32) -> Result, Error> { 141 | add_thread_id_function(&c, total_thread_count)?; 142 | 143 | let mut stmt = c.prepare( 144 | "SELECT id, token, created_at, file, last_error \ 145 | FROM backup_requests \ 146 | where status = ? and sb_thread_id(id) = ? \ 147 | order by created_at asc limit 1" 148 | )?; 149 | 150 | stmt.query_row( 151 | params![&RequestStatus::Pending, thread_id], 152 | from_row).optional().map_err(Error::from) 153 | } 154 | 155 | pub fn find_executed(c: Connection) -> Result, Error> { 156 | let since = time::now_utc() - time::Duration::hours(1); 157 | 158 | log::debug!("using since = {}", since.rfc3339()); 159 | 160 | let mut stmt = c.prepare( 161 | "SELECT id, token, created_at, file, last_error FROM backup_requests \ 162 | where (status = ? OR status = ?) \ 163 | and created_at < ? \ 164 | order by created_at desc LIMIT 5")?; 165 | 166 | let rows = stmt.query_map(params![RequestStatus::Error, RequestStatus::Executed, &since.to_timespec()],from_row)?; 167 | 168 | let mut result = vec![]; 169 | 170 | for row in rows { 171 | result.push(row?); 172 | } 173 | 174 | Ok(result) 175 | } 176 | 177 | pub fn find_with_status(c: Connection, id: Uuid) -> Result, Error> { 178 | let mut stmt = c.prepare("SELECT id, token, created_at, file, last_error, status FROM backup_requests where id = ?")?; 179 | stmt.query_row(params![&id.to_string()], move |row| { 180 | let req = from_row(row)?; 181 | let status: RequestStatus = row.get(5)?; 182 | Ok((req, status)) 183 | }).optional().map_err(Error::from) 184 | } 185 | 186 | struct SqlUuid(Uuid); 187 | 188 | impl FromSql for SqlUuid { 189 | fn column_result(value: ValueRef<'_>) -> Result { 190 | value 191 | .as_str() 192 | .and_then(|s| s.parse::().map_err(|_| FromSqlError::InvalidType)) 193 | .map(SqlUuid) 194 | } 195 | } 196 | 197 | struct SqlTokenInfo(TokenInfo); 198 | 199 | impl FromSql for SqlTokenInfo { 200 | fn column_result(value: ValueRef<'_>) -> Result { 201 | value 202 | .as_str() 203 | .and_then(|s| serde_json::from_str::(s).map_err(|_| FromSqlError::InvalidType)) 204 | .map(SqlTokenInfo) 205 | } 206 | } 207 | 208 | impl ToSql for SqlTokenInfo { 209 | fn to_sql(&self) -> rusqlite::Result> { 210 | let s = serde_json::to_string(&self.0).map_err(|err| rusqlite::Error::ToSqlConversionFailure(err.into()))?; 211 | Ok(ToSqlOutput::from(s)) 212 | } 213 | } 214 | 215 | struct SqlPathBuf(PathBuf); 216 | 217 | impl FromSql for SqlPathBuf { 218 | fn column_result(value: ValueRef<'_>) -> Result { 219 | value 220 | .as_str() 221 | .and_then(|s| s.parse::().map_err(|_| FromSqlError::InvalidType)) 222 | .map(SqlPathBuf) 223 | } 224 | } 225 | 226 | impl ToSql for RequestStatus { 227 | fn to_sql(&self) -> rusqlite::Result> { 228 | Ok(ToSqlOutput::from(format!("{:?}", self))) 229 | } 230 | } 231 | 232 | impl FromSql for RequestStatus { 233 | fn column_result(value: ValueRef<'_>) -> Result { 234 | match value.as_str()? { 235 | "Pending" => Ok(RequestStatus::Pending), 236 | "Executed" => Ok(RequestStatus::Executed), 237 | "Error" => Ok(RequestStatus::Error), 238 | "CompletedOk" => Ok(RequestStatus::CompletedOk), 239 | "CompletedError" => Ok(RequestStatus::CompletedError), 240 | s => Err(FromSqlError::Other(failure::format_err!("Could not parse RequestStatus: {}", s).into())) 241 | } 242 | } 243 | } 244 | 245 | #[cfg(test)] 246 | pub mod tests { 247 | use super::*; 248 | use rusqlite::NO_PARAMS; 249 | use r2d2_sqlite::SqliteConnectionManager; 250 | use crate::server::db::Pool; 251 | 252 | pub fn new_db() -> Result { 253 | let manager = SqliteConnectionManager::memory(); 254 | let pool = Pool::new(manager)?; 255 | let schema_str = std::fs::read_to_string("./schema.sql")?; 256 | pool.get().unwrap().execute(&schema_str, NO_PARAMS)?; 257 | Ok(pool) 258 | } 259 | 260 | fn new_req() -> BackupRequest { 261 | BackupRequest { 262 | id: Uuid::new_v4(), 263 | token: TokenInfo::default(), 264 | time_created: time::get_time(), 265 | file: None, 266 | last_error: None, 267 | } 268 | } 269 | 270 | fn create_past(pool: &Pool) -> Result { 271 | let req = new_req(); 272 | create(pool.get()?, &req)?; 273 | set_old(pool.get()?, req.id, None)?; 274 | Ok(req) 275 | } 276 | 277 | impl PartialEq for BackupRequest { 278 | fn eq(&self, other: &Self) -> bool { 279 | self.id == other.id && 280 | self.token.access_token == other.token.access_token && // good enough 281 | self.time_created.sec == other.time_created.sec && // ignores nsec, rusqlite loses precision when saving to db 282 | self.file == other.file && 283 | self.last_error == other.last_error 284 | } 285 | } 286 | 287 | pub fn set_old(c: Connection, id: Uuid, duration: Option) -> Result<(), Error> { 288 | let at = time::get_time() - duration.unwrap_or(time::Duration::minutes(61)); 289 | c.execute("UPDATE backup_requests set created_at = ? where id = ?", params![&at, id.to_string()])?; 290 | Ok(()) 291 | } 292 | 293 | #[test] 294 | fn test_add_pending_backup() -> Result<(), Error> { 295 | let pool = new_db()?; 296 | let req = new_req(); 297 | let res = create(pool.get()?, &req)?; 298 | 299 | assert_eq!(res, req.id); 300 | Ok(()) 301 | } 302 | 303 | #[test] 304 | fn test_pending_returns_latest_backup() -> Result<(), Error> { 305 | let pool = new_db()?; 306 | let req = new_req(); 307 | 308 | create(pool.get()?, &req)?; 309 | 310 | let pending = oldest_pending(pool.get()?, 0, 1)?; 311 | 312 | assert_eq!(pending.unwrap().id, req.id); 313 | Ok(()) 314 | } 315 | 316 | #[test] 317 | fn test_gets_backups_for_specified_thread_only() -> Result<(), Error> { 318 | let pool = new_db()?; 319 | let mut req = new_req(); 320 | 321 | let id_1 = Uuid::parse_str("35027614-7cc0-4d51-81f5-fbcc5eeae2e5")?; // has sb_thread_id = 1 322 | let id_2 = Uuid::parse_str("d2c70a33-c1a2-4ee1-9061-f8aa80262466")?; // has sb_thread_id = 2 323 | 324 | req.id = id_1; 325 | create(pool.get()?, &req)?; 326 | 327 | req.id = id_2; 328 | create(pool.get()?, &req)?; 329 | 330 | let pending = oldest_pending(pool.get()?, 0, 3)?; 331 | assert_eq!(pending, None); 332 | 333 | let pending = oldest_pending(pool.get()?, 1, 3)?; 334 | assert_eq!(pending.unwrap().id, id_1); 335 | 336 | let pending = oldest_pending(pool.get()?, 2, 3)?; 337 | assert_eq!(pending.unwrap().id, id_2); 338 | 339 | Ok(()) 340 | } 341 | 342 | #[test] 343 | fn test_finds_error_requests() -> Result<(), Error> { 344 | let pool = new_db()?; 345 | let req = create_past(&pool)?; 346 | set_error(pool.get()?, req.id, "[test] something happened")?; 347 | 348 | let all_executed = find_executed(pool.get()?)?; 349 | let executed = all_executed.first().unwrap(); 350 | 351 | assert_eq!(executed.id, req.id); 352 | Ok(()) 353 | } 354 | 355 | #[test] 356 | fn test_finds_finished_requests() -> Result<(), Error> { 357 | let pool = new_db()?; 358 | let req = create_past(&pool)?; 359 | set_executed(pool.get()?, req.id, &PathBuf::from("/tmp/done.json"))?; 360 | 361 | let all_executed = find_executed(pool.get()?)?; 362 | let executed = all_executed.first().unwrap(); 363 | 364 | assert_eq!(executed.id, req.id); 365 | Ok(()) 366 | } 367 | 368 | #[test] 369 | fn test_does_not_finds_completed_requests() -> Result<(), Error> { 370 | let pool = new_db()?; 371 | let req = create_past(&pool)?; 372 | set_completed(pool.get()?, req.id, false)?; 373 | 374 | let all_executed = find_executed(pool.get()?)?; 375 | 376 | assert_eq!(all_executed.len(), 0); 377 | Ok(()) 378 | } 379 | 380 | #[test] 381 | fn test_find() -> Result<(), Error> { 382 | let pool = new_db()?; 383 | let req = new_req(); 384 | 385 | create(pool.get()?, &req)?; 386 | 387 | let found = find_with_status(pool.get()?, req.id)?.unwrap(); 388 | 389 | assert_eq!(found.0, req); 390 | assert_eq!(found.1, RequestStatus::Pending); 391 | Ok(()) 392 | } 393 | } 394 | } 395 | 396 | -------------------------------------------------------------------------------- /src/server/mod.rs: -------------------------------------------------------------------------------- 1 | use actix_web::{App, http, HttpResponse, HttpServer, middleware, web}; 2 | 3 | use r2d2_sqlite::SqliteConnectionManager; 4 | use rspotify::spotify::oauth2::{SpotifyOAuth, TokenInfo}; 5 | 6 | use serde::{Deserialize, Serialize}; 7 | use uuid::Uuid; 8 | 9 | use crate::server::db::{BackupRequest, Pool, Connection}; 10 | use crate::spotify::*; 11 | use crate::config::Config; 12 | use tera::Tera; 13 | 14 | mod db; 15 | pub mod daemon; 16 | 17 | mod app { 18 | use super::*; 19 | use crate::server::db::RequestStatus; 20 | use tera::Context; 21 | 22 | #[derive(Clone)] 23 | pub struct DefaultRenderer { 24 | pub base_path: String, 25 | tera: Tera 26 | } 27 | 28 | impl DefaultRenderer { 29 | pub fn new(base_path: &str, tera: Tera) -> DefaultRenderer { 30 | DefaultRenderer { base_path: base_path.into(), tera } 31 | } 32 | 33 | pub fn render(&self, tmpl: &str, ctx: &mut Context) -> Result { 34 | ctx.insert("base_path", &self.base_path); 35 | self.tera.render(tmpl, &ctx).map_err(failure::Error::from) 36 | } 37 | } 38 | 39 | #[derive(Deserialize)] 40 | pub struct SpotifyApiCallbackParams { 41 | code: String 42 | } 43 | 44 | #[derive(Serialize)] 45 | pub struct BackupResponse { 46 | pub backup_id: Uuid, 47 | pub missing: bool, 48 | pub executed: bool, 49 | pub completed: bool, 50 | pub error: Option, 51 | } 52 | 53 | pub async fn index(renderer: web::Data) -> Result { 54 | let body = renderer.render("index.html", &mut Context::new())?; 55 | Ok(HttpResponse::Ok().body(body)) 56 | } 57 | 58 | pub async fn find_backup(c: Connection, uuid: Uuid) -> Option<(BackupRequest, RequestStatus)> { 59 | web::block(move || { db::backup_request::find_with_status(c, uuid) }).await.unwrap() 60 | } 61 | 62 | 63 | pub async fn backup_get(path: web::Path<(Uuid, )>, pool: web::Data, renderer: web::Data) -> Result { 64 | let uuid = path.0; 65 | 66 | if let Some((backup, status)) = find_backup(pool.get()?, uuid).await { 67 | let resp = BackupResponse { 68 | backup_id: uuid, 69 | missing: false, 70 | executed: status.executed(), 71 | completed: status.completed(), 72 | error: backup.last_error, 73 | }; 74 | 75 | let body = renderer.render("backup.html", &mut Context::from_serialize(resp)?)?; 76 | Ok(HttpResponse::Ok().body(body)) 77 | } else { 78 | let resp = BackupResponse { 79 | backup_id: uuid, 80 | missing: true, 81 | executed: false, 82 | completed: false, 83 | error: "backup does not exist".to_string().into(), 84 | }; 85 | 86 | let body = renderer.render("backup.html", &mut Context::from_serialize(resp)?)?; 87 | Ok(HttpResponse::NotFound().body(body)) 88 | } 89 | } 90 | 91 | async fn save_backup_request(pool: &Pool, oauth_code: &TokenInfo) -> Uuid { 92 | let id = Uuid::new_v4(); 93 | let p = pool.clone(); 94 | let token = oauth_code.clone(); 95 | 96 | let req = BackupRequest { 97 | id, 98 | token, 99 | time_created: time::now_utc().to_timespec(), 100 | file: None, 101 | last_error: None, 102 | }; 103 | 104 | web::block(move || { db::backup_request::create(p.get()?, &req) }).await.unwrap() 105 | } 106 | 107 | async fn get_access_token(spotify_oauth: &SpotifyOAuth, code: &str) -> TokenInfo { 108 | let code_owned = code.to_string(); 109 | let spotify_owned = spotify_oauth.clone(); 110 | 111 | web::block(move || { 112 | spotify_owned.get_access_token(&code_owned).ok_or("Received token was not valid") 113 | }).await.unwrap() 114 | } 115 | 116 | pub async fn callback(renderer: web::Data, info: web::Query, db: web::Data, spotify_oauth: web::Data) -> HttpResponse { 117 | let token = get_access_token(&spotify_oauth, &info.code).await; 118 | let uuid = save_backup_request(&db, &token).await; 119 | redirect(format!("{}/backups/{}", renderer.base_path, uuid)) 120 | } 121 | 122 | pub fn redirect(url: String) -> HttpResponse { 123 | HttpResponse::Found().set_header(http::header::LOCATION, url).finish() 124 | } 125 | } 126 | 127 | mod api { 128 | use super::*; 129 | 130 | pub async fn backup_get(path: web::Path<(Uuid, )>, pool: web::Data) -> HttpResponse { 131 | let uuid = path.0; 132 | 133 | if let Some((req, status)) = app::find_backup(pool.get().unwrap(), uuid).await { 134 | let response = app::BackupResponse { 135 | backup_id: req.id, 136 | missing: false, 137 | executed: status.executed(), 138 | completed: status.completed(), 139 | error: req.last_error 140 | }; 141 | 142 | HttpResponse::Ok().json(response) 143 | } else { 144 | HttpResponse::NotFound().set_header("Content-Type", "json").finish() 145 | } 146 | } 147 | 148 | pub async fn backup_start(spotify_oauth: web::Data) -> Result { 149 | let uri = build_user_redirect_uri(&spotify_oauth)?; 150 | Ok(app::redirect(uri)) 151 | } 152 | } 153 | 154 | #[actix_rt::main] 155 | pub async fn server(config: Config) -> std::io::Result<()> { 156 | let tera = Tera::new("templates/**/*").unwrap(); 157 | 158 | let base_uri = config.base_uri(); 159 | let base_path = base_uri.path().trim_end_matches("/"); 160 | 161 | let spotify_oauth = build_spotify_oauth(&config.base_uri().to_string(), config.token_cache_path()); 162 | 163 | let downloads_path = config.downloads_path(); 164 | 165 | // Start N db executor actors (N = number of cores avail) 166 | let manager = SqliteConnectionManager::file(config.db_path()); 167 | let pool = Pool::new(manager).unwrap(); 168 | 169 | let renderer = app::DefaultRenderer::new(base_path, tera); 170 | 171 | HttpServer::new(move || { 172 | App::new() 173 | .wrap(middleware::Logger::default()) 174 | .data(pool.clone()) 175 | .data(spotify_oauth.clone()) 176 | .data(renderer.clone()) 177 | .route("/", web::get().to(app::index)) 178 | .route("/callback", web::get().to(app::callback)) 179 | .route("/backups/{id}", web::get().to(app::backup_get)) 180 | .service( 181 | web::scope("/api") 182 | .route("/backups/{id}", web::get().to(api::backup_get)) 183 | .route("/backups", web::post().to(api::backup_start)) 184 | ) 185 | .service(actix_files::Files::new("/static", "static/")) 186 | .service(actix_files::Files::new("/downloads", downloads_path.clone())) 187 | }) 188 | .bind("0.0.0.0:8000") 189 | .unwrap() 190 | .run() 191 | .await 192 | } 193 | -------------------------------------------------------------------------------- /src/spotify/auth.rs: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/simao/spotify-backup/5aa3b27fd4af52e8205a2bfa7e1c986c24b282f8/src/spotify/auth.rs -------------------------------------------------------------------------------- /src/spotify/client.rs: -------------------------------------------------------------------------------- 1 | use rspotify::spotify::client::Spotify; 2 | use rspotify::spotify::model::page::Page; 3 | use rspotify::spotify::model::track::FullTrack; 4 | use rspotify::spotify::senum::AlbumType; 5 | use std::str::FromStr; 6 | use rspotify::spotify::model::album::FullAlbum; 7 | 8 | use failure::Error; 9 | 10 | use crate::serialize::*; 11 | 12 | // TODO: Nothing in this file is tested at all, write it tests 13 | 14 | pub trait SpotifyClient { 15 | fn saved_albums( 16 | &self, 17 | limit: Option, 18 | offset: Option, 19 | ) -> Result, Error>; 20 | 21 | fn playlists( 22 | &self, 23 | user_id: &str, 24 | limit: Option, 25 | offset: Option, 26 | ) -> Result, Error>; 27 | 28 | fn playlist( 29 | &self, 30 | playlist_id: &PlaylistId, 31 | ) -> Result<(Playlist, Page), Error>; 32 | 33 | fn playlist_tracks( 34 | &self, 35 | user_id: &str, 36 | playlist_id: &PlaylistId, 37 | limit: Option, 38 | offset: Option, 39 | ) -> Result, Error>; 40 | } 41 | 42 | 43 | trait SpotifyApiRetry { 44 | fn with_api_retry(&self, max_tries: u32, f: F) -> Result 45 | where 46 | F: Fn(&Self) -> Result; 47 | } 48 | 49 | impl SpotifyApiRetry for Spotify { 50 | fn with_api_retry(&self, max_tries: u32, f: F) -> Result 51 | where 52 | F: Fn(&Spotify) -> Result { 53 | let result = f(&self); 54 | 55 | if let Err(ref err) = result { 56 | if let Some(rspotify::spotify::client::ApiError::RateLimited(Some(i))) = err.downcast_ref::() { 57 | std::thread::sleep(std::time::Duration::from_secs(*i as u64 * 2)); 58 | 59 | if max_tries > 0 { 60 | log::warn!("rate limited, retrying api call after {} seconds", i); 61 | self.with_api_retry(max_tries - 1, f) 62 | } else { 63 | log::error!("rate limited, giving up"); 64 | result 65 | } 66 | } else { 67 | result 68 | } 69 | } else { 70 | result 71 | } 72 | } 73 | } 74 | 75 | impl SpotifyClient for Spotify { 76 | fn saved_albums( 77 | &self, 78 | limit: Option, 79 | offset: Option, 80 | ) -> Result, Error> { 81 | self.current_user_saved_albums(limit, offset) 82 | .map(|page| { 83 | let albums: Vec = page.items.iter().map(|i| i.album.clone().into()).collect(); 84 | 85 | Page { 86 | href: page.href, 87 | items: albums, 88 | limit: page.limit, 89 | offset: page.offset, 90 | previous: page.previous, 91 | total: page.total, 92 | next: page.next, 93 | } 94 | }) 95 | .map_err(|e| e.into()) 96 | } 97 | 98 | fn playlists( 99 | &self, 100 | user_id: &str, 101 | limit: Option, 102 | offset: Option, 103 | ) -> Result, Error> { 104 | self.user_playlists(user_id, limit, offset) 105 | .map(|page| { 106 | let ids = page.items.iter().map(|p| p.id.clone()).collect(); 107 | 108 | Page { 109 | href: page.href, 110 | items: ids, 111 | limit: page.limit, 112 | offset: page.offset, 113 | previous: page.previous, 114 | total: page.total, 115 | next: page.next, 116 | } 117 | }) 118 | .map_err(|e| e.into()) 119 | } 120 | 121 | fn playlist( 122 | &self, 123 | playlist_id: &PlaylistId, 124 | ) -> Result<(Playlist, Page), Error> { 125 | let f = |spotify: &Self| { 126 | spotify.playlist(playlist_id, None, None) 127 | .map(|p| { 128 | let playlist = Playlist { 129 | id: p.id, 130 | name: p.name, 131 | tracks: vec![], 132 | track_count: 0, 133 | }; 134 | 135 | let full_tracks: Vec = 136 | p 137 | .tracks 138 | .items 139 | .iter() 140 | .map(|i| i.track.iter()) 141 | .flatten() 142 | .map(|t| t.clone().into()) 143 | .collect(); 144 | 145 | let tracks = Page { 146 | href: p.tracks.href, 147 | items: full_tracks, 148 | limit: p.tracks.limit, 149 | offset: p.tracks.offset, 150 | previous: p.tracks.previous, 151 | total: p.tracks.total, 152 | next: p.tracks.next, 153 | }; 154 | 155 | (playlist, tracks) 156 | }) 157 | .map_err(|e| e.into()) 158 | }; 159 | 160 | self.with_api_retry(3, f) 161 | } 162 | 163 | fn playlist_tracks( 164 | &self, 165 | user_id: &str, 166 | playlist_id: &PlaylistId, 167 | limit: Option, 168 | offset: Option, 169 | ) -> Result, Error> { 170 | let f = |spotify: &Self| { 171 | spotify.user_playlist_tracks(user_id, playlist_id, None, limit, offset, None) 172 | .map(|page| { 173 | let full_tracks: Vec = 174 | page 175 | .items 176 | .iter() 177 | .map(|i| i.track.iter()) 178 | .flatten() 179 | .map(|t| t.clone().into()) 180 | .collect(); 181 | 182 | Page { 183 | href: page.href, 184 | items: full_tracks, 185 | limit: page.limit, 186 | offset: page.offset, 187 | previous: page.previous, 188 | total: page.total, 189 | next: page.next, 190 | }}) 191 | .map_err(|e| e.into()) 192 | }; 193 | 194 | self.with_api_retry(3, f) 195 | } 196 | } 197 | 198 | impl From for Album { 199 | fn from(full_album: FullAlbum) -> Self { 200 | let artists: Vec = full_album 201 | .artists 202 | .iter() 203 | .map(|artist| Artist { 204 | name: artist.name.clone(), 205 | }) 206 | .collect(); 207 | 208 | Album { 209 | title: full_album.name.clone(), 210 | artists, 211 | album_type: Some(full_album.album_type.clone()), 212 | release_date: Some(full_album.release_date.clone()), 213 | } 214 | } 215 | } 216 | 217 | impl From for Track { 218 | fn from(full_track: FullTrack) -> Self { 219 | Track { 220 | name: full_track.name.clone(), 221 | album: Album { 222 | title: full_track.album.name.clone(), 223 | artists: full_track 224 | .album 225 | .artists 226 | .iter() 227 | .map(|a| Artist { 228 | name: a.name.clone(), 229 | }) 230 | .collect(), 231 | album_type: full_track 232 | .album 233 | .album_type 234 | .clone() 235 | .and_then(|at| AlbumType::from_str(&at).ok()), 236 | release_date: full_track.album.release_date.clone(), 237 | }, 238 | } 239 | } 240 | } 241 | -------------------------------------------------------------------------------- /src/spotify/mod.rs: -------------------------------------------------------------------------------- 1 | use rspotify::spotify::oauth2::SpotifyOAuth; 2 | use failure::Error; 3 | use std::path::PathBuf; 4 | 5 | pub mod auth; 6 | pub mod client; 7 | 8 | pub fn build_spotify_oauth(base_url: &str, cache_path: PathBuf) -> SpotifyOAuth { 9 | // Needs env file 10 | SpotifyOAuth::default() 11 | .redirect_uri(&format!("{}/callback", base_url.trim_end_matches("/"))) 12 | .cache_path(cache_path) 13 | .scope("user-library-read playlist-read-private") // TODO: Maybe needs more scopes? 14 | .build() 15 | } 16 | 17 | pub fn build_user_redirect_uri(oauth: &SpotifyOAuth) -> Result { 18 | let state = rspotify::spotify::util::generate_random_string(16); 19 | let auth_url = oauth.get_authorize_url(Some(&state), None); 20 | 21 | Ok(auth_url) 22 | } 23 | -------------------------------------------------------------------------------- /static/fonts/roboto-v20-latin-300.woff: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/simao/spotify-backup/5aa3b27fd4af52e8205a2bfa7e1c986c24b282f8/static/fonts/roboto-v20-latin-300.woff -------------------------------------------------------------------------------- /static/fonts/roboto-v20-latin-300.woff2: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/simao/spotify-backup/5aa3b27fd4af52e8205a2bfa7e1c986c24b282f8/static/fonts/roboto-v20-latin-300.woff2 -------------------------------------------------------------------------------- /static/fonts/roboto-v20-latin-300italic.woff: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/simao/spotify-backup/5aa3b27fd4af52e8205a2bfa7e1c986c24b282f8/static/fonts/roboto-v20-latin-300italic.woff -------------------------------------------------------------------------------- /static/fonts/roboto-v20-latin-300italic.woff2: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/simao/spotify-backup/5aa3b27fd4af52e8205a2bfa7e1c986c24b282f8/static/fonts/roboto-v20-latin-300italic.woff2 -------------------------------------------------------------------------------- /static/fonts/roboto-v20-latin-700.woff: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/simao/spotify-backup/5aa3b27fd4af52e8205a2bfa7e1c986c24b282f8/static/fonts/roboto-v20-latin-700.woff -------------------------------------------------------------------------------- /static/fonts/roboto-v20-latin-700.woff2: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/simao/spotify-backup/5aa3b27fd4af52e8205a2bfa7e1c986c24b282f8/static/fonts/roboto-v20-latin-700.woff2 -------------------------------------------------------------------------------- /static/fonts/roboto-v20-latin-700italic.woff: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/simao/spotify-backup/5aa3b27fd4af52e8205a2bfa7e1c986c24b282f8/static/fonts/roboto-v20-latin-700italic.woff -------------------------------------------------------------------------------- /static/fonts/roboto-v20-latin-700italic.woff2: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/simao/spotify-backup/5aa3b27fd4af52e8205a2bfa7e1c986c24b282f8/static/fonts/roboto-v20-latin-700italic.woff2 -------------------------------------------------------------------------------- /static/miligram.css: -------------------------------------------------------------------------------- 1 | /*! Milligram-scss v1.3.0 https://milligram.github.io Copyright (c) 2017 CJ Patoilo Licensed under the MIT license */ 2 | /*! Milligram-scss v1.3.0 https://milligram.github.io Copyright (c) 2017 CJ Patoilo Licensed under the MIT license */ 3 | /*! Milligram-scss v1.3.0 https://milligram.github.io Copyright (c) 2017 CJ Patoilo Licensed under the MIT license */ 4 | *, *:after, *:before { box-sizing: inherit; } 5 | 6 | html { box-sizing: border-box; font-size: 62.5%; } 7 | 8 | body { color: black; font-family: 'Roboto', 'Helvetica Neue', 'Helvetica', 'Arial', sans-serif; font-size: 1.6em; font-weight: 300; letter-spacing: .01em; line-height: 1.6; } 9 | 10 | /*! Milligram-scss v1.3.0 https://milligram.github.io Copyright (c) 2017 CJ Patoilo Licensed under the MIT license */ 11 | blockquote { border-left: 0.3rem solid #d1d1d1; margin-left: 0; margin-right: 0; padding: 1rem 1.5rem; } 12 | 13 | blockquote *:last-child { margin-bottom: 0; } 14 | 15 | /*! Milligram-scss v1.3.0 https://milligram.github.io Copyright (c) 2017 CJ Patoilo Licensed under the MIT license */ 16 | .button, button, input[type='button'], input[type='reset'], input[type='submit'] { background-color: #c72c41; border: 0.1rem solid #c72c41; border-radius: .4rem; color: #fff; cursor: pointer; display: inline-block; font-size: 1.1rem; font-weight: 700; height: 3.8rem; letter-spacing: .1rem; line-height: 3.8rem; padding: 0 3.0rem; text-align: center; text-decoration: none; text-transform: uppercase; white-space: nowrap; } 17 | 18 | .button:focus, .button:hover, button:focus, button:hover, input[type='button']:focus, input[type='button']:hover, input[type='reset']:focus, input[type='reset']:hover, input[type='submit']:focus, input[type='submit']:hover { background-color: black; border-color: black; color: #fff; outline: 0; } 19 | 20 | .button[disabled], button[disabled], input[type='button'][disabled], input[type='reset'][disabled], input[type='submit'][disabled] { cursor: default; opacity: .5; } 21 | 22 | .button[disabled]:focus, .button[disabled]:hover, button[disabled]:focus, button[disabled]:hover, input[type='button'][disabled]:focus, input[type='button'][disabled]:hover, input[type='reset'][disabled]:focus, input[type='reset'][disabled]:hover, input[type='submit'][disabled]:focus, input[type='submit'][disabled]:hover { background-color: #c72c41; border-color: #c72c41; } 23 | 24 | .button.button-outline, button.button-outline, input[type='button'].button-outline, input[type='reset'].button-outline, input[type='submit'].button-outline { background-color: transparent; color: #c72c41; } 25 | 26 | .button.button-outline:focus, .button.button-outline:hover, button.button-outline:focus, button.button-outline:hover, input[type='button'].button-outline:focus, input[type='button'].button-outline:hover, input[type='reset'].button-outline:focus, input[type='reset'].button-outline:hover, input[type='submit'].button-outline:focus, input[type='submit'].button-outline:hover { background-color: transparent; border-color: black; color: black; } 27 | 28 | .button.button-outline[disabled]:focus, .button.button-outline[disabled]:hover, button.button-outline[disabled]:focus, button.button-outline[disabled]:hover, input[type='button'].button-outline[disabled]:focus, input[type='button'].button-outline[disabled]:hover, input[type='reset'].button-outline[disabled]:focus, input[type='reset'].button-outline[disabled]:hover, input[type='submit'].button-outline[disabled]:focus, input[type='submit'].button-outline[disabled]:hover { border-color: inherit; color: #c72c41; } 29 | 30 | .button.button-clear, button.button-clear, input[type='button'].button-clear, input[type='reset'].button-clear, input[type='submit'].button-clear { background-color: transparent; border-color: transparent; color: #c72c41; } 31 | 32 | .button.button-clear:focus, .button.button-clear:hover, button.button-clear:focus, button.button-clear:hover, input[type='button'].button-clear:focus, input[type='button'].button-clear:hover, input[type='reset'].button-clear:focus, input[type='reset'].button-clear:hover, input[type='submit'].button-clear:focus, input[type='submit'].button-clear:hover { background-color: transparent; border-color: transparent; color: black; } 33 | 34 | .button.button-clear[disabled]:focus, .button.button-clear[disabled]:hover, button.button-clear[disabled]:focus, button.button-clear[disabled]:hover, input[type='button'].button-clear[disabled]:focus, input[type='button'].button-clear[disabled]:hover, input[type='reset'].button-clear[disabled]:focus, input[type='reset'].button-clear[disabled]:hover, input[type='submit'].button-clear[disabled]:focus, input[type='submit'].button-clear[disabled]:hover { color: #c72c41; } 35 | 36 | /*! Milligram-scss v1.3.0 https://milligram.github.io Copyright (c) 2017 CJ Patoilo Licensed under the MIT license */ 37 | code { background: #f4f5f6; border-radius: .4rem; font-size: 86%; margin: 0 .2rem; padding: .2rem .5rem; white-space: nowrap; } 38 | 39 | pre { background: #f4f5f6; border-left: 0.3rem solid #c72c41; overflow-y: hidden; } 40 | 41 | pre > code { border-radius: 0; display: block; padding: 1rem 1.5rem; white-space: pre; } 42 | 43 | /*! Milligram-scss v1.3.0 https://milligram.github.io Copyright (c) 2017 CJ Patoilo Licensed under the MIT license */ 44 | hr { border: 0; border-top: 0.1rem solid #f4f5f6; margin: 3.0rem 0; } 45 | 46 | /*! Milligram-scss v1.3.0 https://milligram.github.io Copyright (c) 2017 CJ Patoilo Licensed under the MIT license */ 47 | input[type='email'], input[type='number'], input[type='password'], input[type='search'], input[type='tel'], input[type='text'], input[type='url'], textarea, select { appearance: none; background-color: transparent; border: 0.1rem solid #d1d1d1; border-radius: .4rem; box-shadow: none; box-sizing: inherit; height: 3.8rem; padding: .6rem 1.0rem; width: 100%; } 48 | 49 | input[type='email']:focus, input[type='number']:focus, input[type='password']:focus, input[type='search']:focus, input[type='tel']:focus, input[type='text']:focus, input[type='url']:focus, textarea:focus, select:focus { border-color: #c72c41; outline: 0; } 50 | 51 | select { background: url('data:image/svg+xml;utf8,') center right no-repeat; padding-right: 3.0rem; } 52 | 53 | select:focus { background-image: url('data:image/svg+xml;utf8,'); } 54 | 55 | textarea { min-height: 6.5rem; } 56 | 57 | label, legend { display: block; font-size: 1.6rem; font-weight: 700; margin-bottom: .5rem; } 58 | 59 | fieldset { border-width: 0; padding: 0; } 60 | 61 | input[type='checkbox'], input[type='radio'] { display: inline; } 62 | 63 | .label-inline { display: inline-block; font-weight: normal; margin-left: .5rem; } 64 | 65 | /*! Milligram-scss v1.3.0 https://milligram.github.io Copyright (c) 2017 CJ Patoilo Licensed under the MIT license */ 66 | .container { margin: 0 auto; max-width: 112.0rem; padding: 0 2.0rem; position: relative; width: 100%; } 67 | 68 | .row { display: flex; flex-direction: column; padding: 0; width: 100%; } 69 | 70 | .row.row-no-padding { padding: 0; } 71 | 72 | .row.row-no-padding > .column { padding: 0; } 73 | 74 | .row.row-wrap { flex-wrap: wrap; } 75 | 76 | .row.row-top { align-items: flex-start; } 77 | 78 | .row.row-bottom { align-items: flex-end; } 79 | 80 | .row.row-center { align-items: center; } 81 | 82 | .row.row-stretch { align-items: stretch; } 83 | 84 | .row.row-baseline { align-items: baseline; } 85 | 86 | .row .column { display: block; flex: 1 1 auto; margin-left: 0; max-width: 100%; width: 100%; } 87 | 88 | .row .column.column-offset-10 { margin-left: 10%; } 89 | 90 | .row .column.column-offset-20 { margin-left: 20%; } 91 | 92 | .row .column.column-offset-25 { margin-left: 25%; } 93 | 94 | .row .column.column-offset-33, .row .column.column-offset-34 { margin-left: 33.3333%; } 95 | 96 | .row .column.column-offset-50 { margin-left: 50%; } 97 | 98 | .row .column.column-offset-66, .row .column.column-offset-67 { margin-left: 66.6666%; } 99 | 100 | .row .column.column-offset-75 { margin-left: 75%; } 101 | 102 | .row .column.column-offset-80 { margin-left: 80%; } 103 | 104 | .row .column.column-offset-90 { margin-left: 90%; } 105 | 106 | .row .column.column-10 { flex: 0 0 10%; max-width: 10%; } 107 | 108 | .row .column.column-20 { flex: 0 0 20%; max-width: 20%; } 109 | 110 | .row .column.column-25 { flex: 0 0 25%; max-width: 25%; } 111 | 112 | .row .column.column-33, .row .column.column-34 { flex: 0 0 33.3333%; max-width: 33.3333%; } 113 | 114 | .row .column.column-40 { flex: 0 0 40%; max-width: 40%; } 115 | 116 | .row .column.column-50 { flex: 0 0 50%; max-width: 50%; } 117 | 118 | .row .column.column-60 { flex: 0 0 60%; max-width: 60%; } 119 | 120 | .row .column.column-66, .row .column.column-67 { flex: 0 0 66.6666%; max-width: 66.6666%; } 121 | 122 | .row .column.column-75 { flex: 0 0 75%; max-width: 75%; } 123 | 124 | .row .column.column-80 { flex: 0 0 80%; max-width: 80%; } 125 | 126 | .row .column.column-90 { flex: 0 0 90%; max-width: 90%; } 127 | 128 | .row .column .column-top { align-self: flex-start; } 129 | 130 | .row .column .column-bottom { align-self: flex-end; } 131 | 132 | .row .column .column-center { align-self: center; } 133 | 134 | @media (min-width: 40rem) { .row { flex-direction: row; margin-left: -1.0rem; width: calc(100% + 2.0rem); } 135 | .row .column { margin-bottom: inherit; padding: 0 1.0rem; } } 136 | 137 | /*! Milligram-scss v1.3.0 https://milligram.github.io Copyright (c) 2017 CJ Patoilo Licensed under the MIT license */ 138 | a { color: #c72c41; text-decoration: none; } 139 | 140 | a:focus, a:hover { color: black; } 141 | 142 | /*! Milligram-scss v1.3.0 https://milligram.github.io Copyright (c) 2017 CJ Patoilo Licensed under the MIT license */ 143 | dl, ol, ul { list-style: none; margin-top: 0; padding-left: 0; } 144 | 145 | dl dl, dl ol, dl ul, ol dl, ol ol, ol ul, ul dl, ul ol, ul ul { font-size: 90%; margin: 1.5rem 0 1.5rem 3.0rem; } 146 | 147 | ol { list-style: decimal inside; } 148 | 149 | ul { list-style: circle inside; } 150 | 151 | /*! Milligram-scss v1.3.0 https://milligram.github.io Copyright (c) 2017 CJ Patoilo Licensed under the MIT license */ 152 | .button, button, dd, dt, li { margin-bottom: 1.0rem; } 153 | 154 | fieldset, input, select, textarea { margin-bottom: 1.5rem; } 155 | 156 | blockquote, dl, figure, form, ol, p, pre, table, ul { margin-bottom: 2.5rem; } 157 | 158 | /*! Milligram-scss v1.3.0 https://milligram.github.io Copyright (c) 2017 CJ Patoilo Licensed under the MIT license */ 159 | table { border-spacing: 0; width: 100%; } 160 | 161 | td, th { border-bottom: 0.1rem solid #e1e1e1; padding: 1.2rem 1.5rem; text-align: left; } 162 | 163 | td:first-child, th:first-child { padding-left: 0; } 164 | 165 | td:last-child, th:last-child { padding-right: 0; } 166 | 167 | /*! Milligram-scss v1.3.0 https://milligram.github.io Copyright (c) 2017 CJ Patoilo Licensed under the MIT license */ 168 | b, strong { font-weight: bold; } 169 | 170 | p { margin-top: 0; } 171 | 172 | h1, h2, h3, h4, h5, h6 { font-weight: 300; letter-spacing: -.1rem; margin-bottom: 2.0rem; margin-top: 0; } 173 | 174 | h1 { font-size: 4.6rem; line-height: 1.2; } 175 | 176 | h2 { font-size: 3.6rem; line-height: 1.25; } 177 | 178 | h3 { font-size: 2.8rem; line-height: 1.3; } 179 | 180 | h4 { font-size: 2.2rem; letter-spacing: -.08rem; line-height: 1.35; } 181 | 182 | h5 { font-size: 1.8rem; letter-spacing: -.05rem; line-height: 1.5; } 183 | 184 | h6 { font-size: 1.6rem; letter-spacing: 0; line-height: 1.4; } 185 | 186 | /*! Milligram-scss v1.3.0 https://milligram.github.io Copyright (c) 2017 CJ Patoilo Licensed under the MIT license */ 187 | img { max-width: 100%; } 188 | 189 | /*! Milligram-scss v1.3.0 https://milligram.github.io Copyright (c) 2017 CJ Patoilo Licensed under the MIT license */ 190 | .clearfix:after { clear: both; content: ' '; display: table; } 191 | 192 | .float-left { float: left; } 193 | 194 | .float-right { float: right; } 195 | 196 | /** Icons */ 197 | .icon > svg { display: inline-block; width: 16px; height: 16px; vertical-align: middle; } 198 | 199 | .icon > svg path { fill: black; } 200 | 201 | /** Site header */ 202 | .wrapper .container { max-width: 80rem; } 203 | 204 | .site-header { text-align: center; } 205 | 206 | .navbar { display: block; } 207 | 208 | .nav-item { display: inline-block; padding: 1rem; } 209 | 210 | .post-nav { text-align: center; padding: 1rem; } 211 | 212 | .post-title { display: block; } 213 | 214 | /** override column size for mobile */ 215 | @media (max-width: 40rem) { .row .column.column-33, .row .column.column-34 { flex: 0 0 100%; max-width: 100%; } } 216 | 217 | /** Site footer */ 218 | .site-footer { border-top: 1px solid #e8e8e8; margin: 2rem 0; padding: 2rem 0; } 219 | 220 | .contact-list, .social-media-list { list-style: none; margin-left: 0; } 221 | 222 | /** Misc */ 223 | .preview-panel { padding: 1rem; box-sizing: border-box; border: 1px solid #fff; } 224 | 225 | .preview-panel:hover { border: 1px solid #e8e8e8; } 226 | 227 | h1.title { font-family: 'Lexend Deca', sans-serif; } 228 | 229 | a.title2 { color: black; text-decoration: none; } 230 | 231 | a.title2:focus, a.title2:hover { color: black; } 232 | 233 | .site-footer { border-top: 1px solid #e8e8e8; margin: 2rem 0; padding: 2rem 0; text-align: center; } 234 | 235 | .post-title { text-align: center; } 236 | 237 | .lum-lightbox { background: black; } 238 | 239 | /*# sourceMappingURL=main.css.map */ -------------------------------------------------------------------------------- /static/roboto.css: -------------------------------------------------------------------------------- 1 | /* roboto-300 - latin */ 2 | @font-face { 3 | font-family: 'Roboto'; 4 | font-style: normal; 5 | font-weight: 300; 6 | src: local('Roboto Light'), local('Roboto-Light'), 7 | url('fonts/roboto-v20-latin-300.woff2') format('woff2'), /* Chrome 26+, Opera 23+, Firefox 39+ */ 8 | url('fonts/roboto-v20-latin-300.woff') format('woff'); /* Chrome 6+, Firefox 3.6+, IE 9+, Safari 5.1+ */ 9 | } 10 | 11 | /* roboto-300italic - latin */ 12 | @font-face { 13 | font-family: 'Roboto'; 14 | font-style: italic; 15 | font-weight: 300; 16 | src: local('Roboto Light Italic'), local('Roboto-LightItalic'), 17 | url('fonts/roboto-v20-latin-300italic.woff2') format('woff2'), /* Chrome 26+, Opera 23+, Firefox 39+ */ 18 | url('fonts/roboto-v20-latin-300italic.woff') format('woff'); /* Chrome 6+, Firefox 3.6+, IE 9+, Safari 5.1+ */ 19 | } 20 | 21 | /* roboto-700 - latin */ 22 | @font-face { 23 | font-family: 'Roboto'; 24 | font-style: normal; 25 | font-weight: 700; 26 | src: local('Roboto Bold'), local('Roboto-Bold'), 27 | url('fonts/roboto-v20-latin-700.woff2') format('woff2'), /* Chrome 26+, Opera 23+, Firefox 39+ */ 28 | url('fonts/roboto-v20-latin-700.woff') format('woff'); /* Chrome 6+, Firefox 3.6+, IE 9+, Safari 5.1+ */ 29 | } 30 | 31 | /* roboto-700italic - latin */ 32 | @font-face { 33 | font-family: 'Roboto'; 34 | font-style: italic; 35 | font-weight: 700; 36 | src: local('Roboto Bold Italic'), local('Roboto-BoldItalic'), 37 | url('fonts/roboto-v20-latin-700italic.woff2') format('woff2'), /* Chrome 26+, Opera 23+, Firefox 39+ */ 38 | url('fonts/roboto-v20-latin-700italic.woff') format('woff'); /* Chrome 6+, Firefox 3.6+, IE 9+, Safari 5.1+ */ 39 | } 40 | -------------------------------------------------------------------------------- /templates/backup.html: -------------------------------------------------------------------------------- 1 | {% extends "base.html" %} 2 | 3 | {% block content %} 4 | 5 |
6 |
7 |
8 | {% if missing %} 9 |

Missing Backup

10 |

Backup with up with {{backup_id}} not found

11 | 12 | {% elif error %} 13 |

Error

14 | There was an error with your backup: {{error}} 15 | 16 | {% elif completed %} 17 |

Backup expired

18 |

This backup was complete and no longer available.

19 |

Backups and all private data is deleted after certain period of time and they must be downloaded before they expire.

20 | 21 | {% elif executed %} 22 |

Download Backup

23 |

Your backup is ready

24 |

25 | Download your backup here 26 |

27 | 28 | {% else %} 29 |

Download a backup

30 |

Your backup is being processed

31 |

Refresh this page to check if your backup is ready

32 | 36 | 37 | {% endif %} 38 |
39 |
40 |
41 | 42 | {% endblock content %} 43 | 44 | {% block footer %} 45 | {{ super() }} 46 | 47 | 51 | 52 | 99 | {% endblock footer %} 100 | 101 | -------------------------------------------------------------------------------- /templates/base.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | Spotify Backup 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | {% block content %} 17 | {% endblock content %} 18 | 19 | {% block footer %} 20 | {% endblock footer %} 21 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /templates/index.html: -------------------------------------------------------------------------------- 1 | {% extends "base.html" %} 2 | 3 | {% block content %} 4 | 5 |
6 |
7 |
8 |

Backup your Spotify Account

9 |

Press the button to backup your album and playlist data from spotify.

10 |

You will be able to download a json file containing all your albums and playlists.

11 |

This only backs up your spotify data, it does not backup the actual audio files.

12 |

This app can only access your spotify data for an hour. 13 | After an hour, all data is deleted and you will no longer be able to download your backup.

14 |

15 | If you don't want to trust this app with your spotify data 16 | you could also run it yourself, see this github repo. 17 |

18 |
19 | 20 |
21 |
22 |
23 |
24 |
25 | 26 | {% endblock content %} 27 | -------------------------------------------------------------------------------- /tests/full_backup.rs: -------------------------------------------------------------------------------- 1 | extern crate spotify_backup; 2 | 3 | use rspotify::spotify::client::Spotify; 4 | use rspotify::spotify::oauth2::{SpotifyClientCredentials, SpotifyOAuth}; 5 | use rspotify::spotify::util::get_token; 6 | 7 | #[test] 8 | fn test_full_backup() { 9 | let mut oauth = SpotifyOAuth::default() 10 | .scope("user-library-read playlist-read-private") 11 | .build(); 12 | 13 | let token = get_token(&mut oauth).unwrap(); 14 | 15 | let client_credential = SpotifyClientCredentials::default() 16 | .token_info(token) 17 | .build(); 18 | 19 | let spotify = Spotify::default() 20 | .client_credentials_manager(client_credential) 21 | .build(); 22 | 23 | let backup = spotify_backup::backup_fn::DefaultBackup::full_backup("simaomm", &spotify).unwrap(); 24 | 25 | assert!(backup.albums.len() >= 389); 26 | } 27 | --------------------------------------------------------------------------------