Exemplo n.º 1
0
 def __init__(self, id, **kwargs):
     super(Artist, self).__init__(**kwargs)
     self.client = CopilotClient(settings.COPILOT_USER)
     self.id = id
     self._get()
     self._assign_assets()
     self.events = EventManager(self.id)
Exemplo n.º 2
0
class Artist(object):
    def __init__(self, id, **kwargs):
        super(Artist, self).__init__(**kwargs)
        self.client = CopilotClient(settings.COPILOT_USER)
        self.id = id
        self._get()
        self._assign_assets()
        self.events = EventManager(self.id)

    def _get(self):
        data = self.client.get('artists/{id}'.format(id=self.id)).json()
        props = ['assets', 'bookerSimple', 'stageName', 'web', 'shortBio', 'longBio',
            'facebook', 'myspace', 'twitter', 'mainact']
        for prop in props:
            try:
                setattr(self, prop, data[prop])
            except KeyError:
                logger.warning("Property {prop} is missing in copilot API "
                    "response for Artist {id}".format(prop=prop, id=self.id))

    def _assign_assets(self):
        self.media = defaultdict(list)
        for asset in self.assets:
            for asset_tag in asset['tags']:
                self.media[asset_tag['tagName']].append(asset)

    def news(self, page=1):
        kwargs = {
            'page': page,
            'references.type': 'ARTIST',
            'references.id': self.id,
        }
        return self.client.get_paginated('news', **kwargs).json()

    def videos(self, page=1):
        return self.client.get_paginated('artists/{id}/videoEntries'.format(
            id=self.id), page=page).json()

    def records(self, page=1):
        return self.client.get_paginated('artists/{id}/records'.format(
            id=self.id), page=page).json()

    def images(self):
        for asset in self.assets:
            if asset['mimeType'].startswith('image'):
                yield asset
            continue
Exemplo n.º 3
0
 def render(self, context, instance, placeholder):
     client = CopilotClient(settings.COPILOT_USER)
     context['news'] = client.get(self.endpoint).json()['content']
     return context
Exemplo n.º 4
0
 def __init__(self, artist_id=None, **kwargs):
     super(EventManager, self).__init__(**kwargs)
     self.client = CopilotClient(settings.COPILOT_USER)
     self.artist_id = artist_id
Exemplo n.º 5
0
class EventManager(object):
    ALL_ENDPOINT = 'events/'
    ARTIST_ENDPOINT = 'artists/{id}/events/'
    SORTING = 'dateOfEvent,asc'
    FROM = 'from/{}/'
    ARTIST_FROM = '{}/'
    TO = 'to/{}/'
    ARTIST_TO = '{}/'

    def __init__(self, artist_id=None, **kwargs):
        super(EventManager, self).__init__(**kwargs)
        self.client = CopilotClient(settings.COPILOT_USER)
        self.artist_id = artist_id

    def _get_from(self):
        if self.artist_id:
            return self.ARTIST_FROM
        else:
            return self.FROM

    def _get_to(self):
        if self.artist_id:
            return self.ARTIST_TO
        else:
            return self.TO

    def _get_endpoint(self, **kwargs):
        if self.artist_id:
            endpoint = self._get_endpoint_for_artist(**kwargs)
        else:
            endpoint = self._get_endpoint_for_all(**kwargs)

        try:
            endpoint += self._get_from().format(kwargs['start_date'].isoformat())
            endpoint += self._get_to().format(kwargs['end_date'].isoformat())
        except KeyError:
            # no problem if start_date or end_date not given
            pass
        return endpoint

    def _get_endpoint_for_all(self, **kwargs):
        return self.ALL_ENDPOINT

    def _get_endpoint_for_artist(self, **kwargs):
        return self.ARTIST_ENDPOINT.format(id=self.artist_id)

    def _get_sorting(self):
        return self.SORTING

    def _get(self, endpoint):
        logger.debug('API call: {}'.format(endpoint))
        kwargs = {
            'page.sort': self._get_sorting(),
            'external': False,
        }
        events = self.client.get_paginated(endpoint, **kwargs).json()
        events['artists'] = {}
        for event in events['content']:
            try:
                event['dateOfEvent'] = datetime.strptime(event['dateOfEvent']+event['start'], '%Y-%m-%d%H:%M:%S.%f')
            except KeyError:
                try:
                    event['dateOfEvent'] = datetime.strptime(event['dateOfEvent'], '%Y-%m-%d')
                except KeyError:
                    pass
            for cast_item in event['cast']:
                try:
                    events['artists'][cast_item['artist']['id']]['events'].append(event)
                except KeyError:
                    events['artists'][cast_item['artist']['id']] = {
                        'artist': cast_item['artist'],
                        'events': [event, ]
                    }
        return events


    def _get_years(self):
        endpoint = self._get_endpoint()
        events = self._get(endpoint)
        years = {}
        for event in events:
            try:
                years[event['dateOfEvent'].year].append(event)
            except KeyError:
                years[event['dateOfEvent'].year] = [event, ]
        return years


    def year(self, year=None):
        """
        Return events for year `year`.
        """
        if year is None:
            year = datetime.now().year
        endpoint = self._get_endpoint(
            start_date=date(year, 1, 1),
            end_date=date(year, 12, 31)
        )
        return self._get(endpoint)

    def prevnext(self, year=None):
        """
        Return events for year `year` and one year before and after.
        """
        if year is None:
            year = datetime.now().year
        endpoint = self._get_endpoint(
            start_date=date(year-1, 1, 1),
            end_date=date(year+1, 12, 31)
        )
        return self._get(endpoint)

    @property
    def years(self):
        try:
            return self._years
        except AttributeError:
            self._years = self._get_years()
            return self._years

    def all(self):
        """
        Return all events.
        """
        return self._get(self._get_endpoint())

    def upcoming(self):
        endpoint = self._get_endpoint(
            start_date=datetime.now().date())
        return self._get(endpoint)

    def __str__(self):
        if self.artist_id:
            return 'EventManager for {}'.format(self.artist_id)
        else:
            return 'EventManager'