Exemple #1
0
def _convert_feed(user, serv, feed, since, screen_name):
    """Take the json and convert to ServiceItems"""

    items = []

    for status in feed:
        # we are interested in tweets since
        if status.created_at.date() > since:
            item = ServiceItem()
            item.location = {}
            twitter_text = TwitterText(status.text)
            twitter_text.autolink.auto_link_usernames_or_lists()
            twitter_text.autolink.auto_link_hashtags()
            item.body = unicode(twitter_text.text)

            if re.search("http://yfrog.com/\S*", item.body) \
               or re.search("http://twitpic.com/\S*", item.body):
                item.pic_link = True

            item.created = status.created_at
            item.link_back = "http://twitter.com/%s/status/%s" % \
                (screen_name, str(status.id))
            if status.geo:
                item.location['lat'] = status.geo['coordinates'][0]
                item.location['long'] = status.geo['coordinates'][1]
            item.service = serv
            item.user = user
            items.append(item)
            
    return items
Exemple #2
0
    def _convert_feed(self, json, since):
        """Take the raw json from the feed and convert it to ServiceItems.
        """

        items = []
        if json and json['response'].has_key('checkins'):
            for checkin in json['response']['checkins']['items']:
                created = datetime.fromtimestamp(checkin['createdAt'])

                if created.date() >= since:
                    item = ServiceItem()
                    item.location = {}
                    item.link_back = 'http://foursquare.com/venue/%s' % (checkin['venue']['id'],)
                    item.title = checkin['venue']['name']

                    if checkin.has_key('shout') and checkin['shout']:
                        item.body = checkin['shout']
                    else:
                        if len(checkin['venue']['categories']) > 0 and checkin['venue']['location'].has_key('city'):
                            item.body = "A %s in %s" % (checkin['venue']['categories'][0]['name'], checkin['venue']['location']['city'])
                        elif checkin['venue'].has_key('city'):
                            item.body = "In %s" % (checkin['venue']['location']['city'])
                        else:
                            item.body = "%s" % (checkin['venue']['name'])

                    if checkin['venue']['location'].has_key('lat') and checkin['venue']['location']['lng']:
                        item.location['lat'] = checkin['venue']['location']['lat']
                        item.location['long'] = checkin['venue']['location']['lng']

                    item.created = created
                    item.service = self.service
                    
                    if checkin.has_key('isMayor'):
                        item.is_mayor = checkin['isMayor']
                    else:
                        pass

                    if checkin['venue'].has_key('categories') and len(checkin['venue']['categories']) > 0:
                        item.icon = checkin['venue']['categories'][0]['icon']
                        item.categories = checkin['venue']['categories']

                    items.append(item)
                    del(item)

        return items
Exemple #3
0
    def _create_checkin(self, checkin):
        """Convert a raw checkin into a service item"""
        
        item = ServiceItem()
        created = None
        if checkin.has_key('createdAt'):
            created = datetime.fromtimestamp(checkin['createdAt'])
        item.location = {}
        item.link_back = 'http://foursquare.com/venue/%s' % (checkin['venue']['id'],)
        item.title = checkin['venue']['name']
        
        if checkin['venue'].has_key('location') and checkin['venue']['location'].has_key('city'):
            item.city = checkin['venue']['location']['city']

        if checkin.has_key('shout') and checkin['shout']:
            item.body = checkin['shout']
        else:
            if len(checkin['venue']['categories']) > 0 and checkin['venue']['location'].has_key('city'):
                item.body = "A %s in %s" % (checkin['venue']['categories'][0]['name'], checkin['venue']['location']['city'])
            elif checkin['venue'].has_key('city'):
                item.body = "In %s" % (checkin['venue']['location']['city'])
            else:
                item.body = "%s" % (checkin['venue']['name'])

        if checkin['venue']['location'].has_key('lat') and checkin['venue']['location']['lng']:
            item.location['lat'] = checkin['venue']['location']['lat']
            item.location['long'] = checkin['venue']['location']['lng']

        if created:
            item.created = created
        item.service = self.service
        
        if checkin.has_key('isMayor'):
            item.is_mayor = checkin['isMayor']
        else:
            pass

        if checkin['venue'].has_key('categories') and len(checkin['venue']['categories']) > 0:
            item.icon = checkin['venue']['categories'][0]['icon']
            item.categories = checkin['venue']['categories']
        
        return item
Exemple #4
0
    def get_upcoming_gigs(self, since, artist=None):
        """Return a list of up coming gigs for the user.
        """

        items = []

        gigs = get_data(
            self.service,
            'http://ws.audioscrobbler.com/2.0/?method=artist.getevents&artist=%s&api_key=%s&format=json' % (
                artist.replace(' ', '+'),
                self.service.app.auth_settings['api_key']
            ),
            disable_oauth=True
        )

        items = []

        if gigs and gigs.has_key('events') and gigs['events'].has_key('event') :
            for gig in gigs['events']['event']:
                item = ServiceItem()
                item.location = {}
                try:
                    if gig.has_key('venue') and \
                       gig['venue'].has_key('name') and \
                       gig.has_key('startDate'):
                        item.venue_name = gig['venue']['name']
                        item.event_url = gig['url']
                        item.date = datetime.strptime(gig['startDate'], '%a, %d %b %Y %H:%M:%S')

                        if gig['venue'].has_key('location') and \
                           gig['venue']['location'].has_key('geo:point'):
                            item.location['long'] = \
                                gig['venue']['location']['geo:point']['geo:long']
                            item.location['lat'] = \
                                gig['venue']['location']['geo:point']['geo:lat']

                        items.append(item)
                except:
                    pass
        return items
Exemple #5
0
def _convert_feed(serv, user, json, since):
    """Take the raw json from the feed and convert it to
    ServiceItems."""
    
    items = []
    
    if json and json.has_key('checkins'):
        for checkin in json['checkins']:

            # grab the +0000 bit on the end of the date and use it make the time right
            offset = checkin['created'].rsplit(' ')[-1]
            offset = offset[1:]
            offset = offset[:2]

            time_offset = timedelta(hours=int(offset))
            
            created = datetime.strptime(checkin['created'].replace(' +0000', ''), '%a, %d %b %y %H:%M:%S') #'Fri, 04 Feb 11 12:42:38 +0000'
            created = created + time_offset
            
            if created.date() >= since:
                item = ServiceItem()
                item.location = {}
                item.link_back = 'http://foursquare.com/venue/%s' % checkin['venue']['id']
                item.title = checkin['venue']['name']
                if checkin.has_key('shout') and checkin['shout']:
                    item.body = checkin['shout']
                else:
                    item.body = checkin['venue']['city']
                if checkin['venue'].has_key('geolat') and checkin['venue']['geolat']:
                    item.location['lat'] = checkin['venue']['geolat']
                    item.location['long'] = checkin['venue']['geolong']
                item.created = created
                item.service = serv
                if checkin['venue'].has_key('primarycategory'):
                    item.icon = checkin['venue']['primarycategory']['iconurl']
                item.user = user
                items.append(item)
                del(item)
    
    return items
Exemple #6
0
def get_upcoming_gigs(user, since, model_instance=None, artist=None):
    """Return a list of up coming gigs for the user."""

    serv = model_instance or get_model_instance(user, __name__)
    items = []

    access_token = AccessToken.objects.get(service=serv)

    gigs = get_data(
        serv,
        'http://ws.audioscrobbler.com/2.0/?method=artist.getevents&artist=%s&api_key=%s&format=json'
            % (artist.replace(' ', '+'), access_token.api_token,),
        disable_oauth=True
    )

    items = []

    if gigs and gigs.has_key('events') and gigs['events'].has_key('event') :
        for gig in gigs['events']['event']:
            item = ServiceItem()
            item.location = {}
            try:
                if gig.has_key('venue') and \
                   gig['venue'].has_key('name') and \
                   gig.has_key('startDate'):
                    item.venue_name = gig['venue']['name']
                    item.event_url = gig['url']
                    item.date = gig['startDate']
                    if gig['venue'].has_key('location') and \
                       gig['venue']['location'].has_key('geo:point'):
                        item.location['long'] = \
                            gig['venue']['location']['geo:point']['geo:long']
                        item.location['lat'] = \
                            gig['venue']['location']['geo:point']['geo:lat']
                    items.append(item)
            except:
                pass
    return items
Exemple #7
0
    def get_stats_items(self, since):
        """Fetch and normalise the updates from the service and generate stats.
        """

        self.flickr = flickrapi.FlickrAPI(self.service.app.auth_settings['api_key'])
        photos = self._get_service_items(since)

        items = []

        if photos:
            for photo in photos:

                item = ServiceItem()

                # Info about the pic
                pic = self.flickr.photos_getInfo(photo_id=photo['id'], format='json', nojsoncallback='1')
                pic_json = simplejson.loads(pic)

                # Info about how the pic was taken
                exif = self.flickr.photos_getExif(photo_id=photo['id'], format='json', nojsoncallback ='1')
                exif_json = simplejson.loads(exif)
                item.camera_make, item.camera_model = self._extract_camera_type(exif_json)
                item.title = pic_json['photo']['title']['_content']
                item.body = pic_json['photo']['description']['_content']

                # Use date from when the photo was uploaded to flickr NOT when it was taken
                item.created = datetime.fromtimestamp(float(pic_json['photo']['dates']['posted'])) #u'posted': u'1300054696'

                item.link_back = pic_json['photo']['urls']['url'][0]['_content']
                item.tags = pic_json['photo']['tags']['tag']
                item.favorite = pic_json['photo']['isfavorite']

                # Add views
                item.views = pic_json['photo']['views']

                # Add tags
                item.tags = pic_json['photo']['tags']['tag']

                item.number_of_comments = pic_json['photo']['comments']['_content']

                item.url_thumb = "http://farm%s.static.flickr.com/%s/%s_%s_t.jpg" % (
                    pic_json['photo']['farm'],
                    pic_json['photo']['server'],
                    pic_json['photo']['id'],
                    pic_json['photo']['secret']
                )

                item.url_small = "http://farm%s.static.flickr.com/%s/%s_%s_m.jpg" % (
                    pic_json['photo']['farm'],
                    pic_json['photo']['server'],
                    pic_json['photo']['id'],
                    pic_json['photo']['secret']
                )

                item.body = '<br/><img src="%s" />' % (item.url_thumb,)

                # Add location
                item.location = {}
                if pic_json['photo'].has_key('location'):
                    item.location['lat'] = pic_json['photo']['location']['latitude']
                    item.location['long'] = pic_json['photo']['location']['longitude']

                item.service = self.service

                items.append(item)
        return items
Exemple #8
0
def get_stats_items(user, since, model_instance):
    """Fetch and normalise the updates from the service and generate stats."""

    serv = model_instance or get_model_instance(user, __name__)
    access_token = AccessToken.objects.get(service=serv)
    
    flickr = flickrapi.FlickrAPI(access_token.api_token)
    photos = _get_service_items(user, model_instance, flickr, serv, access_token)
    
    items = []
    
    if photos and photos['photos'].has_key('photo'):
        for photo in photos['photos']['photo']:

            item = ServiceItem()            
            
            # info about the pic
            pic = flickr.photos_getInfo(photo_id=photo['id'], format='json', nojsoncallback='1')
            pic_json = simplejson.loads(pic)

            # info about how the pic was taken
            exif = flickr.photos_getExif(photo_id=photo['id'], format='json', nojsoncallback ='1')
            exif_json = simplejson.loads(exif)
            item.camera_make = _extract_camera_type(exif_json)
    
            item.location = {}
            item.title = pic_json['photo']['title']['_content']
    
            # use date from when the photo was uploaded to flickr NOT when it was taken
            item.created = datetime.fromtimestamp(float(pic_json['photo']['dates']['posted'])) #u'posted': u'1300054696'
            
            item.service = serv
    
            item.link_back = pic_json['photo']['urls']['url'][0]['_content']
            item.tags = pic_json['photo']['tags']['tag']
            item.favorite = pic_json['photo']['isfavorite']
    
            # add views
            item.views = pic_json['photo']['views']
    
            # add tags
            item.tags = pic_json['photo']['tags']['tag']
    
            if pic_json['photo']['comments']['_content'] == 0:
                item.number_of_comments = "No comments"
            else:
                item.number_of_comments = pic_json['photo']['comments']['_content']
    
            item.url_thumb = "http://farm%s.static.flickr.com/%s/%s_%s_t.jpg" % \
                         (pic_json['photo']['farm'],
                          pic_json['photo']['server'],
                          pic_json['photo']['id'],
                          pic_json['photo']['secret'])
    
            item.url_small = "http://farm%s.static.flickr.com/%s/%s_%s_m.jpg" % \
                         (pic_json['photo']['farm'],
                          pic_json['photo']['server'],
                          pic_json['photo']['id'],
                          pic_json['photo']['secret'])
    
            item.body = "<br/><img src='" + item.url_thumb +"'/>"
            # add location
            if pic_json['photo'].has_key('location'):
                item.location['lat'] = pic_json['photo']['location']['latitude']
                item.location['long'] = pic_json['photo']['location']['longitude']
    
            item.user = user
            items.append(item)
                    
        return items
Exemple #9
0
    def get_stats_items(self, since):
        """Fetch and normalise the updates from the service and generate stats.
        """

        user_id = self._get_username()
        if not user_id:
            return
        
        photos = self._get_oauth_v1('http://api.flickr.com/services/rest/?method=flickr.people.getPhotos&user_id=%s&format=json&nojsoncallback=1&min_upload_date=%s' % (user_id, since.strftime('%Y-%m-%d+%H:%M:%S')))

        items = []

        if photos and int(photos['photos']['total']) > 0:
            for photo in photos['photos']['photo']:

                item = ServiceItem()

                # Info about the pic
                pic_json = self._get_oauth_v1('http://api.flickr.com/services/rest/?method=flickr.photos.getInfo&user_id=%s&format=json&nojsoncallback=1&photo_id=%s' % (user_id, photo['id']))

                # Info about how the pic was taken
                exif_json = self._get_oauth_v1('http://api.flickr.com/services/rest/?method=flickr.photos.getExif&user_id=%s&format=json&nojsoncallback=1&photo_id=%s' % (user_id, photo['id']))
                item.camera_make, item.camera_model = self._extract_camera_type(exif_json)
                item.title = pic_json['photo']['title']['_content']
                item.body = pic_json['photo']['description']['_content']

                # Use date from when the photo was uploaded to flickr NOT when it was taken
                item.created = datetime.fromtimestamp(float(pic_json['photo']['dates']['posted'])) #u'posted': u'1300054696'

                item.link_back = pic_json['photo']['urls']['url'][0]['_content']
                item.tags = pic_json['photo']['tags']['tag']
                item.favorite = pic_json['photo']['isfavorite']

                # Add views
                item.views = pic_json['photo']['views']

                # Add tags
                item.tags = pic_json['photo']['tags']['tag']

                item.number_of_comments = pic_json['photo']['comments']['_content']

                item.url_thumb = "http://farm%s.static.flickr.com/%s/%s_%s_t.jpg" % (
                    pic_json['photo']['farm'],
                    pic_json['photo']['server'],
                    pic_json['photo']['id'],
                    pic_json['photo']['secret']
                )

                item.url_small = "http://farm%s.static.flickr.com/%s/%s_%s_m.jpg" % (
                    pic_json['photo']['farm'],
                    pic_json['photo']['server'],
                    pic_json['photo']['id'],
                    pic_json['photo']['secret']
                )

                item.body = '<br/><img src="%s" />' % (item.url_thumb,)

                # Add location
                item.location = {}
                if pic_json['photo'].has_key('location'):
                    item.location['lat'] = pic_json['photo']['location']['latitude']
                    item.location['long'] = pic_json['photo']['location']['longitude']

                item.service = self.service

                items.append(item)
        return items