def fbevent_to_event(fbevent, allow_place_import=True, save=False): e = Event() e.name = fbevent.get_field('name', '').strip() e.description = fbevent.get_field('description', '').strip() e.dtstart = fbevent.get_dtstart() e.dtend = fbevent.get_dtend() e.url = fbevent.get_field('website', '').strip() # fairly sure this isn't part of a Graph API Event place_fbpage_id = fbevent.get_venue_id() if place_fbpage_id: linked_places = Place.objects.filter(fb_id=place_fbpage_id) if len(linked_places) > 0: if len(linked_places) > 1: pass # TODO: need an admin warning generated about data cleanup e.place = linked_places[0] if not e.place: new_place = fbevent.get_place(allow_import=allow_place_import) if new_place: new_place.listed = False # don't automatically list any place we're creating dynamically here if save: # see if inner Location needs saved if new_place.location_id is None and new_place.location is not None: new_place.location.save() # this statment is necessary for Django's stupid inner model handling new_place.location = new_place.location new_place.save() e.place = new_place else: # worst case, we get a location string to use as a primitive e.place_primitive = fbevent.get_field('location', '') try: im_url = fbevent.get_picture_url(size='large') e.image = imagefile_from_url(im_url) except IOError: # TODO: log network error pass if save: e.save() return e
def fbpage_to_place(fbpage, save=False): if not fbpage.valid: return None p = Place() # special parking/hours objects used to serialize to DB # TODO: this is temporary. dig into django custom model field to make less hacky p.hours = fbpage.get_hours() parking = fbpage.get_parking() if parking: p.set_parking(parking) location = fbpage.get_location() if location is not None and save: location.save() p.location = location p.name = fbpage.get_field('name', '').strip()[:200] p.fb_id = fbpage.get_field('id', '').strip()[:50] p.description = fbpage.get_field('description', '').strip() # if no description, try 'about' if not p.description: p.description = fbpage.get_field('about', '').strip() p.phone = fbpage.get_field('phone', '').strip()[:200] p.url = fbpage.get_field('website', '').strip() if p.url: p.url = p.url.split()[0][:200] try: im_url = fbpage.get_picture_url(size='large') p.image = imagefile_from_url(im_url) except IOError: # TODO: log network/image format error pass if save: p.save() return p
def insert_row(row, idx): logging.info(u"Importing row %d (%s)" % (int(idx), row["name"])) try: lat = decimal.Decimal(row.get("lat")) except (TypeError, decimal.InvalidOperation): lat = None try: lng = decimal.Decimal(row.get("lng")) except (TypeError, decimal.InvalidOperation): lng = None address = row.get("street", "") location = Location(address=address, postcode="15213", latitude=lat, longitude=lng) # want to resolve location if we have an address worht normalizing and/or we don't have geocoded values if location.address or location.latitude is None or location.longitude is None: logging.info('Resolving location "%s"' % unicode(location)) resolved = resolve_location(location) if resolved: # hack to get around Google Geocoding appending unviersity names onto many addresses if "university" in resolved.address.lower() and "university" not in row.get("street", "").lower(): resolved.address = ",".join(resolved.address.split(",")[1:]) resolved.address = resolved.address.strip() try: # if exact match exists, use it instead of the newly found one location = Location.objects.get(address=resolved.address, postcode=location.postcode) except Location.DoesNotExist: location = resolved location.save() else: logging.warning("Geocoding failed") has_corporate_fb = row.get("fb_id", "").startswith("corporate") place, created = Place.objects.get_or_create( name=row["name"], location=location, url=row.get("url", "")[:200], phone=row.get("phone", "")[:200], fb_id=row.get("fb_id", "").split(":")[-1], description=row.get("description", ""), listed=bool(int(row.get("listed", "0"))), ) for t in row.get("tags", "").split(";"): tag, _ = Tag.objects.get_or_create(name=slugify(t.lower())) place.tags.add(tag) place.save() image_path = row.get("image_url", "") if re.match("https?\:", image_path): if re.match("https?\:\/\/profile.ak.fbcdn.net", image_path): logging.info("Skipping Facebook cdn image") else: logging.info("Pulling live image from web") try: place.image = utils.imagefile_from_url(image_path) except IOError: logging.error("Failed pulling image") pass elif image_path != "": logging.info("Using local image file") f = open(os.path.join(DATA_DIR, "images", image_path)) place.image = File(f) place.save() if place.image: place.image.close() if place.fb_id: logging.info("Supplementing info from Facebook...") try: places_fb_import.commit_place(place, corporate=has_corporate_fb) except places_fb_import.FacebookAPIError as e: logging.error("Facebook API error (fb id %s): %s" % (str(place.fb_id), unicode(e))) except IOError as e: logging.error(unicode(e)) logging.info("Finished import: %s" % unicode(place)) logging.info("")