Exemplo n.º 1
0
    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'))
Exemplo n.º 2
0
    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
Exemplo n.º 3
0
def test_image_model_can_be_created(db):
    image = Image(large='http://img.host/large.jpg')
    db.add(image)
    db.commit()
    image_id = image.id
    db.expunge(image)
    assert db.query(Image).get(image_id)
Exemplo n.º 4
0
 def _update_account_profile(self, account_info):
     image_url = account_info.get('avatar_url')
     self.account.image = Image(
         small=image_url,
         medium=image_url.replace('large', 't300x300'),
         large=image_url.replace('large', 't500x500'))
     self.account.title = account_info.get('username')
Exemplo n.º 5
0
 def _update_account_profile(self, account_info):
     snippet = account_info.get('snippet', {})
     thumbnails = snippet.get('thumbnails', {})
     self.account.image = Image(small=thumbnails.get('default',
                                                     {}).get('url'),
                                medium=thumbnails.get('medium',
                                                      {}).get('url'),
                                large=thumbnails.get('high', {}).get('url'))
     self.account.title = snippet.get('title')
Exemplo n.º 6
0
    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'))
Exemplo n.º 7
0
 def _sync_cloudplayer_profile(self):
     cloudplayer = self.db.query(Account).get(
         (self.current_user['cloudplayer'], 'cloudplayer'))
     if not cloudplayer:
         return
     if not cloudplayer.title:
         cloudplayer.title = self.account.title
     if not cloudplayer.image:
         cloudplayer.image = Image(small=self.account.image.small,
                                   medium=self.account.image.medium,
                                   large=self.account.image.large)
Exemplo n.º 8
0
def test_auth_controller_updates_account_from_info_dicts(db, current_user):
    controller = CloudplayerController(db, current_user)
    access_info = {'access_token': 'xyz'}
    account_info = {
        'id': 1234,
        'title': 'foo bar',
        'image': Image(large='image.co/large')
    }
    controller.update_account(access_info, account_info)
    assert sqlalchemy.orm.util.object_state(controller.account).persistent
    assert controller.account.id == '1234'
    assert controller.account.title == 'foo bar'
    assert controller.account.image.large == 'image.co/large'
    assert controller.account.access_token == 'xyz'
Exemplo n.º 9
0
    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
Exemplo n.º 10
0
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
Exemplo n.º 11
0
    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
Exemplo n.º 12
0
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
Exemplo n.º 13
0
 def _update_account_profile(self, account_info):
     snippet = account_info.get('snippet', {})
     self.account.image = Image.from_youtube(snippet.get('thumbnails'))
     self.account.title = snippet.get('title')
Exemplo n.º 14
0
 def _update_account_profile(self, account_info):
     self.account.image = Image.from_soundcloud(
         account_info.get('avatar_url'))
     self.account.title = account_info.get('username')