Ejemplo n.º 1
0
def insert(events, loc):
    """Iterate through event list and insert into DB"""
    n = 0
    type_errors = 0
    for e in events:
        try:
            lfm_id = e['id']
        except TypeError, te:
            _log.warn("Skipped TypeError on badly formatted event data: %s" % e)
            type_errors += 1
            continue
        existing_event = is_event_in_db(lfm_id)
        if existing_event and not _UPDATE_EXISTING:
            continue # skip events that already exist in DB
        image, image_content = None, None
        image = get_image_url(e['image'])
        if not image:
            continue # We can't work without a badge image
        event_date = e['startDate'] # format: Tue, 07 Jul 2009
        dtx = event_date.split(' ')
        event_date = ' '.join(dtx[:4]) # discard any time component
        dt = time.strptime(event_date, _DATE_FORMAT)
        event_date = date(*dt[:3])
        if event_date < _TODAY:
            continue # Ignore past events
        venue_url = e['venue']['url']
        if not venue_url:
            continue # Don't accept bad venues

        title = e['title']
        website = e['website']
        desc = e['description']
        headliner = e['artists'].get('headliner', '')
        artist_list = set([a for a in e['artists']['artist'] if len(a) > 1 and a != headliner])
        if not artist_list:
            if headliner:
                artist_list = []
            else:
                artist_list = [title]
        full_list = list(artist_list)
        if headliner:
            full_list.insert(0, headliner)
        csv_artist_list = full_list and u",".join(full_list) or u""
        quoted_list = [u'"%s"' % a for a in full_list]
        hashtag = u",".join(quoted_list)
        art_html = [u"<li>%s</li>" % a for a in artist_list]        
        if headliner:
            art_html.insert(0, u'<li class="headliner">%s</li>' % headliner)
        art_html = u"<h4 class='artist_title'>Artists</h4><ul class='artist_list'>%s</ul>\n" % u"\n".join(art_html)
        desc = u"%s%s" % (art_html, desc)
        venue = e['venue']['name'].strip()
        venue_address = e['venue']['location']['street'].strip()
        venue_city = e['venue']['location']['city'].strip()
        venue_zip = e['venue']['location']['postalcode'].strip()
        if headliner and venue and headliner == title:
            # Change title format to:
            # headliner at venue name! mm/dd
            mm_dd = event_date.strftime("%m/%d")
            title = u"%s at %s! %s" % (headliner, venue, mm_dd)
            if len(title) > 120:
                title = u"%s.." % title[:118]
        lat, lng = None, None
        source = ''
        if e['venue']['location'].get("geo:point", {}).get("geo:lat", None):
            lat = e['venue']['location']["geo:point"]["geo:lat"]
            lng = e['venue']['location']["geo:point"]["geo:long"]
            lat = str(round(float(lat), 3))
            lng = str(round(float(lng), 3))
            source = u"%s,%s" % (lat, lng)
        tz = e['venue']['location'].get('timezone', '')
        event_time = e.get('startTime', "21:00")
        has_start_time = 'startTime' in e
        image_content = ContentFile(get_raw_image(image))
        fname, ext = os.path.splitext(image)
        fname = u'%s%s' % (uuid4().hex[::2], ext)        
        vn, created = Venue.objects.get_or_create(
            name=venue[:255],
            geo_loc=source,
            defaults=dict(
                source='last-fm',
                address=venue_address[:255],
                city=venue_city[:255],
                zip_code=venue_zip[:12],
                venue_url=venue_url[:255],
            )
        )
        if False and not created and vn.source != 'last-fm':
            # Update empty fields
            dirty = False
            if not vn.address:
                vn.address = venue_address[:255]
                dirty = True
            if not vn.city:
                vn.city = venue_city[:255]
                dirty = True
            if not vn.zip_code:
                vn.zip_code = venue_zip[:12]
                dirty = True
            if not vn.venue_url:
                vn.venue_url = venue_url[:255]
                dirty = True
            if dirty:
                vn.save()
        if existing_event:
            # only update title on this existing event
            existing_event.title = title[:120]
            super(Event, existing_event).save()
            _x.debug("Updated title for %s", existing_event)
        else:
            user_profile = get_artist_user_profile(headliner)
            ex = Event(
                lastfm_id = lfm_id,
                artist=_ARTIST,
                creator=user_profile,
                is_submitted=True,
                is_approved=True,
                title=title[:120],
                url=(u"%s-%s" % (slugify(title)[:30], uuid4().hex[::4]))[:35],
                description=desc,
                venue=vn,
                event_date=event_date,
                event_start_time=event_time,
                event_timezone=tz,
                hashtag=u'', # hashtag[:250], # don't fetch tweets for last.fm generated events (3/23/2010)
                location=loc,
                headliner=headliner[:200] or u'',
                artists=csv_artist_list[:250] or u'',
                ext_event_source='last.fm',
            )
            ex._has_start_time = ex.event_start_time and has_start_time
            ey = try_merge(ex)
            if ey:
                # new event is a duplicate; hide it
                ex.is_deleted = True
                _x.warn("Merged %s into %s", ex, ey)
            ex.image.save(fname, image_content, save=False)
            ex._create_resized_images(raw_field=None, save=False)
            ex.save()
            ex.attendee_set.get_or_create(attendee_profile=user_profile)
            _x.debug("Saved %s", ex)
        n += 1