Skip to content

Commit

Permalink
Merge branch 'pics'
Browse files Browse the repository at this point in the history
Closes: emillon#6
  • Loading branch information
emillon committed Mar 15, 2015
2 parents 9d489a2 + b02c556 commit b797b51
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 8 deletions.
1 change: 1 addition & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ Changelog

0.0.3
-----
* Pictures can be uploaded and retrieved (#6)

0.0.2
-----
Expand Down
15 changes: 12 additions & 3 deletions mixcloud/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def me(self):
r = requests.get(url)
return User.from_json(r.json(), m=self)

def upload(self, cloudcast, mp3file):
def upload(self, cloudcast, mp3file, picturefile=None):
url = '{root}/upload/'.format(root=self.api_root)
payload = {'name': cloudcast.name,
'percentage_music': 100,
Expand All @@ -51,10 +51,14 @@ def upload(self, cloudcast, mp3file):
for num, tag in enumerate(cloudcast.tags):
payload['tags-%s-tag' % num] = tag

files = {'mp3': mp3file}
if picturefile is not None:
files['picture'] = picturefile

r = requests.post(url,
data=payload,
params={'access_token': self.access_token},
files={'mp3': mp3file},
files=files,
)
return r

Expand Down Expand Up @@ -110,7 +114,7 @@ def cloudcasts(self, limit=None, offset=None):
class Cloudcast(object):

def __init__(self, key, name, sections, tags,
description, user, created_time, m=None):
description, user, created_time, pictures=None, m=None):
self.key = key
self.name = name
self.tags = tags
Expand All @@ -119,6 +123,7 @@ def __init__(self, key, name, sections, tags,
self.user = user
self.created_time = created_time
self.m = m
self.pictures = pictures

@staticmethod
def from_json(d, m=None):
Expand All @@ -137,6 +142,7 @@ def from_json(d, m=None):
desc,
user,
created_time,
pictures=d.get('pictures'),
m=m,
)

Expand Down Expand Up @@ -166,6 +172,9 @@ def description(self):
self._load()
return self._description

def picture(self):
return self.pictures['large']

@staticmethod
def from_yml(f, user):
setup_yaml()
Expand Down
3 changes: 3 additions & 0 deletions mixcloud/mock.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ def _register_cloudcast_only(self, user, cloudcast):
'name': user.name,
},
'created_time': cloudcast.created_time.isoformat(),
'pictures': {
'large': 'http://httpbin.org/status/418',
}
}
httpretty.register_uri(httpretty.GET, url, body=json.dumps(cc_data))
return cc_data
Expand Down
13 changes: 8 additions & 5 deletions tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,15 @@ def parse_tracklist(s):

class TestMixcloud(unittest.TestCase):

@classmethod
def setUpClass(cls):
httpretty.enable()

def setUp(self):
self.m = mixcloud.Mixcloud()
httpretty.reset()
httpretty.enable()
self.mc = MockServer()

def tearDown(self):
httpretty.disable()

def testArtist(self):
self.mc.register_artist(afx)
resp = self.m.artist('aphex-twin')
Expand All @@ -101,6 +101,7 @@ def testCloudcast(self):
self.assertEqual(sec.track.artist.name, 'Time of your life')
self.assertEqual(cc.tags, ['Funky house', 'Funk', 'Soul'])
self.assertEqual(cc.description(), 'Bla bla')
self.assertTrue(cc.picture().startswith('http'))
self.assertEqual(cc.user.key, 'spartacus')
self.assertEqual(cc.user.name, 'Spartacus')

Expand All @@ -124,6 +125,7 @@ def testUpload(self):
def upload_callback(request, uri, headers):
data = parse_multipart(request.body)
self.assertIn('mp3', data)
self.assertIn('picture', data)
name = data['name']
key = mixcloud.slugify(name)
sections, tags = parse_headers(data)
Expand All @@ -137,7 +139,8 @@ def upload_callback(request, uri, headers):

self.mc.handle_upload(upload_callback)
mp3file = io.StringIO(u'\x00' * 30)
r = self.m.upload(partytime, mp3file)
picturefile = io.StringIO(u'\x00' * 30)
r = self.m.upload(partytime, mp3file, picturefile=picturefile)
self.assertEqual(r.status_code, 200)
me = self.m.me()
cc = me.cloudcast('party-time')
Expand Down

0 comments on commit b797b51

Please sign in to comment.