chronicle-spotify 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 230610bca1ab6354681e21b3702832d04ac8e2b134e940acd7535da96f1466d5
4
+ data.tar.gz: b8cab459d2173718113607ef4bb051c6eea6a21eb39df849e13ce95a3f5d4ab7
5
+ SHA512:
6
+ metadata.gz: 8bc73fad90e5ae81719fd657949716095cbb2dce117dabb5106879c2b480969b772a5b4290220a59b969a596c7f1af989b0919eec6bdc443456b3386da5560f8
7
+ data.tar.gz: bd1034b1496925f9ca1db1d06de634d4738e447bb051eacffc730fa8469adfbbb654640d6aa75275629981dd7243266f6004f631a19d80b2bc5a8aadd69efe6c
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ .DS_Store
data/.rubocop.yml ADDED
@@ -0,0 +1,44 @@
1
+ AllCops:
2
+ EnabledByDefault: true
3
+ TargetRubyVersion: 2.7
4
+
5
+ Style/FrozenStringLiteralComment:
6
+ SafeAutoCorrect: true
7
+
8
+ Style/StringLiterals:
9
+ Enabled: false
10
+
11
+ Layout/MultilineAssignmentLayout:
12
+ Enabled: false
13
+
14
+ Layout/MultilineMethodCallIndentation:
15
+ EnforcedStyle: indented
16
+
17
+ Layout/RedundantLineBreak:
18
+ Enabled: false
19
+
20
+ Style/MethodCallWithArgsParentheses:
21
+ Enabled: false
22
+
23
+ Style/MethodCalledOnDoEndBlock:
24
+ Exclude:
25
+ - 'spec/**/*'
26
+
27
+ Style/OpenStructUse:
28
+ Enabled: false
29
+
30
+ Style/Copyright:
31
+ Enabled: false
32
+
33
+ Style/MissingElse:
34
+ Enabled: false
35
+
36
+ Style/SymbolArray:
37
+ EnforcedStyle: brackets
38
+
39
+ Style/WordArray:
40
+ EnforcedStyle: brackets
41
+
42
+ Lint/ConstantResolution:
43
+ Enabled: false
44
+
@@ -0,0 +1,84 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or
22
+ advances of any kind
23
+ * Trolling, insulting or derogatory comments, and personal or political attacks
24
+ * Public or private harassment
25
+ * Publishing others' private information, such as a physical or email
26
+ address, without their explicit permission
27
+ * Other conduct which could reasonably be considered inappropriate in a
28
+ professional setting
29
+
30
+ ## Enforcement Responsibilities
31
+
32
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
+
34
+ Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
35
+
36
+ ## Scope
37
+
38
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
+
40
+ ## Enforcement
41
+
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at andrew@hyfen.net. All complaints will be reviewed and investigated promptly and fairly.
43
+
44
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
+
46
+ ## Enforcement Guidelines
47
+
48
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
+
50
+ ### 1. Correction
51
+
52
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
+
54
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
+
56
+ ### 2. Warning
57
+
58
+ **Community Impact**: A violation through a single incident or series of actions.
59
+
60
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
+
62
+ ### 3. Temporary Ban
63
+
64
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
+
66
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
+
68
+ ### 4. Permanent Ban
69
+
70
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
+
72
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
73
+
74
+ ## Attribution
75
+
76
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
+
79
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
+
81
+ [homepage]: https://www.contributor-covenant.org
82
+
83
+ For answers to common questions about this code of conduct, see the FAQ at
84
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in chronicle-spotify.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
data/Gemfile.lock ADDED
@@ -0,0 +1,190 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ chronicle-spotify (0.1.0)
5
+ chronicle-etl (~> 0.5)
6
+ omniauth-spotify
7
+ rspotify (~> 2.11)
8
+
9
+ GEM
10
+ remote: https://rubygems.org/
11
+ specs:
12
+ activesupport (7.0.2.3)
13
+ concurrent-ruby (~> 1.0, >= 1.0.2)
14
+ i18n (>= 1.6, < 2)
15
+ minitest (>= 5.1)
16
+ tzinfo (~> 2.0)
17
+ addressable (2.8.0)
18
+ public_suffix (>= 2.0.2, < 5.0)
19
+ ast (2.4.2)
20
+ byebug (11.1.3)
21
+ chronic_duration (0.10.6)
22
+ numerizer (~> 0.1.1)
23
+ chronicle-etl (0.5.3)
24
+ activesupport (~> 7.0)
25
+ chronic_duration (~> 0.10.6)
26
+ colorize (~> 0.8.1)
27
+ launchy
28
+ marcel (~> 1.0.2)
29
+ mini_exiftool (~> 2.10)
30
+ nokogiri (~> 1.13)
31
+ omniauth (~> 2)
32
+ sequel (~> 5.35)
33
+ sinatra (~> 2)
34
+ sqlite3 (~> 1.4)
35
+ thor (~> 1.2)
36
+ thor-hollaback (~> 0.2)
37
+ tty-progressbar (~> 0.17)
38
+ tty-prompt (~> 0.23)
39
+ tty-spinner
40
+ tty-table (~> 0.11)
41
+ xdg (>= 4.0)
42
+ coderay (1.1.3)
43
+ colorize (0.8.1)
44
+ concurrent-ruby (1.1.10)
45
+ domain_name (0.5.20190701)
46
+ unf (>= 0.0.5, < 1.0.0)
47
+ faraday (2.2.0)
48
+ faraday-net_http (~> 2.0)
49
+ ruby2_keywords (>= 0.0.4)
50
+ faraday-net_http (2.0.2)
51
+ hashie (5.0.0)
52
+ hollaback (0.1.1)
53
+ http-cookie (1.0.4)
54
+ domain_name (~> 0.5)
55
+ i18n (1.10.0)
56
+ concurrent-ruby (~> 1.0)
57
+ jwt (2.3.0)
58
+ launchy (2.5.0)
59
+ addressable (~> 2.7)
60
+ marcel (1.0.2)
61
+ method_source (1.0.0)
62
+ mime-types (3.4.1)
63
+ mime-types-data (~> 3.2015)
64
+ mime-types-data (3.2022.0105)
65
+ mini_exiftool (2.10.2)
66
+ minitest (5.15.0)
67
+ multi_json (1.15.0)
68
+ multi_xml (0.6.0)
69
+ mustermann (1.1.1)
70
+ ruby2_keywords (~> 0.0.1)
71
+ netrc (0.11.0)
72
+ nokogiri (1.13.4-arm64-darwin)
73
+ racc (~> 1.4)
74
+ numerizer (0.1.1)
75
+ oauth2 (1.4.9)
76
+ faraday (>= 0.17.3, < 3.0)
77
+ jwt (>= 1.0, < 3.0)
78
+ multi_json (~> 1.3)
79
+ multi_xml (~> 0.5)
80
+ rack (>= 1.2, < 3)
81
+ omniauth (2.1.0)
82
+ hashie (>= 3.4.6)
83
+ rack (>= 2.2.3)
84
+ rack-protection
85
+ omniauth-oauth2 (1.7.2)
86
+ oauth2 (~> 1.4)
87
+ omniauth (>= 1.9, < 3)
88
+ omniauth-spotify (0.0.13)
89
+ omniauth-oauth2 (~> 1.1)
90
+ parallel (1.22.1)
91
+ parser (3.1.2.0)
92
+ ast (~> 2.4.1)
93
+ pastel (0.8.0)
94
+ tty-color (~> 0.5)
95
+ pry (0.13.1)
96
+ coderay (~> 1.1)
97
+ method_source (~> 1.0)
98
+ pry-byebug (3.9.0)
99
+ byebug (~> 11.0)
100
+ pry (~> 0.13.0)
101
+ public_suffix (4.0.7)
102
+ racc (1.6.0)
103
+ rack (2.2.3)
104
+ rack-protection (2.2.0)
105
+ rack
106
+ rainbow (3.1.1)
107
+ rake (13.0.6)
108
+ regexp_parser (2.3.0)
109
+ rest-client (2.0.2)
110
+ http-cookie (>= 1.0.2, < 2.0)
111
+ mime-types (>= 1.16, < 4.0)
112
+ netrc (~> 0.8)
113
+ rexml (3.2.5)
114
+ rspotify (2.11.1)
115
+ addressable (~> 2.8.0)
116
+ omniauth-oauth2 (>= 1.6)
117
+ rest-client (~> 2.0.2)
118
+ rubocop (1.27.0)
119
+ parallel (~> 1.10)
120
+ parser (>= 3.1.0.0)
121
+ rainbow (>= 2.2.2, < 4.0)
122
+ regexp_parser (>= 1.8, < 3.0)
123
+ rexml
124
+ rubocop-ast (>= 1.16.0, < 2.0)
125
+ ruby-progressbar (~> 1.7)
126
+ unicode-display_width (>= 1.4.0, < 3.0)
127
+ rubocop-ast (1.17.0)
128
+ parser (>= 3.1.1.0)
129
+ ruby-progressbar (1.11.0)
130
+ ruby2_keywords (0.0.5)
131
+ sequel (5.55.0)
132
+ sinatra (2.2.0)
133
+ mustermann (~> 1.0)
134
+ rack (~> 2.2)
135
+ rack-protection (= 2.2.0)
136
+ tilt (~> 2.0)
137
+ sqlite3 (1.4.2)
138
+ strings (0.2.1)
139
+ strings-ansi (~> 0.2)
140
+ unicode-display_width (>= 1.5, < 3.0)
141
+ unicode_utils (~> 1.4)
142
+ strings-ansi (0.2.0)
143
+ thor (1.2.1)
144
+ thor-hollaback (0.2.1)
145
+ hollaback (~> 0.1)
146
+ thor (>= 0.19.1)
147
+ tilt (2.0.10)
148
+ tty-color (0.6.0)
149
+ tty-cursor (0.7.1)
150
+ tty-progressbar (0.18.2)
151
+ strings-ansi (~> 0.2)
152
+ tty-cursor (~> 0.7)
153
+ tty-screen (~> 0.8)
154
+ unicode-display_width (>= 1.6, < 3.0)
155
+ tty-prompt (0.23.1)
156
+ pastel (~> 0.8)
157
+ tty-reader (~> 0.8)
158
+ tty-reader (0.9.0)
159
+ tty-cursor (~> 0.7)
160
+ tty-screen (~> 0.8)
161
+ wisper (~> 2.0)
162
+ tty-screen (0.8.1)
163
+ tty-spinner (0.9.3)
164
+ tty-cursor (~> 0.7)
165
+ tty-table (0.12.0)
166
+ pastel (~> 0.8)
167
+ strings (~> 0.2.0)
168
+ tty-screen (~> 0.8)
169
+ tzinfo (2.0.4)
170
+ concurrent-ruby (~> 1.0)
171
+ unf (0.1.4)
172
+ unf_ext
173
+ unf_ext (0.0.8.1)
174
+ unicode-display_width (2.1.0)
175
+ unicode_utils (1.4.0)
176
+ wisper (2.0.1)
177
+ xdg (6.3.2)
178
+
179
+ PLATFORMS
180
+ arm64-darwin-20
181
+
182
+ DEPENDENCIES
183
+ bundler (~> 2.3)
184
+ chronicle-spotify!
185
+ pry-byebug (~> 3.9)
186
+ rake (~> 13.0)
187
+ rubocop
188
+
189
+ BUNDLED WITH
190
+ 2.3.8
data/README.md ADDED
@@ -0,0 +1,78 @@
1
+ # Chronicle::Spotify
2
+ [![Gem Version](https://badge.fury.io/rb/chronicle-spotify.svg)](https://badge.fury.io/rb/chronicle-spotify)
3
+
4
+ Extract your Spotify history using the command line with this plugin for [chronicle-etl](https://github.com/chronicle-app/chronicle-etl).
5
+
6
+ ## Usage
7
+
8
+ ### 1. Install Chronicle-ETL and this plugin
9
+
10
+ ```sh
11
+ # Install chronicle-etl and this plugin
12
+ $ gem install chronicle-etl
13
+ $ chronicle-etl plugins:install spotify
14
+ ```
15
+
16
+ ### 2. Create a Spotify App
17
+ To get access to the Spotify API, you must first create an app. Press the "Create an app" button in the [Developer Dashboard](https://developer.spotify.com/dashboard/applications).
18
+
19
+ In the app's setting, in the `Redirect URIs` field, add `http://localhost:4567/auth/spotify/callback`. After your app has been saved, grab the `client_id` and `client_secret` credentials and save them to chronicle-etl secrets:
20
+
21
+ ```sh
22
+ $ chronicle-etl secrets:set spotify client_id
23
+ $ chronicle-etl secrets:set spotify client_secret
24
+ ```
25
+
26
+ ### 3. Authorize Spotify
27
+
28
+ Next, we need an access token for accessing your data. We can use the authorization flow:
29
+
30
+ ```sh
31
+ $ chronicle-etl authorizations:new spotify
32
+ ```
33
+
34
+ This will open a browser window to authorize on spotify.com. When the flow is complete, access/refresh tokens will be saved in the chronicle secret system under the "spotify" namespace. It'll be available automatically whenever you use this plugin.
35
+
36
+ ### 4. Use the the plugin
37
+ ```sh
38
+ # Extract recent limits
39
+ $ chronicle-etl --extractor spotify:listens --limit 10
40
+
41
+ # Extract liked tracks from the last week
42
+ $ chronicle-etl --extractor spotify:liked-tracks --since 1w
43
+ # Transform as Chronicle Schema
44
+ $ chronicle-etl --extractor spotify:liked-tracks --since 1w --transformer chronicle:like --loader json
45
+
46
+ # Extract saved albums
47
+ $ chronicle-etl --extractor spotify:saved-albums --limit 10
48
+ ```
49
+
50
+ ## Available Connectors
51
+ ### Extractors
52
+
53
+ All the extractors expect `uid`, `access_token` and `refresh_token` to be available in your Chronicle secrets. After doing the authorization flow, you can verify that they exist using: `$ chronicle-etl secrets:list spotify`
54
+
55
+ #### `liked-tracks`
56
+
57
+ Extractor for your Spotify liked tracks
58
+
59
+ #### `saved-albums`
60
+
61
+ Extractor for your Spotify saved albums
62
+ #### `listens`
63
+
64
+ Extractor for your recent listens. Due to API limitations, only your 50 most recent
65
+
66
+ ### Transformers
67
+
68
+ #### `like`
69
+
70
+ Transform a like (either from `saved-albums` or `liked-tracks`) into Chronicle Schema
71
+
72
+ #### `listen`
73
+
74
+ Transforms a listen (from `listens`) into Chronicle Schema
75
+
76
+ ## Roadmap
77
+ - extractor for playlist activity ([#3](https://github.com/chronicle-app/chronicle-spotify/issues/3))
78
+ - incorporate more Spotify metadata into the transformed records
data/Rakefile ADDED
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ task default: %i[]
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ require "chronicle/spotify"
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ Pry.start
13
+
14
+ # require "irb"
15
+ # IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,47 @@
1
+ # frozen_string_literal: true
2
+
3
+ lib = File.expand_path("../lib", __FILE__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require "chronicle/spotify/version"
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = "chronicle-spotify"
9
+ spec.version = Chronicle::Spotify::VERSION
10
+ spec.authors = ["Andrew Louis"]
11
+ spec.email = ["andrew@hyfen.net"]
12
+
13
+ spec.summary = "Spotify importer for Chronicle"
14
+ spec.description = "Spotify connectors"
15
+ spec.homepage = "https://github.com/chronicle-app/chronicle-spotify"
16
+
17
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
18
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
19
+ if spec.respond_to?(:metadata)
20
+ spec.metadata['allowed_push_host'] = "https://rubygems.org"
21
+
22
+ spec.metadata["homepage_uri"] = spec.homepage
23
+ spec.metadata["source_code_uri"] = "https://github.com/chronicle-app/chronicle-spotify"
24
+ spec.metadata["changelog_uri"] = "https://github.com/chronicle-app/chronicle-spotify"
25
+ else
26
+ raise "RubyGems 2.0 or newer is required to protect against " \
27
+ "public gem pushes."
28
+ end
29
+
30
+ # Specify which files should be added to the gem when it is released.
31
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
32
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
33
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
34
+ end
35
+ spec.bindir = "exe"
36
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
37
+ spec.require_paths = ["lib"]
38
+
39
+ spec.add_dependency "chronicle-etl", "~> 0.5"
40
+ spec.add_dependency "omniauth-spotify"
41
+ spec.add_dependency "rspotify", "~> 2.11"
42
+
43
+ spec.add_development_dependency "rubocop"
44
+ spec.add_development_dependency "bundler", "~> 2.3"
45
+ spec.add_development_dependency "rake", "~> 13.0.6"
46
+ spec.add_development_dependency "pry-byebug", "~> 3.9"
47
+ end
@@ -0,0 +1,18 @@
1
+ require 'omniauth-spotify'
2
+
3
+ module Chronicle
4
+ module Spotify
5
+ class Authorizer < Chronicle::ETL::OauthAuthorizer
6
+ provider :spotify
7
+ omniauth_strategy :spotify
8
+ scope 'user-read-recently-played playlist-read-private playlist-read-collaborative user-read-private user-read-email user-library-read'
9
+ pluck_secrets({
10
+ access_token: [:credentials, :token],
11
+ refresh_token: [:credentials, :refresh_token],
12
+ uid: [:uid],
13
+ name: [:info, :name],
14
+ email: [:info, :email],
15
+ })
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,96 @@
1
+ module Chronicle
2
+ module Spotify
3
+ module Builders
4
+ def build_listen(track:, timestamp:, actor:)
5
+ record = ::Chronicle::ETL::Models::Activity.new({
6
+ verb: 'listened',
7
+ provider: 'spotify',
8
+ end_at: timestamp
9
+ })
10
+ record.dedupe_on << [:provider, :verb, :end_at]
11
+ record.actor = build_actor(actor)
12
+ record.involved = build_track(track)
13
+ record
14
+ end
15
+
16
+ def build_liked(object:, timestamp:, actor:)
17
+ record = ::Chronicle::ETL::Models::Activity.new({
18
+ verb: 'liked',
19
+ provider: 'spotify',
20
+ end_at: timestamp
21
+ })
22
+ record.dedupe_on << [:provider, :verb, :end_at]
23
+ record.actor = build_actor(actor)
24
+
25
+ record.involved = if object[:album_type]
26
+ build_album(object)
27
+ else
28
+ build_track(object)
29
+ end
30
+
31
+ record
32
+ end
33
+
34
+ def build_track(track)
35
+ record = ::Chronicle::ETL::Models::Entity.new({
36
+ represents: 'song',
37
+ provider: 'spotify',
38
+ provider_id: track[:id],
39
+ title: track[:name],
40
+ })
41
+
42
+ record.creators = track[:artists].map{ |creator| build_creator(creator) }
43
+ record.containers = build_album(track[:album])
44
+ record.dedupe_on << [:provider, :provider_id, :represents]
45
+ record
46
+ end
47
+
48
+ def build_creator(artist)
49
+ record = ::Chronicle::ETL::Models::Entity.new({
50
+ represents: 'musicartist',
51
+ provider: 'spotify',
52
+ provider_id: artist[:id],
53
+ provider_url: artist[:external_urls][:spotify],
54
+ title: artist[:name],
55
+ })
56
+ record.dedupe_on << [:provider_url]
57
+ record.dedupe_on << [:provider, :provider_id, :represents]
58
+ record
59
+ end
60
+
61
+ def build_album(album)
62
+ record = ::Chronicle::ETL::Models::Entity.new({
63
+ represents: 'album',
64
+ provider: 'spotify',
65
+ provider_id: album[:id],
66
+ provider_url: album[:external_urls][:spotify],
67
+ title: album[:name],
68
+ })
69
+ record.attachments = build_image(album[:images].first[:url])
70
+ record.dedupe_on << [:provider_url]
71
+ record.dedupe_on << [:provider, :provider_id, :represents]
72
+ record
73
+ end
74
+
75
+ def build_image(image_url)
76
+ ::Chronicle::ETL::Models::Attachment.new({
77
+ url_original: image_url
78
+ })
79
+ end
80
+
81
+ def build_actor(actor)
82
+ record = ::Chronicle::ETL::Models::Entity.new({
83
+ represents: 'identity',
84
+ provider: 'spotify',
85
+ provider_id: actor[:id],
86
+ provider_url: actor[:external_urls][:spotify],
87
+ slug: actor[:id],
88
+ title: actor[:display_name]
89
+ })
90
+ record.dedupe_on << [:provider_url]
91
+ record.dedupe_on << [:provider, :provider_id, :represents]
92
+ record
93
+ end
94
+ end
95
+ end
96
+ end
@@ -0,0 +1,35 @@
1
+ module Chronicle
2
+ module Spotify
3
+ class LikeTransformer < Chronicle::ETL::Transformer
4
+ include Chronicle::Spotify::Builders
5
+
6
+ register_connector do |r|
7
+ r.provider = 'spotify'
8
+ r.description = 'a spotify track'
9
+ r.identifier = 'like'
10
+ end
11
+
12
+ def transform
13
+ build_liked(
14
+ timestamp: timestamp,
15
+ object: object,
16
+ actor: @extraction.meta[:actor]
17
+ )
18
+ end
19
+
20
+ def id
21
+ object[:id]
22
+ end
23
+
24
+ def timestamp
25
+ Time.parse(@extraction.data[:added_at])
26
+ end
27
+
28
+ private
29
+
30
+ def object
31
+ @object = @extraction.data[:track] || @extraction.data[:album]
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,17 @@
1
+ module Chronicle
2
+ module Spotify
3
+ class SavedTracksExtractor < Chronicle::Spotify::SpotifyExtractor
4
+ register_connector do |r|
5
+ r.provider = 'spotify'
6
+ r.description = 'liked tracks'
7
+ r.identifier = 'liked-tracks'
8
+ end
9
+
10
+ def extract
11
+ @proxy.saved_tracks(after: @config.since, limit: @config.limit) do |item|
12
+ yield Chronicle::ETL::Extraction.new(data: item, meta: { actor: @actor })
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,29 @@
1
+ module Chronicle
2
+ module Spotify
3
+ class ListenTransformer < Chronicle::ETL::Transformer
4
+ include Chronicle::Spotify::Builders
5
+
6
+ register_connector do |r|
7
+ r.provider = 'spotify'
8
+ r.description = 'a spotify listen'
9
+ r.identifier = 'listen'
10
+ end
11
+
12
+ def transform
13
+ build_listen(
14
+ timestamp: timestamp,
15
+ track: @extraction.data[:track],
16
+ actor: @extraction.meta[:actor]
17
+ )
18
+ end
19
+
20
+ def id
21
+ @extraction.data[:track][:id]
22
+ end
23
+
24
+ def timestamp
25
+ Time.parse(@extraction.data[:played_at])
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,17 @@
1
+ module Chronicle
2
+ module Spotify
3
+ class ListenedExtractor < Chronicle::Spotify::SpotifyExtractor
4
+ register_connector do |r|
5
+ r.provider = 'spotify'
6
+ r.description = 'listened tracks'
7
+ r.identifier = 'listens'
8
+ end
9
+
10
+ def extract
11
+ @proxy.recently_played(after: @config.since, limit: @config.limit, before: @config.until) do |item|
12
+ yield Chronicle::ETL::Extraction.new(data: item, meta: { actor: @actor })
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,77 @@
1
+ require 'rspotify'
2
+
3
+ module Chronicle
4
+ module Spotify
5
+ class Proxy
6
+ PER_PAGE = 50
7
+
8
+ def initialize(access_token:, refresh_token:, client_id:, client_secret:, uid:)
9
+ auth_hash = {
10
+ 'credentials' => {
11
+ 'token' => access_token,
12
+ 'refresh_token' => refresh_token
13
+ },
14
+ 'id' => uid
15
+ }
16
+ RSpotify.authenticate(client_id, client_secret)
17
+ RSpotify.raw_response = true
18
+ @authenticated_user = RSpotify::User.new(auth_hash)
19
+ end
20
+
21
+ def user
22
+ JSON.parse(RSpotify::User.find(@authenticated_user.id), symbolize_names: true)
23
+ end
24
+
25
+ def saved_albums(after: nil, limit:, &block)
26
+ retrieve_all(method: :saved_albums, after: after, limit: limit, &block)
27
+ end
28
+
29
+ def saved_tracks(after: nil, limit:, &block)
30
+ retrieve_all(method: :saved_tracks, after: after, limit: limit, &block)
31
+ end
32
+
33
+ def recently_played(before: nil, after: nil, limit: nil, &block)
34
+ before = before&.to_i&.*(1000)
35
+ retrieve_all(method: :recently_played, before: before, after: after, limit: limit, &block)
36
+ end
37
+
38
+ private
39
+
40
+ def retrieve_all(method:, before: nil, after: nil, limit: nil)
41
+ has_more = true
42
+ count = 0
43
+ offset = 0 if method != :recently_played # FIXME: hacky
44
+
45
+ while has_more
46
+ response = retrieve_page(method: method, before: before, offset: offset)
47
+
48
+ records = response[:items]
49
+ records = records.first(limit - count) if limit
50
+ records = records.filter { |record| Time.parse(record[:added_at] || record[:played_at]) > after } if after
51
+
52
+ break unless records.any?
53
+
54
+ records.each do |track|
55
+ yield track
56
+ end
57
+
58
+ count += records.count
59
+ has_more = response[:next]
60
+ # Available for recently_played
61
+ before = response.dig(:cursors, :before)
62
+ offset += PER_PAGE if offset
63
+ end
64
+ end
65
+
66
+ def retrieve_page(method:, before: nil, offset: nil)
67
+ options = {
68
+ offset: offset,
69
+ limit: PER_PAGE,
70
+ before: before
71
+ }.compact
72
+
73
+ JSON.parse(@authenticated_user.send(method, **options), symbolize_names: true)
74
+ end
75
+ end
76
+ end
77
+ end
@@ -0,0 +1,17 @@
1
+ module Chronicle
2
+ module Spotify
3
+ class SavedAlbumsExtractor < Chronicle::Spotify::SpotifyExtractor
4
+ register_connector do |r|
5
+ r.provider = 'spotify'
6
+ r.description = 'saved albums'
7
+ r.identifier = 'saved-albums'
8
+ end
9
+
10
+ def extract
11
+ @proxy.saved_albums(after: @config.since, limit: @config.limit) do |item|
12
+ yield Chronicle::ETL::Extraction.new(data: item, meta: { actor: @actor })
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,22 @@
1
+ module Chronicle
2
+ module Spotify
3
+ class SpotifyExtractor < Chronicle::ETL::Extractor
4
+ setting :uid
5
+ setting :access_token
6
+ setting :refresh_token
7
+ setting :client_id
8
+ setting :client_secret
9
+
10
+ def prepare
11
+ @proxy = Proxy.new(
12
+ uid: @config.uid,
13
+ access_token: @config.access_token,
14
+ refresh_token: @config.refresh_token,
15
+ client_id: @config.client_id,
16
+ client_secret: @config.client_secret
17
+ )
18
+ @actor = @proxy.user
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Chronicle
4
+ module Spotify
5
+ VERSION = "0.1.0"
6
+ end
7
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'chronicle/etl'
4
+
5
+ require_relative "spotify/version"
6
+
7
+ require_relative "spotify/authorizer"
8
+ require_relative "spotify/proxy"
9
+ require_relative "spotify/builders"
10
+
11
+ require_relative "spotify/spotify_extractor"
12
+ require_relative "spotify/listens_extractor"
13
+ require_relative "spotify/liked_tracks_extractor"
14
+ require_relative "spotify/saved_albums_extractor"
15
+
16
+ require_relative "spotify/like_transformer"
17
+ require_relative "spotify/listen_transformer"
18
+ module Chronicle
19
+ module Spotify
20
+ end
21
+ end
metadata ADDED
@@ -0,0 +1,165 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: chronicle-spotify
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Andrew Louis
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2022-04-24 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: chronicle-etl
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.5'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.5'
27
+ - !ruby/object:Gem::Dependency
28
+ name: omniauth-spotify
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspotify
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '2.11'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '2.11'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rubocop
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: bundler
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '2.3'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '2.3'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rake
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: 13.0.6
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: 13.0.6
97
+ - !ruby/object:Gem::Dependency
98
+ name: pry-byebug
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '3.9'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '3.9'
111
+ description: Spotify connectors
112
+ email:
113
+ - andrew@hyfen.net
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - ".gitignore"
119
+ - ".rubocop.yml"
120
+ - CODE_OF_CONDUCT.md
121
+ - Gemfile
122
+ - Gemfile.lock
123
+ - README.md
124
+ - Rakefile
125
+ - bin/console
126
+ - bin/setup
127
+ - chronicle-spotify.gemspec
128
+ - lib/chronicle/spotify.rb
129
+ - lib/chronicle/spotify/authorizer.rb
130
+ - lib/chronicle/spotify/builders.rb
131
+ - lib/chronicle/spotify/like_transformer.rb
132
+ - lib/chronicle/spotify/liked_tracks_extractor.rb
133
+ - lib/chronicle/spotify/listen_transformer.rb
134
+ - lib/chronicle/spotify/listens_extractor.rb
135
+ - lib/chronicle/spotify/proxy.rb
136
+ - lib/chronicle/spotify/saved_albums_extractor.rb
137
+ - lib/chronicle/spotify/spotify_extractor.rb
138
+ - lib/chronicle/spotify/version.rb
139
+ homepage: https://github.com/chronicle-app/chronicle-spotify
140
+ licenses: []
141
+ metadata:
142
+ allowed_push_host: https://rubygems.org
143
+ homepage_uri: https://github.com/chronicle-app/chronicle-spotify
144
+ source_code_uri: https://github.com/chronicle-app/chronicle-spotify
145
+ changelog_uri: https://github.com/chronicle-app/chronicle-spotify
146
+ post_install_message:
147
+ rdoc_options: []
148
+ require_paths:
149
+ - lib
150
+ required_ruby_version: !ruby/object:Gem::Requirement
151
+ requirements:
152
+ - - ">="
153
+ - !ruby/object:Gem::Version
154
+ version: '0'
155
+ required_rubygems_version: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - ">="
158
+ - !ruby/object:Gem::Version
159
+ version: '0'
160
+ requirements: []
161
+ rubygems_version: 3.3.3
162
+ signing_key:
163
+ specification_version: 4
164
+ summary: Spotify importer for Chronicle
165
+ test_files: []