File: ipfs.py

package info (click to toggle)
beets 2.5.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 7,988 kB
  • sloc: python: 46,429; javascript: 8,018; xml: 334; sh: 261; makefile: 125
file content (312 lines) | stat: -rw-r--r-- 10,363 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
# This file is part of beets.
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.

"""Adds support for ipfs. Requires go-ipfs and a running ipfs daemon"""

import os
import shutil
import subprocess
import tempfile

from beets import config, library, ui, util
from beets.plugins import BeetsPlugin


class IPFSPlugin(BeetsPlugin):
    def __init__(self):
        super().__init__()
        self.config.add(
            {
                "auto": True,
                "nocopy": False,
            }
        )

        if self.config["auto"]:
            self.import_stages = [self.auto_add]

    def commands(self):
        cmd = ui.Subcommand("ipfs", help="interact with ipfs")
        cmd.parser.add_option(
            "-a", "--add", dest="add", action="store_true", help="Add to ipfs"
        )
        cmd.parser.add_option(
            "-g", "--get", dest="get", action="store_true", help="Get from ipfs"
        )
        cmd.parser.add_option(
            "-p",
            "--publish",
            dest="publish",
            action="store_true",
            help="Publish local library to ipfs",
        )
        cmd.parser.add_option(
            "-i",
            "--import",
            dest="_import",
            action="store_true",
            help="Import remote library from ipfs",
        )
        cmd.parser.add_option(
            "-l",
            "--list",
            dest="_list",
            action="store_true",
            help="Query imported libraries",
        )
        cmd.parser.add_option(
            "-m",
            "--play",
            dest="play",
            action="store_true",
            help="Play music from remote libraries",
        )

        def func(lib, opts, args):
            if opts.add:
                for album in lib.albums(args):
                    if len(album.items()) == 0:
                        self._log.info(
                            "{} does not contain items, aborting", album
                        )

                    self.ipfs_add(album)
                    album.store()

            if opts.get:
                self.ipfs_get(lib, args)

            if opts.publish:
                self.ipfs_publish(lib)

            if opts._import:
                self.ipfs_import(lib, args)

            if opts._list:
                self.ipfs_list(lib, args)

            if opts.play:
                self.ipfs_play(lib, opts, args)

        cmd.func = func
        return [cmd]

    def auto_add(self, session, task):
        if task.is_album:
            if self.ipfs_add(task.album):
                task.album.store()

    def ipfs_play(self, lib, opts, args):
        from beetsplug.play import PlayPlugin

        jlib = self.get_remote_lib(lib)
        player = PlayPlugin()
        config["play"]["relative_to"] = None
        player.album = True
        player.play_music(jlib, player, args)

    def ipfs_add(self, album):
        try:
            album_dir = album.item_dir()
        except AttributeError:
            return False
        try:
            if album.ipfs:
                self._log.debug("{} already added", album_dir)
                # Already added to ipfs
                return False
        except AttributeError:
            pass

        self._log.info("Adding {} to ipfs", album_dir)

        if self.config["nocopy"]:
            cmd = "ipfs add --nocopy -q -r".split()
        else:
            cmd = "ipfs add -q -r".split()
        cmd.append(album_dir)
        try:
            output = util.command_output(cmd).stdout.split()
        except (OSError, subprocess.CalledProcessError) as exc:
            self._log.error("Failed to add {}, error: {}", album_dir, exc)
            return False
        length = len(output)

        for linenr, line in enumerate(output):
            line = line.strip()
            if linenr == length - 1:
                # last printed line is the album hash
                self._log.info("album: {}", line)
                album.ipfs = line
            else:
                try:
                    item = album.items()[linenr]
                    self._log.info("item: {}", line)
                    item.ipfs = line
                    item.store()
                except IndexError:
                    # if there's non music files in the to-add folder they'll
                    # get ignored here
                    pass

        return True

    def ipfs_get(self, lib, query):
        query = query[0]
        # Check if query is a hash
        # TODO: generalize to other hashes; probably use a multihash
        # implementation
        if query.startswith("Qm") and len(query) == 46:
            self.ipfs_get_from_hash(lib, query)
        else:
            albums = self.query(lib, query)
            for album in albums:
                self.ipfs_get_from_hash(lib, album.ipfs)

    def ipfs_get_from_hash(self, lib, _hash):
        try:
            cmd = "ipfs get".split()
            cmd.append(_hash)
            util.command_output(cmd)
        except (OSError, subprocess.CalledProcessError) as err:
            self._log.error(
                "Failed to get {} from ipfs.\n{.output}", _hash, err
            )
            return False

        self._log.info("Getting {} from ipfs", _hash)
        imp = ui.commands.TerminalImportSession(
            lib, loghandler=None, query=None, paths=[_hash]
        )
        imp.run()
        # This uses a relative path, hence we cannot use util.syspath(_hash,
        # prefix=True). However, that should be fine since the hash will not
        # exceed MAX_PATH.
        shutil.rmtree(util.syspath(_hash, prefix=False))

    def ipfs_publish(self, lib):
        with tempfile.NamedTemporaryFile() as tmp:
            self.ipfs_added_albums(lib, tmp.name)
            try:
                if self.config["nocopy"]:
                    cmd = "ipfs add --nocopy -q ".split()
                else:
                    cmd = "ipfs add -q ".split()
                cmd.append(tmp.name)
                output = util.command_output(cmd).stdout
            except (OSError, subprocess.CalledProcessError) as err:
                msg = f"Failed to publish library. Error: {err}"
                self._log.error(msg)
                return False
            self._log.info("hash of library: {}", output)

    def ipfs_import(self, lib, args):
        _hash = args[0]
        if len(args) > 1:
            lib_name = args[1]
        else:
            lib_name = _hash
        lib_root = os.path.dirname(lib.path)
        remote_libs = os.path.join(lib_root, b"remotes")
        if not os.path.exists(remote_libs):
            try:
                os.makedirs(remote_libs)
            except OSError as e:
                msg = f"Could not create {remote_libs}. Error: {e}"
                self._log.error(msg)
                return False
        path = os.path.join(remote_libs, lib_name.encode() + b".db")
        if not os.path.exists(path):
            cmd = f"ipfs get {_hash} -o".split()
            cmd.append(path)
            try:
                util.command_output(cmd)
            except (OSError, subprocess.CalledProcessError):
                self._log.error("Could not import {}", _hash)
                return False

        # add all albums from remotes into a combined library
        jpath = os.path.join(remote_libs, b"joined.db")
        jlib = library.Library(jpath)
        nlib = library.Library(path)
        for album in nlib.albums():
            if not self.already_added(album, jlib):
                new_album = []
                for item in album.items():
                    item.id = None
                    new_album.append(item)
                added_album = jlib.add_album(new_album)
                added_album.ipfs = album.ipfs
                added_album.store()

    def already_added(self, check, jlib):
        for jalbum in jlib.albums():
            if jalbum.mb_albumid == check.mb_albumid:
                return True
        return False

    def ipfs_list(self, lib, args):
        fmt = config["format_album"].get()
        try:
            albums = self.query(lib, args)
        except OSError:
            ui.print_("No imported libraries yet.")
            return

        for album in albums:
            ui.print_(format(album, fmt), " : ", album.ipfs.decode())

    def query(self, lib, args):
        rlib = self.get_remote_lib(lib)
        albums = rlib.albums(args)
        return albums

    def get_remote_lib(self, lib):
        lib_root = os.path.dirname(lib.path)
        remote_libs = os.path.join(lib_root, b"remotes")
        path = os.path.join(remote_libs, b"joined.db")
        if not os.path.isfile(path):
            raise OSError
        return library.Library(path)

    def ipfs_added_albums(self, rlib, tmpname):
        """Returns a new library with only albums/items added to ipfs"""
        tmplib = library.Library(tmpname)
        for album in rlib.albums():
            try:
                if album.ipfs:
                    self.create_new_album(album, tmplib)
            except AttributeError:
                pass
        return tmplib

    def create_new_album(self, album, tmplib):
        items = []
        for item in album.items():
            try:
                if not item.ipfs:
                    break
            except AttributeError:
                pass
            item_path = os.fsdecode(os.path.basename(item.path))
            # Clear current path from item
            item.path = f"/ipfs/{album.ipfs}/{item_path}"

            item.id = None
            items.append(item)
        if len(items) < 1:
            return False
        self._log.info("Adding '{}' to temporary library", album)
        new_album = tmplib.add_album(items)
        new_album.ipfs = album.ipfs
        new_album.store(inherit=False)