示例#1
0
def getTrailsAndPhotos(lon, lat, cnt, resort, trails, photos):
    """
    gets all trails nearby given resort
    associates all created trails with resortid
    and adds photo for trail into the photos for the resort
    """
    data = None
    try:
        if resort is None:
            data = fetch.fetchJSON(
                'https://www.hikingproject.com/data/get-trails?lat=' +
                str(lat) + '&lon=' + str(lon) + '&maxDistance=10&maxResults=' +
                str(cnt) +
                '&sort=distance&key=200217902-4d9f4e11973eb6aa502e868e55361062'
            )
        else:
            data = fetch.fetchJSON(
                'https://www.hikingproject.com/data/get-trails?lat=' +
                str(resort.lat) + '&lon=' + str(resort.lon) +
                '&maxDistance=10&maxResults=' + str(cnt) +
                '&sort=distance&key=200217902-4d9f4e11973eb6aa502e868e55361062'
            )
    except ValueError as e:
        return trails, photos
    for t in data['trails']:
        if t['id'] not in trails:
            trail = Trail(name=t['name'], id=t['id'])
            trail.difficulty = t['difficulty'] if 'difficulty' in t else None
            trail.summary = t['summary'] if 'summary' in t else None
            trail.stars = t['stars'] if 'stars' in t else None
            trail.starVotes = t['starVotes'] if 'starVotes' in t else None
            trail.lat = t['latitude'] if 'latitude' in t else None
            trail.lon = t['longitude'] if 'longitude' in t else None
            trail.length = t['length'] if 'length' in t else None
            trail.ascent = t['ascent'] if 'ascent' in t else None
            trail.descent = t['descent'] if 'descent' in t else None
            try:
                youtubedata = fetch.fetchJSON(
                    'https://www.googleapis.com/youtube/v3/search?q=' +
                    trail.name + ' trail' +
                    '&part=snippet&type=video&maxResults=25&key=AIzaSyDRwflQaI1Zq5bqKVQJ2YBDHb7l7oD1L2o'
                )
                trail.youtubeid = youtubedata['items'][0]['id']['videoId']
            except ValueError:
                trail.youtubeid = None
            except IndexError:
                trail.youtubeid = None
            trails[t['id']] = trail
            if 'imgMedium' in t and t['imgMedium'] != "":
                photo = Photo(id=trail.id,
                              name=trail.name + " photo",
                              lat=trail.lat,
                              lon=trail.lon)
                photo.url = t['imgMedium']
                photos[t['imgMedium']] = photo
                photo.trail = trails[t['id']]
                photo.content = getPhotoContents(photo.url)
        resort.trails.append(trails[t['id']])
        resort.photos += trails[t['id']].photos
    return trails, photos
示例#2
0
def createtrail(t, ytdata):
    trail = Trail(name=t['name'], id=t['id'])
    trail.difficulty = t['difficulty']
    trail.summary = t['summary']
    trail.stars = t['stars']
    trail.starVotes = t['starVotes']
    trail.lat = t['latitude']
    trail.lon = t['longitude']
    trail.length = t['length']
    trail.ascent = t['ascent']
    trail.descent = t['descent']
    trail.img = t['imgMedium']
    trail.youtubeid = ytdata['items'][0]['id']['videoId']
    return trail