1 00:00:00,720 --> 00:00:04,020 - For our last example, we're going to introduce 2 00:00:04,020 --> 00:00:07,320 a combination of geocoding and mapping 3 00:00:07,320 --> 00:00:10,720 and use that information to help us take 4 00:00:10,720 --> 00:00:14,470 a live tweet stream and plot the resulting 5 00:00:14,470 --> 00:00:18,000 tweets that we get on an interactive map. 6 00:00:18,000 --> 00:00:20,850 And as you'll see, the tweets will be 7 00:00:20,850 --> 00:00:24,435 plotted as map markers which the user 8 00:00:24,435 --> 00:00:28,820 of the map can click on to pop open the map marker 9 00:00:28,820 --> 00:00:32,560 and to look at the sender of the tweet 10 00:00:32,560 --> 00:00:35,210 and the tweet's text as well. 11 00:00:35,210 --> 00:00:37,540 So, we'll once again be collecting 12 00:00:37,540 --> 00:00:40,080 streaming tweets using the same techniques 13 00:00:40,080 --> 00:00:43,190 that we used in the last two examples. 14 00:00:43,190 --> 00:00:47,730 Now, as far as location information in tweets is concerned 15 00:00:47,730 --> 00:00:51,520 by default, for privacy reasons, twitter disables 16 00:00:51,520 --> 00:00:54,640 precise location information that would give 17 00:00:54,640 --> 00:00:56,650 latitude and longitude coordinates 18 00:00:56,650 --> 00:00:59,140 from which a tweet was sent. 19 00:00:59,140 --> 00:01:03,440 If you want, as a user of twitter, you can opt in 20 00:01:03,440 --> 00:01:06,280 to allowing twitter to track your locations 21 00:01:06,280 --> 00:01:08,910 and some people do choose to do that. 22 00:01:08,910 --> 00:01:11,950 However, even though most tweets don't 23 00:01:11,950 --> 00:01:14,850 contain precise location information 24 00:01:14,850 --> 00:01:19,190 they do typically include the user's home location 25 00:01:19,190 --> 00:01:22,710 however, that may be a fake location 26 00:01:22,710 --> 00:01:26,080 or a fictitious location, it may be invalid 27 00:01:26,080 --> 00:01:27,840 depending on what they typed in 28 00:01:27,840 --> 00:01:30,690 when they set up their user account. 29 00:01:30,690 --> 00:01:34,870 So, what we're going to do is grab those locations 30 00:01:34,870 --> 00:01:37,210 the user's home locations, and we're going 31 00:01:37,210 --> 00:01:40,400 to take those and translate them into 32 00:01:40,400 --> 00:01:44,160 actual latitudes and longitudes if we can 33 00:01:44,160 --> 00:01:45,950 for any valid locations 34 00:01:45,950 --> 00:01:50,190 and then we'll use those to plot our map markers. 35 00:01:50,190 --> 00:01:52,480 Now to do part of this work, we're going to be 36 00:01:52,480 --> 00:01:55,410 taking advantage of the geopy library 37 00:01:55,410 --> 00:01:59,520 which we had you install in early on in this lesson 38 00:01:59,520 --> 00:02:02,820 at the same time that you were installing Tweepy 39 00:02:02,820 --> 00:02:06,930 and in the geopy library, it provides geocoding 40 00:02:06,930 --> 00:02:08,820 capabilities which is the ability to 41 00:02:08,820 --> 00:02:11,780 take a location and get the corresponding 42 00:02:11,780 --> 00:02:14,840 latitude and longitude in the world. 43 00:02:14,840 --> 00:02:18,430 Geopy has support for dozens of different 44 00:02:18,430 --> 00:02:22,240 geocoding services, many of those geocoding services 45 00:02:22,240 --> 00:02:25,890 have either free tiers or lite tiers 46 00:02:25,890 --> 00:02:28,050 with rate limits on them 47 00:02:28,050 --> 00:02:31,200 and for our purposes, we chose to use 48 00:02:31,200 --> 00:02:36,200 the OpenMapQuest geocoding service via the geopy library. 49 00:02:37,790 --> 00:02:40,040 Now, as you'll see, we're going to be able to 50 00:02:40,040 --> 00:02:42,020 take a location such as Boston, MA 51 00:02:43,070 --> 00:02:45,360 turn it into a latitude and longitude 52 00:02:45,360 --> 00:02:48,040 which are numeric values that we will use 53 00:02:48,040 --> 00:02:50,080 for plotting on the maps. 54 00:02:50,080 --> 00:02:53,340 The OpenMapQuest geocoding API gives you the ability 55 00:02:53,340 --> 00:02:56,970 to make up to 15 thousand transactions per month 56 00:02:56,970 --> 00:03:00,040 on their free tier, and you can sign up 57 00:03:00,040 --> 00:03:03,470 for OpenMapQuest at this location. 58 00:03:03,470 --> 00:03:06,260 So, once you go to that website 59 00:03:06,260 --> 00:03:09,230 in the upper right corner of the page at the moment, anyway 60 00:03:09,230 --> 00:03:11,760 is where you'll see the sign up link 61 00:03:11,760 --> 00:03:14,700 so you'll wanna go ahead and sign up 62 00:03:14,700 --> 00:03:17,190 and you'll have to fill out your information 63 00:03:17,190 --> 00:03:19,960 and once you're signed up, it will log you in 64 00:03:19,960 --> 00:03:23,500 and it will take you to this web page 65 00:03:23,500 --> 00:03:25,260 and If it doesn't you can enter 66 00:03:25,260 --> 00:03:29,470 this web page on your own and on that web page 67 00:03:29,470 --> 00:03:34,470 you'll be able to create new keys for accessing this API. 68 00:03:34,990 --> 00:03:38,460 So I'm gonna go ahead and pull up my logged in account 69 00:03:38,460 --> 00:03:40,278 and you can see I've already created 70 00:03:40,278 --> 00:03:43,620 a DeitelTest app, a similar concept to 71 00:03:43,620 --> 00:03:45,820 what we did with creating a Twitter app 72 00:03:45,820 --> 00:03:47,890 I won't click on this because then it will 73 00:03:47,890 --> 00:03:50,750 reveal my private key, but what you're going to 74 00:03:50,750 --> 00:03:53,490 do here is create a new key 75 00:03:53,490 --> 00:03:56,120 and so I'll pull up that page briefly 76 00:03:56,120 --> 00:03:59,010 all you need to specify is the app name 77 00:03:59,010 --> 00:04:02,010 we don't use this concept of a callback URL 78 00:04:02,010 --> 00:04:03,350 in any of our apps. 79 00:04:03,350 --> 00:04:05,120 So, you'll specify the name and click 80 00:04:05,120 --> 00:04:07,940 create app, once you do that, you'll be taken 81 00:04:07,940 --> 00:04:09,810 to a page where you can manage 82 00:04:09,810 --> 00:04:12,410 the app's keys and in our case 83 00:04:12,410 --> 00:04:15,170 I could click on this to show you what that looks like 84 00:04:15,170 --> 00:04:18,380 but, basically, what you're going to need to do 85 00:04:18,380 --> 00:04:22,270 is grab the consumer key value 86 00:04:22,270 --> 00:04:25,800 and you're going to replace in our keys.py file 87 00:04:25,800 --> 00:04:30,800 the YourKeyHere value for the mapquest_key variable. 88 00:04:31,400 --> 00:04:34,650 So you'll wanna go ahead and open keys.py 89 00:04:34,650 --> 00:04:37,150 like you did earlier today or earlier 90 00:04:37,150 --> 00:04:39,010 whenever you were watching the videos 91 00:04:39,010 --> 00:04:43,440 and you'll replace your key in the keys.py file 92 00:04:43,440 --> 00:04:47,230 or you'll put your key in the keys.py file 93 00:04:47,230 --> 00:04:50,840 make sure you save the file before you import it 94 00:04:50,840 --> 00:04:55,840 into the IPython session or into any code that requires it. 95 00:04:57,410 --> 00:05:00,410 Now, in addition, we're going to be using another 96 00:05:00,410 --> 00:05:02,780 library which you have not installed yet. 97 00:05:02,780 --> 00:05:05,580 It's the Folium library, let me pull up its 98 00:05:05,580 --> 00:05:08,120 GitHub page here, so on this page 99 00:05:08,120 --> 00:05:11,120 you'll find the GitHub information 100 00:05:11,120 --> 00:05:14,010 and installation instructions 101 00:05:14,010 --> 00:05:17,160 but basically the folium library is 102 00:05:17,160 --> 00:05:21,727 a python library that takes advantage of Leaflet.js 103 00:05:23,510 --> 00:05:25,540 which is a JavaScript mapping library 104 00:05:25,540 --> 00:05:28,650 for creating maps in the context of webpages 105 00:05:29,670 --> 00:05:33,050 so we'll be using this to create our maps. 106 00:05:33,050 --> 00:05:36,020 As you'll see we will output an HTML file 107 00:05:36,020 --> 00:05:38,800 that actually contains the mapping code 108 00:05:38,800 --> 00:05:42,010 and when you open that HTML file in your web browser 109 00:05:42,010 --> 00:05:44,230 you'll be presented with the interactive 110 00:05:44,230 --> 00:05:46,470 map that we're going to create. 111 00:05:46,470 --> 00:05:49,400 So, before you execute any of the code in this example 112 00:05:49,400 --> 00:05:52,020 you will want to make sure that you install 113 00:05:52,020 --> 00:05:55,210 the folium library and it will also install 114 00:05:55,210 --> 00:06:00,170 as part of that, the support for Leaflet.js as well. 115 00:06:00,170 --> 00:06:04,970 Now, Leaflet.js uses open source maps by default 116 00:06:04,970 --> 00:06:09,210 specifically, it uses maps from the OpenStreetMap.org 117 00:06:09,210 --> 00:06:12,500 website, and they do require us to display 118 00:06:12,500 --> 00:06:16,170 the copyright notice that you see here on the screen 119 00:06:16,170 --> 00:06:20,350 so all of the maps are copyright to the contributors 120 00:06:20,350 --> 00:06:24,303 that created those maps so, we're just making it- 121 00:06:25,670 --> 00:06:27,060 making you - excuse me 122 00:06:27,060 --> 00:06:30,710 aware of the fact that these maps are under copyright 123 00:06:30,710 --> 00:06:33,010 so please be careful when using 124 00:06:33,010 --> 00:06:35,950 them in your own applications. 125 00:06:35,950 --> 00:06:38,380 So with that said, the next thing that 126 00:06:38,380 --> 00:06:40,030 we're going to do in the next video 127 00:06:40,030 --> 00:06:44,820 is start the setup for creating this application 128 00:06:44,820 --> 00:06:47,040 that's going to stream a bunch of tweets 129 00:06:47,040 --> 00:06:50,860 and translate their locations into latitudes 130 00:06:50,860 --> 00:06:54,213 and longitudes so that we can plot them on a map.