cred=db.login.find_one({})

#Tweepy Login credensials are stored in mongodb databse
auth = tweepy.OAuthHandler(cred["consumerKey"], cred["consumerSecret"])
auth.set_access_token(cred["oauthTocken"], cred["oauthTokenSecret"])
api = tweepy.API(auth)
api

"""
Explanation: Note: My credentials Is stored in mongoDb database
End of explanation
"""


cities=["Delhi","Kolkata","Bangalore","Surat","India"]
loc = list(db.WOEID.find({"country" : "India",'name':{"$in":cities}},{'_id':0,'name':1,'woeid':1}))


pprint.pprint(loc[1])

"""
Explanation: <h2>Where On Earth ID (WOEID)</h2>
Extract WOEID for most common places & store for future reference, Becouse query is limited on twitter
below two line are commented becouse data already stored in db no need of it anymore<br />
<p><b>
Woeid = api.trends_available() #<br />
db.WOEID.insert_many(Woeid)
</b></p>

Selecting Cities in WOEID data
End of explanation
"""


Trend={}
for place in loc:
    Trend[place["name"]] = api.trends_place(place["woeid"])

"""
Explanation: Performing Latest Trend Query & Strore them
End of explanation
"""


for key in Trend:
    for trend in Trend[key][0]['trends']:
        del trend['query']
        del trend["url"]

"""
Explanation: Clean OUr Data drop unneccesary content
End of explanation
"""


for key in Trend:
    db.placesT.insert_one(Trend[key][0])
print "Insertion Complete"


#Delete All Unused Variables
del key,Trend

"""
Explanation: Insert Data Into database
End of explanation
"""


#extract Data from databse
trend=list(db.placesT.find({}))

#colName=trend[0]['locations'][0]['name']
trendName=[li["name"] for li in  trend[0]['trends']]
tweetVolume=[li["tweet_volume"] for li in trend[0]["trends"]]
tweetVolume

trend[0]['as_of']

"""
Explanation: AnaLysing Data
End of explanation
"""


