Created
June 14, 2015 12:44
-
-
Save scorpionhiccup/f10d700216aa1ce3facb to your computer and use it in GitHub Desktop.
Get Youtube Videos describing Landmarks around it based on Cities Location
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
API_KEY= | |
APPLICATION_ID= | |
REST_API_KEY= | |
MASTER_KEY= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# coding=utf-8 | |
import urllib, urllib2, json, requests, httplib | |
from bs4 import BeautifulSoup | |
from parse_rest.connection import ParseBatcher | |
from parse_rest.datatypes import Object, GeoPoint | |
from parse_rest.connection import register | |
class Video(Object): | |
pass | |
video_ids = [] | |
landmarks = [] | |
keys={ | |
'API_KEY':'', | |
'APPLICATION_ID':'', | |
'REST_API_KEY':'', | |
'MASTER_KEY':'' | |
} | |
#def parse(lat, lng): | |
def parse(landmark): | |
name,lat,lng = landmark['name'], landmark['geometry']['location']['lat'], landmark['geometry']['location']['lng'] | |
url_query = 'https://www.googleapis.com/youtube/v3/search?part=snippet&location=' + str(lat) +'%2C+' + str(lng) + '&locationRadius=50km&maxResults=50&order=date&q="GoPro"&safeSearch=moderate&type=video&videoDimension=2d&key='+ keys['API_KEY'] | |
response = urllib2.urlopen(url_query) | |
obj2 = json.loads(response.read()) | |
register(keys['APPLICATION_ID'], keys['REST_API_KEY'], master_key=keys['MASTER_KEY']) | |
for obj in obj2['items']: | |
if obj['id']['videoId'] not in video_ids: | |
video_ids.append(obj['id']['videoId']) | |
vidobj = Video(title=obj['snippet']['title'], | |
videoId=obj['id']['videoId'], | |
description=obj['snippet']['description']) | |
vidobj.location = GeoPoint(latitude=lat, longitude=lng) | |
vidobj.save() | |
break | |
def landmarks(city, country): | |
url = 'https://maps.googleapis.com/maps/api/geocode/json' | |
params = {'address': city} | |
#params = {'address': city + ','+ country} | |
resp = requests.get(url=url, params=params, verify=False) | |
url_data = json.loads(resp.text) | |
try: | |
temp = url_data['results'][0]['geometry']['location'] | |
lat, lng = str(temp['lat']), str(temp['lng']) | |
except Exception, e: | |
lat, lng = '', '' | |
url = 'https://maps.googleapis.com/maps/api/place/nearbysearch/json?location='+lat+','+lng+'&key='+ keys['API_KEY']+'&radius=500000' | |
response = urllib2.urlopen(url) | |
obj2 = json.loads(response.read())['results'] | |
try: | |
for landmark in obj2: | |
parse(landmark) | |
except Exception,e: | |
print city | |
if __name__ == "__main__": | |
with open("keys.txt") as myfile: | |
for line in myfile: | |
name, var = line.partition("=")[::2] | |
keys[str(name.strip())] = str(var.strip()) | |
cities = [] | |
done = [] | |
cities = list(set(cities)-set(done)) | |
for city in cities: | |
landmarks(city, '') |
Author
scorpionhiccup
commented
Jun 14, 2015
- _API_KEY_ Google API key to search based on city's location.
- _APPLICATION_ID, _REST_API_KEY, _MASTER_KEY_ are the Keys of the parse.com Project Keys.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment