def from_youtube(cls, track): snippet = track['snippet'] player = track['player'] statistics = track['statistics'] duration = isodate.parse_duration(track['contentDetails']['duration']) artist = Account( id=snippet['channelId'], provider_id='youtube', image=None, title=snippet['channelTitle']) return cls( id=track['id'], provider_id='youtube', account=artist, aspect_ratio=( float(player['embedHeight']) / float(player['embedWidth'])), duration=int(duration.total_seconds()), favourite_count=statistics.get('likeCount', 0), image=Image.from_youtube(snippet.get('thumbnails')), play_count=statistics.get('viewCount', 0), title=snippet['title'], created=datetime.datetime.strptime( snippet['publishedAt'], '%Y-%m-%dT%H:%M:%S.%fZ'))
async def search(self, ids, kw, fields=Available): response = await self.fetch( ids['track_provider_id'], '/tracks/{}/comments'.format(ids['track_id'])) comments = tornado.escape.json_decode(response.body) entities = [] for comment in comments: user = comment['user'] writer = Account(id=user['id'], provider_id=ids['track_provider_id'], title=user['username'], image=Image.from_soundcloud( user.get('avatar_url'))) entity = TrackComment(id=comment['id'], provider_id=ids['track_provider_id'], body=comment['body'], timestamp=comment['timestamp'], track_id=ids['track_id'], track_provider_id=ids['track_provider_id'], account=writer, created=datetime.datetime.strptime( comment['created_at'], self.DATE_FORMAT)) entities.append(entity) account = self.get_account(entity.provider_id) self.policy.grant_read(account, entities, fields) return entities
def test_auth_controller_should_skip_updating_refresh_token(db, current_user): controller = CloudplayerController(db, current_user) controller.account = account = Account() access_info = {'access_token': '1234'} controller._update_access_info(access_info) assert account.access_token == '1234' assert account.refresh_token is None assert account.token_expiration is None
def test_account_model_can_be_created(current_user, db): account = Account(id='abcd1234', user_id=current_user['user_id'], provider_id='cloudplayer') db.add(account) db.commit() db.expunge(account) assert db.query(Account).get(('abcd1234', 'cloudplayer'))
def _create_account(self, account_info): self.account = Account(id=str(account_info['id']), user_id=self.current_user['user_id'], favourite=Favourite( id=str(account_info['id']), provider_id=self.__provider__), provider_id=self.__provider__) self.db.add(self.account)
def test_auth_controller_syncs_cloudplayer_profile_from_account_info( db, current_user, account): # Check that account with unset attributes is synced account.title = None account.image = None controller = CloudplayerController(db, current_user) controller.account = Account( id='bar', title='foo', image=Image(large='image.co/large'), provider_id='soundcloud', user_id=current_user['user_id']) controller._sync_cloudplayer_profile() assert account.title == 'foo' assert account.image.small == controller.account.image.small assert account.image.medium == controller.account.image.medium assert account.image.large == controller.account.image.large # Check that account with set attributes is left alone controller.account = Account(title='bar', image=None) controller._sync_cloudplayer_profile() assert account.title == 'foo' assert account.image is not None
def test_auth_controller_updates_access_info_to_account(db, current_user): controller = CloudplayerController(db, current_user) controller.account = account = Account() access_info = { 'access_token': '1234', 'refresh_token': '5678', 'expires_in': 3600} controller._update_access_info(access_info) assert account.access_token == '1234' assert account.refresh_token == '5678' expiration = account.token_expiration - datetime.datetime.utcnow() assert datetime.timedelta( seconds=3590) < expiration < datetime.timedelta(seconds=3610)
def account(db, user): from cloudplayer.api.model.account import Account from cloudplayer.api.model.favourite import Favourite from cloudplayer.api.model.image import Image account = Account(id=str(user.id), provider_id='cloudplayer', favourite=Favourite(), user_id=user.id, image=Image( small='https://image.small/{}'.format(randstr()), medium='https://image.medium/{}'.format(randstr()), large='https://image.large/{}'.format(randstr())), title=randstr()) db.add(account) db.commit() return account
def read(self, ids, fields=Available): params = { 'id': ids['id'], 'part': 'snippet,player,contentDetails,statistics', 'maxWidth': '320' } response = yield self.fetch(ids['provider_id'], '/videos', params=params) track_list = tornado.escape.json_decode(response.body) if not track_list['items']: return track = track_list['items'][0] snippet = track['snippet'] player = track['player'] statistics = track['statistics'] duration = isodate.parse_duration(track['contentDetails']['duration']) image = Image(small=snippet['thumbnails']['default']['url'], medium=snippet['thumbnails']['medium']['url'], large=snippet['thumbnails']['high']['url']) artist = Account(id=snippet['channelId'], provider_id=ids['provider_id'], image=None, title=snippet['channelTitle']) entity = Track(id=ids['id'], provider_id=ids['provider_id'], account=artist, aspect_ratio=(float(player['embedHeight']) / float(player['embedWidth'])), duration=int(duration.total_seconds()), favourite_count=statistics['likeCount'], image=image, play_count=statistics['viewCount'], title=snippet['title'], created=datetime.datetime.strptime( snippet['publishedAt'], self.DATE_FORMAT)) account = self.get_account(entity.provider_id) self.policy.grant_read(account, entity, fields) return entity
def from_soundcloud(cls, track): user = track['user'] artist = Account( id=user['id'], provider_id='soundcloud', title=user['username'], image=Image.from_soundcloud(user.get('avatar_url'))) return cls( id=track['id'], provider_id='soundcloud', account=artist, aspect_ratio=1.0, duration=int(track['duration'] / 1000.0), favourite_count=track.get('favoritings_count', 0), image=Image.from_soundcloud(track.get('artwork_url')), play_count=track.get('playback_count', 0), title=track['title'], created=datetime.datetime.strptime( track['created_at'], '%Y/%m/%d %H:%M:%S %z'))
def test_controller_should_set_current_user_on_claimed_token(db, current_user): entity = Token() account = Account( id='1234', user=User(), provider_id='cloudplayer') entity.account = account entity.claimed = True db.add_all((entity, entity.account, entity.account.user)) db.commit() assert entity.account.id != current_user['cloudplayer'] controller = TokenController(db, current_user) token = yield controller.read({'id': entity.id}) assert token.claimed is True assert token.id == entity.id assert account.user.id == current_user['user_id'] db.refresh(entity) assert entity.account_id is None assert entity.account_provider_id is None
def load_user(self): try: user = jwt.decode(self.get_cookie(self.settings['jwt_cookie'], ''), self.settings['jwt_secret'], algorithms=['HS256']) return user, user.copy() except jwt.exceptions.InvalidTokenError: new_user = User() self.db.add(new_user) self.db.commit() new_account = Account(id=str(new_user.id), provider_id='cloudplayer', favourite=Favourite(), user_id=new_user.id) self.db.add(new_account) self.db.commit() user = {p: None for p in self.settings['providers']} user['cloudplayer'] = new_account.id user['user_id'] = new_user.id return None, user
def read(self, ids, fields=Available): response = yield self.fetch(ids['provider_id'], '/tracks/{}'.format(ids['id'])) track = tornado.escape.json_decode(response.body) user = track['user'] if track.get('artwork_url'): image = Image( small=track['artwork_url'] or None, medium=track['artwork_url'].replace('large', 't300x300'), large=track['artwork_url'].replace('large', 't500x500')) else: image = None artist = Account( id=user['id'], provider_id=ids['provider_id'], title=user['username'], image=Image(small=user['avatar_url'], medium=user['avatar_url'].replace('large', 't300x300'), large=user['avatar_url'].replace('large', 't500x500'))) entity = Track(id=ids['id'], provider_id=ids['provider_id'], account=artist, aspect_ratio=1.0, duration=int(track['duration'] / 1000.0), favourite_count=track.get('favoritings_count', 0), image=image, play_count=track.get('playback_count', 0), title=track['title'], created=datetime.datetime.strptime( track['created_at'], self.DATE_FORMAT)) account = self.get_account(entity.provider_id) self.policy.grant_read(account, entity, fields) return entity
def test_descendancy_should_be_inferred_by_children_property(): account = Account(id=13) target = mock.Mock() target.children = [account] assert Child(target) == Principal(account) assert Child(target) != Principal(Account(id=99))
def test_ownership_should_be_inferred_by_account_property(): account = Account(id=55) target = mock.Mock() target.account = account assert Owner(target) == Principal(account) assert Owner(target) != Principal(Account(id=99))
def _create_account(self, account_info): self.account = Account(id=str(account_info['id']), user_id=self.current_user['user_id'], provider_id=self.PROVIDER_ID) self.db.add(self.account)
def test_parenthood_should_be_inferred_by_parent_property(): account = Account(id=13) target = mock.Mock() target.parent.account = account assert Parent(target) == Principal(account) assert Parent(target) != Principal(Account(id=99))
def account(self): # XXX: Check session for this account id without querying from cloudplayer.api.model.account import Account if self.account_id and self.provider_id: return Account(id=self.account_id, provider_id=self.provider_id)