-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTagSearch.py
167 lines (138 loc) · 5.35 KB
/
TagSearch.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# File name: TagSearch.py
# Authors: Anita & Gabriel Radinsky
# Created: 4/2015
# Description: This file will vizulaize the locations of two Instagram trending tags
# the user wishes to search for. Two tags will be ploted on
# a geographical map in different colors to represent where each
# tag was posted. The tag data is being pulled from instagram using
# Instagram API's endpoints. The latitude and longitude used will be
# extracted from a JSON file profuced by the API endpoint.
#
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
import TagSearch
import time
from instagram.client import InstagramAPI
import httplib2
import json
import urllib
from urllib import urlopen
from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt
import numpy as np
import wx
# Initialize wx App
app = wx.App()
app.MainLoop()
def gatherLatitude(tag):
'''
Gathers Latitude coordianates from the JSON file produced
by the Instagram's API endpoints. The 'tag' being passed in will
narrow the search to return all latitudes coordinates gathered from
the JSON file.
'''
file = urlopen("https://api.instagram.com/v1/tags/"+ tag+"/media/recent?access_token=231920771.7f67456.bfe24e8a256d4d5ca2e2131d56b7103b").read()
data = json.loads(file)
latitude = []
for data in data["data"]:
if data['location'] != None:
latitudePoint = data['location']['latitude']
latitude.append(latitudePoint)
return latitude
def gatherLongitude(tag):
'''
Gathers longitude coordianates from the JSON file produced
by the Instagram's API endpoints. The 'tag' being passed in will
narrow the search to return all longitude coordinates gathered from
the JSON file.
'''
file = urlopen("https://api.instagram.com/v1/tags/"+ tag+"/media/recent?access_token=231920771.7f67456.bfe24e8a256d4d5ca2e2131d56b7103b").read()
data = json.loads(file)
longitude =[]
for data in data["data"]:
if data['location'] != None:
longitudePoint = data['location']['longitude']
longitude.append(longitudePoint)
return longitude
def get_marker_color(tagNumber):
'''
Returns green for the first tag and red for the second tag.
'''
if tagNumber == 1:
return ('go')
else:
return ('ro')
def plotMap(longitude1, latitude1, longitude2, latitude2, tag1, tag2):
'''
Plots tag points on their appropriate latitude and longitude
coordinates on the world map.
'''
map = Basemap(projection='robin', resolution = 'l', area_thresh = 1000.0,
lat_0=0, lon_0=-130)
map.drawcoastlines()
map.drawcountries()
map.bluemarble()
map.drawmapboundary()
map.drawmeridians(np.arange(0, 360, 30))
map.drawparallels(np.arange(-90, 90, 30))
min_marker_size = 10
#This will plot the latitudes and longitues for Tag1
for lon, lat in zip(longitude1, latitude1):
x,y = map(lon, lat)
msize = min_marker_size
marker_string = get_marker_color(1)
map.plot(x, y, marker_string, markersize=msize)
#This will plot latitudes and longitues for Tag2
for lon, lat in zip(longitude2, latitude2):
x,y = map(lon, lat)
msize = min_marker_size
marker_string = get_marker_color(2)
map.plot(x, y, marker_string, markersize=msize)
title_string = "#" + tag1 +"(green) and #" + tag2 + "(red) currently trending\n"
plt.title(title_string)
plt.show()
def showTagsonMap():
'''
Asks user for input, gathers data, and plots points.
'''
#Asks user for input
firstTag= userInput()
secondTag= userInput()
#Stores data for first tag
latitude_1= []
longitude_1= []
#Stores data for second tag
latitude_2= []
longitude_2= []
#Appends latitude into "latitude_1"
for x in range(1): # Can change 1 to larger number to gather more data.
points = gatherLatitude(firstTag)
for data in points:
latitude_1.append(data)
for x in range(1): # Can change 1 to larger number to gather more data.
points = gatherLongitude(firstTag)
for data in points:
longitude_1.append(data)
for x in range(1): # Can change 1 to larger number to gather more data.
points = gatherLatitude(secondTag)
for data in points:
latitude_2.append(data)
for x in range(1): # Can change 1 to larger number to gather more data.
points = gatherLongitude(secondTag)
for data in points:
longitude_2.append(data)
if len(latitude_1) == 0:
print "No data trending for first tag"
if len(latitude_2) == 0:
print "No data trending for second tag"
plotMap(longitude_1, latitude_1, longitude_2, latitude_2, firstTag, secondTag)
def userInput():
'''
Takes user input via a dialog pop up and returns whatever the user input.
'''
dlg = wx.TextEntryDialog(None,'Which tag would you like to enter?','InstaDataViz', 'Enter text here')
# This function returns the button pressed to close the dialog
dlg.ShowModal()
# This function will destory the dialog.
dlg.Destroy()
return dlg.GetValue()