예제 #1
0
def add_point(adventure_slug, point_type):
    try:
        point = Point(
            title=request.values.get("title", None),
            latitude=sfloat(request.values.get("latitude", None)),
            longitude=sfloat(request.values.get("longitude", None)),
            desc=request.values.get("desc", None),
            resource=request.values.get("resource", None),
            timestamp=datetime.strptime(
                request.values.get("timestamp", datetime.now().strftime("%Y-%m-%dT%H:%M:%S.000Z")),
                "%Y-%m-%dT%H:%M:%S.%fZ",
            ),
            thumb=request.values.get("thumb", None),
            photo=request.values.get("photo", None),
            video=request.values.get("video", None),
            hide=sbool(request.values.get("hide", None)),
            delorme_id=sint(request.values.get("delorme_id", None)),
            type=point_type,
            adventure=adventure_slug,
        )
        point.save()
        return jsonify(point.to_dict())
    except ValueError as e:
        logging.error(e)
        abort(400)
    except BadRequest as e:
        logging.error(e)
        abort(400)
    except Exception as e:
        logging.error(e)
        abort(500)
    return
예제 #2
0
def load_route():
    try:
        data = json.loads(request.data)
        url = data['url']
    except Exception as e:
        logging.error(0)
        abort(400)

    obj = urllib2.urlopen(url)
    str = obj.read()
    kml_str = ""
    for line in iter(str.splitlines()):
        if not 'atom:link' in line:
            kml_str += line
            kml_str += '\n'

    Point.objects(type='route').delete()

    root = parser.fromstring(kml_str)

    pointid = 1000
    for placemark in root.Document.Folder.Placemark:
        coordinates = placemark.MultiGeometry.Point.coordinates.text.split(',')
        try:
            point = Point(
                title=placemark.name.text,
                type='route',
                latitude=float(coordinates[1]),
                longitude=float(coordinates[0]),
                pointid=pointid,
                timestamp=datetime.now()
            )
        except TypeError:
            abort(500)
        except Exception as e:
            logging.error(0)
            abort(500)
        try:
            point.save()
        except TypeError:
            abort(400)
        except BadRequest as e:
            logging.error(e)
            abort(400)
        except Exception as e:
            logging.error(e)
            abort(500)

        pointid += 1

    return Response(json.dumps({'status': 'ok'}), status=200, mimetype='application/json')
예제 #3
0
def import_media(access_token, client_secret):

    global api
    api = InstagramAPI(
        access_token=access_token.encode("ascii", "ignore"), client_secret=client_secret.encode("ascii", "ignore")
    )

    m = hashlib.md5()

    media = get_media()

    for item in media:

        pointid = int(m.hexdigest()[:8], 16)

        point = Point.objects(pointid == pointid).first()

        if point is None:

            m.update(item.id)

            title = None
            desc = None
            video = None
            if hasattr(item, "caption"):
                if item.caption is not None:
                    desc = item.caption.text
            if hasattr(item, "location"):
                if item.location.name is not None:
                    title = item.location.name
                if item.location.point is not None:
                    latitude = item.location.point.latitude
                    longitude = item.location.point.longitude
            timestamp = item.created_time
            thumb = item.images.get("thumbnail").url
            photo = item.images.get("standard_resolution").url
            if hasattr(item, "videos"):
                video = item.videos.get("standard_resolution").url
            resource = item.link

            try:
                point = Point(
                    title=title,
                    latitude=latitude,
                    longitude=longitude,
                    type="photo",
                    timestamp=timestamp,
                    pointid=pointid,
                    thumb=thumb,
                    photo=photo,
                    video=video,
                    resource=resource,
                    desc=desc,
                )
                point.save()
            except AttributeError:
                pass
            except Exception as e:
                logging.error(e.args[0])

    return Response(json.dumps({"status": "ok"}), status=200, mimetype="application/json")
예제 #4
0
def load_data(url):
    obj = urllib2.urlopen(url)
    root = parser.parse(obj).getroot()
    for placemark in root.Document.Folder.Placemark:
        try:
            point = None
            extended_data = placemark.ExtendedData.Data
            pointid = None
            event = None
            elevation = None
            velocity = None
            course = None
            text = None
            type='tracker'
            for data in extended_data:
                if data.attrib['name'] == 'Id':
                    pointid = int(data.value.text)
                elif data.attrib['name'] == 'Event':
                    event = data.value.text.encode('utf-8')
                elif data.attrib['name'] == 'Elevation':
                    elevation = data.value.text.encode('utf-8')
                elif data.attrib['name'] == 'Velocity':
                    velocity = data.value.text.encode('utf-8')
                elif data.attrib['name'] == 'Course':
                    course = data.value.text.encode('utf-8')
                elif data.attrib['name'] == 'Text':
                    text = data.value.text
                    if text is not None:
                        text = text.encode('utf-8')
            if pointid is not None:
                point = Point.objects(pointid == pointid).first()
            if point is None:
                title = event
                coordinates = placemark.Point.coordinates.text.split(',')
                latitude = float(coordinates[1])
                longitude = float(coordinates[0])
                timestamp = datetime.strptime(placemark.TimeStamp.when.text, "%Y-%m-%dT%H:%M:%SZ")

                if text is not None:
                    desc = text
                    type = 'message'
                else:
                    desc = ''
                    if elevation is not None:
                        desc = desc + "Elevation: {elevation}<br>".format(elevation=elevation)
                    if velocity is not None:
                        desc = desc + "Velocity: {velocity}<br>".format(velocity=velocity)
                    if course is not None:
                        desc = desc + "Course: {course}<br>".format(course=course)

                point = Point(
                    title=title,
                    latitude=latitude,
                    longitude=longitude,
                    type=type,
                    timestamp=timestamp,
                    pointid=pointid,
                    desc=desc
                )
                point.save()
        except AttributeError:
            pass
        except Exception as e:
            logging.error(e.args[0])

    return Response(json.dumps({'status': 'ok'}), status=200, mimetype='application/json')