from tweepy import Stream
from tweepy import OAuthHandler
from tweepy.streaming import StreamListener
import time
import tweepy
from geoloc import coordinates


ckey='your consumer key'
csecret='your consumer secret'
atoken='your access token'
asecret='your access secret'

address=raw_input("Enter the location whose top trend you want to know.\n")

coo=coordinates(address)
lati=coo[0]
longi=coo[1]

auth = tweepy.OAuthHandler(ckey,csecret)
auth.set_access_token(atoken,asecret)
api = tweepy.API(auth)

trendslist=[]

loc=api.trends_closest(lat=lati,long=longi)      #Returns the locations closest to the specified location
place=loc[0]['name']
woeid=loc[0]['woeid']                            #extracting the woeid of the first location

trends=api.trends_place(id=woeid)                #Returns the top 10 trending topics
    req_params={"client_id":client_id, "client_secret":client_secret, "redirect_uri":redirect_uri, "grant_type":grant_type, "code":code}
    
    request_url="https://api.instagram.com/oauth/access_token"
    r=requests.post(request_url, data=req_params)                      #making a POST request with the client details as parameters

    data=json.loads(r.text)
    access_token=str(data["access_token"])
    return access_token

def media_search(lat,lng,access_token):
    url="https://api.instagram.com/v1/media/search?lat="+lat+"&lng="+lng+"&distance=5000"+"&access_token="+access_token
    print url
    r=requests.get(url)
    resp=json.loads(r.text)
    for i in range(10):                                                         #just getting 10 images. can be changed
        pic_url=resp['data'][i]['images']['standard_resolution']['url']         #getting the image url
        p=requests.get(pic_url)
        f_name=str(location)+ ' pic '+str(i)+'.jpg'
        with open(f_name,'wb') as f:
            f.write(p.content)
            f.close()
        print "got one"

location=raw_input("Enter location.\n")
c=coordinates(location)
lat=str(c[0])
lng=str(c[1])

access_token=get_access_token()
media_search(lat, lng, access_token)
    )  #making a POST request with the client details as parameters

    data = json.loads(r.text)
    access_token = str(data["access_token"])
    return access_token


def media_search(lat, lng, access_token):
    url = "https://api.instagram.com/v1/media/search?lat=" + lat + "&lng=" + lng + "&distance=5000" + "&access_token=" + access_token
    print url
    r = requests.get(url)
    resp = json.loads(r.text)
    for i in range(10):  #just getting 10 images. can be changed
        pic_url = resp['data'][i]['images']['standard_resolution'][
            'url']  #getting the image url
        p = requests.get(pic_url)
        f_name = str(location) + ' pic ' + str(i) + '.jpg'
        with open(f_name, 'wb') as f:
            f.write(p.content)
            f.close()
        print "got one"


location = raw_input("Enter location.\n")
c = coordinates(location)
lat = str(c[0])
lng = str(c[1])

access_token = get_access_token()
media_search(lat, lng, access_token)