Exemplo n.º 1
0
def process_mention(twm):
    m = Mention()
    m.tw_id = twm.id
    m.tw_text = twm.text
    m.tw_user_friendly_name = twm.user.name
    m.tw_username = twm.user.screen_name
    m.tw_profile_img_url = twm.user.profile_image_url
    m.tw_user_url = twm.user.url
    if twm.place is not None:
        m.tw_place_full_name = twm.place.full_name
        m.tw_place_type = twm.place.place_type
        # check this
        if twm.place.bounding_box:
            il = Location()
            lon = twm.place.bounding_box.coordinates[0][0][0]
            lat = twm.place.bounding_box.coordinates[0][0][1]
            wkt = "POINT({0} {1})".format(lon, lat)
            il.loc = WKTSpatialElement(wkt)
            m.tw_computed_location = il
            # todo average all points in bounding box? compute centroid?

    # Handle images
    if u"media" in twm.entities:
        for img in twm.entities[u"media"]:
            # this really should be in the service somewhere since it will have to physically save an image to the FS, too.
            i = Image()
            i.filename = "{0}.jpg".format(twm.id)
            # location
            i.address_text = m.tw_place_full_name  #'{0} {1}'.format(m.tw_place_full_name, twm.country)
            i.title = i.address_text
            i.description = m.tw_text
            base_img_url = img[u"media_url"]
            img_url = "{0}:large".format(base_img_url)
            img_response = requests.get(img_url)
            p_img = PIL_Image.open(StringIO(img_response.content))
            cur_path = os.getcwd()
            outfile = os.path.join(cur_path, "CatCat", "static", "image_media", i.filename)
            try:
                p_img.save(outfile)
            except IOError as e:
                print("cannot convert", i.title)
            i.mention_id = m.tw_id
            # associate a location with this image if we have one.
            i.location = m.tw_computed_location

            db.session.add(i)

    db.session.add(m)

    try:
        db.session.commit()
    except IntegrityError:
        db.session.rollback()
Exemplo n.º 2
0
def upload():
    """Page to add a new catcat sighting."""
    form = forms.NewSightingForm()
    if form.validate_on_submit():
        file = request.files['imageFile']
        if file and uploadhelper.allowed_file(file.filename):
            filename = uploadhelper.save_with_rename(file)

            #Create the DB model objects
            i = Image()
            i.creator = current_user
            i.address_text = form.address.data
            i.title = form.title.data
            i.description = form.description.data
            i.filename = filename
            l = Location()
            #Safety first. They should already but Decimals, but double-check.
            lng = Decimal(form.loc_lng.data)
            lat = Decimal(form.loc_lat.data)
            #POINT(lng lat)
            loc_wkt = "POINT({0} {1})".format(lng, lat)
            l.loc = WKTElement(loc_wkt, srid=4326) #4326 is "normal" lag/lng
            i.location = l
            db.session.add(i)
            db.session.commit()

            new_id = i.id
            return redirect(url_for('main.image', id=new_id))
        else:
            flash("File type not allowed.")
    #otherwise...
    return render_template(
        'main/upload.html',
        title='Add A Cat',
        form=form
    )