Example #1
0
def _create_or_update_event(upcoming_id, watchlist_event):
  """
  Adds an Event object for the upcoming event.
  """
  try:
    upcoming_event_info = upcoming.event.getInfo(event_id=upcoming_id)[0]
  except:
    return "ERROR: There was an error retrieving event " + str(upcoming_id)

  try:
    # Look for an UpcomingEvent with this ID. If found, update the UpcomingEvent's related
    # event with any new title, description, or tags.
    print "Found event: " + upcoming_event_info['name']
    upcoming_event                = UpcomingEvent.objects.get(upcoming_event_id=upcoming_id)
    event                         = upcoming_event.event
    if event.get_first_eventtime():
      if not event.get_first_eventtime().is_in_past:
        event.title                   = force_unicode(upcoming_event_info['name'])
        event.description             = force_unicode(upcoming_event_info['description'])
        event.tags                    = force_unicode(" ".join(upcoming_event_info['tags'][0:15]))
        event.save()
        # If this is an event in the future, make sure it's related objects are all up to date.
        # If it's an event in the past, we don't really care.
        if event.get_first_eventtime():
          if not event.get_first_eventtime().is_in_past:
            _create_or_update_related_objects(event, upcoming_event_info, watchlist_event)

  except UpcomingEvent.DoesNotExist:
    # If we didn't find an UpcomingEvent with this ID, it's a new event. Create an event
    # with the appropriate info and call _create_or_update_upcomingevent_for_event to create
    # the associated UpcomingEvent.
    event = Event(
      title                     = force_unicode(upcoming_event_info['name']),
      description               = force_unicode(upcoming_event_info['description']),
      tags                      = force_unicode(" ".join(upcoming_event_info['tags'][0:15])),
      date_created              = datetime.datetime.now(),
      date_published            = datetime.datetime.now(),
      slug                      = get_unique_slug_value(Event, slugify(upcoming_event_info['name'])),
    )
    event.save()
    _create_or_update_related_objects(event, upcoming_event_info, watchlist_event)
Example #2
0
def _create_or_update_place_for_event(event, upcoming_event_info):
  """
  Adds a Place object and an UpcomingVenue object for the related venue.
  """
  try:
    upcoming_venue_info = upcoming.venue.getInfo(venue_id=upcoming_event_info['venue_id'])[0]
    upcoming_metro_info = upcoming.metro.getInfo(metro_id=upcoming_event_info['metro_id'])[0]
  except:
    print "\tERROR: There was an error retrieving the Upcoming venue or metro information for this event."
    return None
    
  upcoming_venue, created_upcoming_venue = UpcomingVenue.objects.get_or_create(
    upcoming_venue_id=upcoming_venue_info['id'],
  )
  if not created_upcoming_venue:
    place                 = upcoming_venue.place
    place.website         = upcoming_venue_info['url']
    place.description     = upcoming_venue_info['description']
    place.address1        = upcoming_venue_info['address']
    place.phone1          = upcoming_venue_info['phone']
  else:
    place = Place(
      name            = upcoming_venue_info['name'],
      slug            = get_unique_slug_value(Place, slugify(upcoming_venue_info['name']))[:30],
      website         = upcoming_venue_info['url'],
      description     = upcoming_venue_info['description'],
      address1        = upcoming_venue_info['address'],
      phone1          = upcoming_venue_info['phone'],
      phone2          = '',
      fax             = '',
    )
    if len(upcoming_venue_info['zip']) <= 5:
      place.zip_code = upcoming_venue_info['zip']

    # Get the city from flickr's API instead of using Upcoming's data, as Flickr's is MUCH more normalized.
    place.city = get_city_from_flickr(city_name=upcoming_venue_info['city'], state=upper(upcoming_metro_info['state_code']), country=upcoming_metro_info['country_code'])
    if place.city:
      print "\tFound city " + str(place.city) + " using Flickr's places API."
    else:
      # If flickr doesn't get us any data, we'll use Upcoming's (shitty) data.
      print "\t Couldn't find the city in Flickr's API; falling back to Upcoming's."
      city      = upcoming_venue_info['city'],
      state     = upper(upcoming_metro_info['state_code'])
      if len(upcoming_metro_info['country_code']) == 2:
        country = upcoming_metro_info['country_code']
      else:
        country = ''
      city, created_city = City.object.get_or_create(
        city      = city, 
        state     = state,
        country   = country,
        slug      = slugify(asciiDammit(city) + " " + asciiDammit(state) + " " + asciiDammit(country)),
      )
      if created_city:
        city.save()
    place.city = city
  
  place.save()

  upcoming_venue.place    = place
  upcoming_venue.user_id  = upcoming_venue_info['user_id']
  upcoming_venue.name     = upcoming_venue_info['name']
  upcoming_venue.url      = upcoming_venue_info['url']
  upcoming_venue.private  = upcoming_venue_info['private']
  upcoming_venue.save()

  return place
Example #3
0
def create_or_update_photo(photo, sizes, tags, exif):
  try:
    print "\nProcessing photo " + str(photo.title[0].elementText).encode('ascii', 'xmlcharrefreplace')
  except:
    print "\nProcessing photo (unable to print title...the photo's id is " + str(photo['id']) + ")"
  try:
    # See if we already have this photo in our database.
    savoy_photo = FlickrPhoto.objects.get(flickr_photo_id=photo['id']).photo
  except:
    # If we don't, create a placeholder Photo object for it.
    savoy_photo = Photo(slug=None)
  
  # Set the various attributes of the Photo object to the appropriate values.
  if photo.title[0].elementText != '':
    savoy_photo.title = force_unicode(photo.title[0].elementText)[:245]
  else:
    savoy_photo.title = 'Untitled'
  savoy_photo.description = force_unicode(photo.description[0].elementText)
  savoy_photo.date_created = photo.dates[0]['taken']
  savoy_photo.date_published = datetime.datetime.fromtimestamp(int(photo.dates[0]['posted']))
  
  if not savoy_photo.slug:
    proposed_slug = slugify(savoy_photo.title)
    savoy_photo.slug = get_unique_slug_value(Photo, proposed_slug)
  savoy_photo.tags = force_unicode(truncate_tags(tags))
  savoy_photo.aperture = ""
  
  for size in sizes:
    if size['label'] == "Original":
      savoy_photo.image_width = size['width']
      savoy_photo.image_height = size['height']
  if savoy_photo.image_width == None or savoy_photo.image_height == None:
    for size in sizes:
      if size['label'] == "Large":
        savoy_photo.image_width = size['width']
        savoy_photo.image_height = size['height']
  
  if exif:
    for tag in exif:
      
      if tag['label'] == 'Make':
        try:
          savoy_photo.make = tag.clean[0].elementText
        except:
          savoy_photo.make = tag.raw[0].elementText
          
      if tag['label'] == 'Model':
        try:
          savoy_photo.model = tag.clean[0].elementText
        except:
          savoy_photo.model = tag.raw[0].elementText
          
      if tag['label'] == 'Orientation':
        try:
          savoy_photo.orientation = tag.clean[0].elementText
        except:
          savoy_photo.orientation = tag.raw[0].elementText
          
      if tag['label'] == 'X-Resolution':
        try:
          savoy_photo.x_resolution_unit = tag.clean[0].elementText
        except:
          savoy_photo.x_resolution_unit = tag.raw[0].elementText
          
      if tag['label'] == 'Y-Resolution':
        try:
          savoy_photo.y_resolution = tag.clean[0].elementText
        except:
          savoy_photo.y_resolution = tag.raw[0].elementText
          
      if tag['label'] == 'Resolution Unit':
        try:
          savoy_photo.resolution_unit = tag.clean[0].elementText
        except:
          savoy_photo.resolution_unit = tag.raw[0].elementText
          
      if tag['label'] == 'Software':
        try:
          savoy_photo.software = tag.clean[0].elementText
        except:
          savoy_photo.software = tag.raw[0].elementText
          
      if tag['label'] == 'Host Computer':
        try:
          savoy_photo.host_computer = tag.clean[0].elementText
        except:
          savoy_photo.host_computer = tag.raw[0].elementText
          
      if tag['label'] == 'YCbCr Positioning':
        try:
          savoy_photo.ycbcr_positioning = tag.clean[0].elementText
        except:
          savoy_photo.ycbcr_positioning = tag.raw[0].elementText
          
      if tag['label'] == 'Exposure':
        try:
          savoy_photo.exposure = tag.clean[0].elementText
        except:
          savoy_photo.exposure = tag.raw[0].elementText
          
      if savoy_photo.aperture == "":
        if tag['label'] == 'Aperture':
          try:
            savoy_photo.aperture = tag.clean[0].elementText
          except:
            savoy_photo.aperture = tag.raw[0].elementText
            
      if tag['label'] == 'Shutter Speed':
        try:
          savoy_photo.shutter_speed = tag.clean[0].elementText
        except:
          savoy_photo.shutter_speed = tag.raw[0].elementText
          
      if tag['label'] == 'Exposure Bias':
        try:
          savoy_photo.exposure_bias = tag.clean[0].elementText
        except:
          savoy_photo.exposure_bias = tag.raw[0].elementText
          
      if tag['label'] == 'Metering Mode':
        try:
          savoy_photo.metering_mode = tag.clean[0].elementText
        except:
          savoy_photo.metering_mode = tag.raw[0].elementText
          
      if tag['label'] == 'Flash':
        try:
          savoy_photo.flash = tag.clean[0].elementText
        except:
          savoy_photo.flash = tag.raw[0].elementText
          
      if tag['label'] == 'Focal Length':
        try:
          savoy_photo.focal_length = tag.clean[0].elementText
        except:
          savoy_photo.focal_length = tag.raw[0].elementText
          
      if tag['label'] == 'Color Space':
        try:
          savoy_photo.color_space = tag.clean[0].elementText
        except:
          savoy_photo.color_space = tag.raw[0].elementText
          
      if tag['label'] == 'Sensing Method':
        try:
          savoy_photo.sensing_method = tag.clean[0].elementText
        except:
          savoy_photo.sensing_method = tag.raw[0].elementText
          
      if tag['label'] == 'Compression':
        try:
          savoy_photo.compression = tag.clean[0].elementText
        except:
          savoy_photo.compression = tag.raw[0].elementText

  # Save the Photo object.
  try:
    savoy_photo.save()
    print "\tSaved photo"
  except:
    print "\tERROR: Could not save photo"

  # Return the saved Photo object.
  return savoy_photo