This repository has been archived by the owner on Oct 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathDiscogs.py
162 lines (141 loc) · 6.52 KB
/
Discogs.py
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
# Author: Dennis Lutter <[email protected]>
# URL: https://github.com/lad1337/XDM-main-plugin-repo/
#
# This file is part of a XDM plugin.
#
# XDM plugin.
# Copyright (C) 2013 Dennis Lutter
#
# This plugin is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This plugin is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see http://www.gnu.org/licenses/.
from xdm.plugins import *
import discogs_client
import re
user_agent = '%s +http://xdm.lad1337.de' % common.getVersionHuman()
OAuth = common.PM.getPluginByIdentifier("de.lad1337.oauth").OAuth1
class Discogs(Provider):
version = "0.14"
identifier = "de.lad1337.music.discogs"
_tag = 'discogs'
single = True
types = ['de.lad1337.music']
_config = {'enabled': True,
'search_range_select': 'master'}
config_meta = {'plugin_desc': 'Information provider for music from http://www.discogs.com. Note: Image download is limited to 1000 per 24h per IP'
}
search_range_select_map = {'master': {'t': 'Master Releases', 'c': ('MasterRelease',)},
'both': {'t': 'Master & Normal Releases', 'c': ('MasterRelease', 'Release')},
'releases': {'t': 'Releases', 'c': ('Release',)}}
oauth = OAuth(
"mjdbIivOosoimlstofrw",
"rspGOethLnnrIBLoLijcmiRmaMXirugB",
request_token_url="https://api.discogs.com/oauth/request_token",
authorize_url="https://www.discogs.com/oauth/authorize",
token_url="https://api.discogs.com/oauth/access_token",
headers={"User-Agent": user_agent}
)
def __init__(self, instance):
super(Discogs, self).__init__(instance)
# https://github.com/discogs/discogs_client
self.d = discogs_client.Client(
user_agent,
consumer_key=self.oauth.consumer_key,
consumer_secret=self.oauth.consumer_secret,
token=self.oauth.resource_owner_key,
secret=self.oauth.resource_owner_secret
)
def _search_range_select(self):
out = {}
for i, o in self.search_range_select_map.items():
out[i] = o['t']
return out
def searchForElement(self, term=''):
self.progress.reset()
# artist = discogs.Artist('Aphex Twin')
mediatype = MediaType.get(MediaType.identifier == 'de.lad1337.music')
mtm = common.PM.getMediaTypeManager('de.lad1337.music')[0]
results = self.d.search(term, type='release')
fakeRoot = mtm.getFakeRoot(term)
self.progress.total = 10
for index, release in enumerate(results):
self.progress.addItem()
self._createAlbum(fakeRoot, mediatype, release)
if index >= 10:
break
return fakeRoot
def _createAlbum(self, fakeRoot, mediaType, release):
artist = release.artists[0]
artistName = re.sub(r' \(\d{1,2}\)$', '', artist.name)
try:
artistElement = Element.getWhereField(mediaType, 'Artist', {'id': artistName}, self.tag, fakeRoot)
except Element.DoesNotExist:
artistElement = Element()
artistElement.mediaType = mediaType
artistElement.parent = fakeRoot
artistElement.type = 'Artist'
artistElement.setField('name', artistName, self.tag)
artistElement.setField('id', artistName, self.tag)
artistElement.saveTemp()
try:
albumElement = Element.getWhereField(mediaType, 'Album', {'id': release.data['id']}, self.tag, artistElement)
print "we have", albumElement
except Element.DoesNotExist:
albumElement = Element()
albumElement.mediaType = mediaType
albumElement.parent = artistElement
albumElement.type = 'Album'
albumElement.setField('name', release.title, self.tag)
albumElement.setField('year', release.data['year'], self.tag)
albumElement.setField('id', release.data['id'], self.tag)
if 'images' in release.data:
for img in release.data['images']:
if img['uri']:
albumElement.setField('cover_image', img['uri'], self.tag)
break
albumElement.saveTemp()
for track in release.tracklist:
trackElement = Element()
trackElement.mediaType = mediaType
trackElement.parent = albumElement
trackElement.type = 'Song'
trackElement.setField('title', track.title, self.tag)
trackElement.setField('length', track.duration, self.tag)
trackElement.setField('position', track.position, self.tag)
trackElement.saveTemp()
albumElement.downloadImages()
def getElement(self, id, element=None, tag=None):
mt = MediaType.get(MediaType.identifier == 'de.lad1337.music')
mtm = common.PM.getMediaTypeManager('de.lad1337.music')[0]
fakeRoot = mtm.getFakeRoot('%s ID: %s' % (self.tag, id))
release = self.d.release(id)
master = self.d.master(id)
# print release
# print master
for release_type in (master, release):
try:
self._createAlbum(fakeRoot, mt, release_type)
except AttributeError:
pass
for ele in fakeRoot.decendants:
if element is None:
if ele.getField('id', self.tag) == id:
return ele
else:
"""print ele.getField('id', self.tag), id, ele.getField('id', self.tag) == id
print element.type, ele.type, element.type == ele.type
print element.parent.getField('id', self.tag), ele.parent.getField('id', self.tag), element.parent.getField('id', self.tag) == ele.parent.getField('id', self.tag)
print '#############'"""
if ele.getField('id', self.tag) == id and element.type == ele.type and element.parent.getField('id', self.tag) == ele.parent.getField('id', self.tag):
return ele
else:
return False