1 00:00:00,710 --> 00:00:02,210 - [Instructor] Next up we're going to show you 2 00:00:02,210 --> 00:00:04,830 how to search through recent tweets, 3 00:00:04,830 --> 00:00:08,990 so again, Twitter makes available up to the last 4 00:00:08,990 --> 00:00:12,470 seven days worth of tweets for a given account, 5 00:00:12,470 --> 00:00:14,880 and you can use the API method search 6 00:00:14,880 --> 00:00:18,240 to search for specific query strings 7 00:00:18,240 --> 00:00:20,820 that you supply as an argument. 8 00:00:20,820 --> 00:00:22,890 It's not guaranteed to give you back 9 00:00:22,890 --> 00:00:25,320 all matching tweets, just so you know, 10 00:00:25,320 --> 00:00:28,870 so you will get what is effectively a sampling of tweets 11 00:00:28,870 --> 00:00:33,520 containing those query terms that you specify. 12 00:00:33,520 --> 00:00:36,090 You're going to be using the search method, 13 00:00:36,090 --> 00:00:37,410 which in turn is going to call 14 00:00:37,410 --> 00:00:40,480 the Twitter Search API's tweets method, 15 00:00:40,480 --> 00:00:42,030 and that is going to enable you 16 00:00:42,030 --> 00:00:45,030 to get 15 tweets at a time by default, 17 00:00:45,030 --> 00:00:49,410 but you can get up to 100 of those at a time. 18 00:00:49,410 --> 00:00:51,370 Now, of course if you're going 19 00:00:51,370 --> 00:00:53,970 to be requesting a lot of results, 20 00:00:53,970 --> 00:00:57,570 then you're going to want to use a cursor object as well, 21 00:00:57,570 --> 00:01:00,870 and in this case we won't be using a cursor object 22 00:01:00,870 --> 00:01:02,550 because we're only going to request 23 00:01:02,550 --> 00:01:05,450 a few results for demonstration purposes. 24 00:01:05,450 --> 00:01:09,730 So, let me switch back over to my terminal window, 25 00:01:09,730 --> 00:01:11,450 and you'll notice that I've already 26 00:01:11,450 --> 00:01:14,100 pasted in a snippet of code here. 27 00:01:14,100 --> 00:01:16,830 We have a file that we've provided for you 28 00:01:16,830 --> 00:01:19,930 in this lessons examples folder 29 00:01:19,930 --> 00:01:21,533 that is called tweetutilities.py, 30 00:01:22,990 --> 00:01:25,670 and it has a method, or a function rather, 31 00:01:25,670 --> 00:01:28,710 in it called print_tweets that we will be using 32 00:01:28,710 --> 00:01:33,310 in this example to display the content of each tweet. 33 00:01:33,310 --> 00:01:35,660 So, I'm going to import that function, 34 00:01:35,660 --> 00:01:38,600 and I just want to show you that function quickly here. 35 00:01:38,600 --> 00:01:42,180 So, this is the tweetutilities.py file, 36 00:01:42,180 --> 00:01:44,770 which just has a number of functions that we predefined 37 00:01:44,770 --> 00:01:47,930 for you that we use in subsequent examples, 38 00:01:47,930 --> 00:01:50,150 and as I scroll down here this is 39 00:01:50,150 --> 00:01:52,840 the definition of the print_tweets function. 40 00:01:52,840 --> 00:01:56,340 It's going to receive a collection of status objects 41 00:01:56,340 --> 00:02:00,830 representing the tweets that are returned in this example 42 00:02:00,830 --> 00:02:03,230 that will be from the search API, 43 00:02:03,230 --> 00:02:06,390 and for each of the tweets in that collection 44 00:02:06,390 --> 00:02:09,040 we're going to get the user object 45 00:02:09,040 --> 00:02:11,680 from the tweet and show the screen_name. 46 00:02:11,680 --> 00:02:16,110 And then if the tweet is in the English language 47 00:02:16,110 --> 00:02:18,920 we're going to display the tweet's text, 48 00:02:18,920 --> 00:02:22,080 and if it's not in English and the language 49 00:02:22,080 --> 00:02:25,240 is not UND, which means undefined, 50 00:02:25,240 --> 00:02:29,440 then we're going to show you the original tweet's text 51 00:02:29,440 --> 00:02:32,290 and we're going to translate that tweet 52 00:02:32,290 --> 00:02:35,560 using TextBlob's translate capabilities, 53 00:02:35,560 --> 00:02:40,140 so we did import the TextBlob class up above, 54 00:02:40,140 --> 00:02:43,500 so we're going to create a TextBlob from the tweet's text, 55 00:02:43,500 --> 00:02:46,090 then we're going to invoke the translate method 56 00:02:46,090 --> 00:02:47,890 on that to get back the translated 57 00:02:47,890 --> 00:02:50,490 string and display that string. 58 00:02:50,490 --> 00:02:52,580 So, of course Twitter is used worldwide 59 00:02:52,580 --> 00:02:56,110 and tweets come in in many different spoken languages. 60 00:02:56,110 --> 00:02:58,500 If Twitter is able to recognize that language 61 00:02:58,500 --> 00:03:00,630 it will encode the tweet accordingly, 62 00:03:00,630 --> 00:03:02,700 and if it's not able to recognize it 63 00:03:02,700 --> 00:03:06,070 it will list that at UND for undefined. 64 00:03:06,070 --> 00:03:09,080 So, going back over to our session now, 65 00:03:09,080 --> 00:03:12,450 the next thing we want to do is perform a search. 66 00:03:12,450 --> 00:03:15,260 So, we're going to call the API method search, 67 00:03:15,260 --> 00:03:17,860 and again, I'll keep stating this 68 00:03:17,860 --> 00:03:19,920 throughout several of the examples, 69 00:03:19,920 --> 00:03:23,000 we really should be using cursors if we're going 70 00:03:23,000 --> 00:03:27,080 to invoke search and get a lot of results back. 71 00:03:27,080 --> 00:03:29,240 In this case we're getting only three results, 72 00:03:29,240 --> 00:03:33,540 so there really is no need to use a cursor for this example. 73 00:03:33,540 --> 00:03:37,020 The important argument is the Q argument, 74 00:03:37,020 --> 00:03:38,930 which represents the query string. 75 00:03:38,930 --> 00:03:42,450 In this case we're looking for recent tweets 76 00:03:42,450 --> 00:03:45,900 for the Mars Opportunity Rover. 77 00:03:45,900 --> 00:03:48,420 So, we're going to execute that, 78 00:03:48,420 --> 00:03:50,730 and now we have tweet objects back 79 00:03:50,730 --> 00:03:54,420 and we can call out print_tweets method, or function rather, 80 00:03:54,420 --> 00:03:57,570 and we can simply hand it the tweets object 81 00:03:57,570 --> 00:04:00,290 that we just created in the previous snippet, 82 00:04:00,290 --> 00:04:04,200 and it's going to then display those tweets to us. 83 00:04:04,200 --> 00:04:05,720 And in this case we did happen 84 00:04:05,720 --> 00:04:09,010 to get a tweet that was translated, 85 00:04:09,010 --> 00:04:12,750 so it looks like this might be Spanish 86 00:04:12,750 --> 00:04:15,450 or possibly Portuguese. 87 00:04:15,450 --> 00:04:17,180 I don't speak either of those languages, 88 00:04:17,180 --> 00:04:19,630 so I'm gonna guess Spanish based on what I'm seeing here. 89 00:04:19,630 --> 00:04:22,670 But in any case, it was able to go ahead 90 00:04:22,670 --> 00:04:24,800 and translate that into English 91 00:04:24,800 --> 00:04:29,180 as well as part of the display here. 92 00:04:29,180 --> 00:04:31,550 Now, separately we have the ability 93 00:04:31,550 --> 00:04:34,710 to work with Twitter Search operators 94 00:04:34,710 --> 00:04:37,900 to refine the searches that we're performing. 95 00:04:37,900 --> 00:04:41,530 So, let me jump back over to the slides for a moment here, 96 00:04:41,530 --> 00:04:45,130 and the search operators allow you 97 00:04:45,130 --> 00:04:48,610 to create basically more complex queries. 98 00:04:48,610 --> 00:04:51,660 I've actually already opened a browser window 99 00:04:51,660 --> 00:04:54,760 for the Twitter Search homepage, 100 00:04:54,760 --> 00:04:57,190 and the reason I did that is you'll notice right below 101 00:04:57,190 --> 00:05:00,030 the search field here there's this operators link, 102 00:05:00,030 --> 00:05:03,520 and if you click that it will take you to a page 103 00:05:03,520 --> 00:05:06,310 that allows you to see all the different 104 00:05:06,310 --> 00:05:08,440 search operators with examples. 105 00:05:08,440 --> 00:05:11,740 I have some examples that I've pasted into my slides here, 106 00:05:11,740 --> 00:05:14,500 so these are just some of the ways in which 107 00:05:14,500 --> 00:05:16,440 you can use Twitter Search operators 108 00:05:16,440 --> 00:05:19,490 as part of your query strings that you supply 109 00:05:19,490 --> 00:05:22,700 to that API object's search method. 110 00:05:22,700 --> 00:05:25,420 So, if you just specify words, 111 00:05:25,420 --> 00:05:28,880 that's implicitly going to be a logical and operation. 112 00:05:28,880 --> 00:05:31,120 So, if we search for "python twitter," 113 00:05:31,120 --> 00:05:32,520 it's going to look for tweets that 114 00:05:32,520 --> 00:05:35,470 contain both "python" and "twitter." 115 00:05:35,470 --> 00:05:38,460 If you include the word OR between words, 116 00:05:38,460 --> 00:05:40,930 that's going to be your logical OR operator, 117 00:05:40,930 --> 00:05:42,687 so it will find tweets that contain 118 00:05:42,687 --> 00:05:45,830 "python" or "twitter" or both. 119 00:05:45,830 --> 00:05:48,410 If you say "python" and you include 120 00:05:48,410 --> 00:05:50,630 a question mark in the search string 121 00:05:50,630 --> 00:05:52,450 it looks for tweets that are asking 122 00:05:52,450 --> 00:05:55,420 questions about the specified topic. 123 00:05:55,420 --> 00:05:59,170 If you specify a search term followed 124 00:05:59,170 --> 00:06:02,100 by a minus sign and another term 125 00:06:02,100 --> 00:06:05,780 it's going to find tweets containing the original term 126 00:06:05,780 --> 00:06:07,930 that don't contain the other one, 127 00:06:07,930 --> 00:06:10,610 so this particular one would look for tweets 128 00:06:10,610 --> 00:06:12,180 that contain the word "planets" 129 00:06:12,180 --> 00:06:15,560 but don't contain the word "mars." 130 00:06:15,560 --> 00:06:19,010 Some other examples, if you include a smiley face emoji, 131 00:06:19,010 --> 00:06:22,200 that's going to look for positive sentiment tweets, 132 00:06:22,200 --> 00:06:24,710 so this is a little bit of sentiment analysis 133 00:06:24,710 --> 00:06:26,770 in the context of Twitter Search. 134 00:06:26,770 --> 00:06:30,100 If you put in a sad face you'll get negative 135 00:06:30,100 --> 00:06:33,090 sentiment tweets on that specified topic. 136 00:06:33,090 --> 00:06:35,660 You also can search based on timeframes, 137 00:06:35,660 --> 00:06:39,210 so I can use the "since" keyword followed by 138 00:06:39,210 --> 00:06:41,810 a colon and a date of the form shown here, 139 00:06:41,810 --> 00:06:45,720 which is a four digit year, a month, and a day, 140 00:06:45,720 --> 00:06:50,140 and it will find tweets on or after that timeframe, 141 00:06:50,140 --> 00:06:54,400 and again, remember you can only do for the past seven days. 142 00:06:54,400 --> 00:06:59,400 So, if today were, well, today is June 12th of 2019, 143 00:07:01,030 --> 00:07:04,960 so I could go back as far as seven days ago for tweets, 144 00:07:04,960 --> 00:07:08,240 so maybe I only want tweets as far back as two days ago 145 00:07:08,240 --> 00:07:10,850 and I could specify a date if I would 146 00:07:10,850 --> 00:07:12,960 like with the "since" keyword. 147 00:07:12,960 --> 00:07:16,170 I can also look for tweets near a particular location, 148 00:07:16,170 --> 00:07:18,150 so I can give it a string like a city, 149 00:07:18,150 --> 00:07:20,690 in this case "New York City" and it will find tweets 150 00:07:20,690 --> 00:07:24,890 that came from that general area or that vicinity, 151 00:07:24,890 --> 00:07:27,360 and you can also use the "from" and "to" keywords 152 00:07:27,360 --> 00:07:29,770 to specify the particular accounts 153 00:07:29,770 --> 00:07:31,390 from which a tweet was sent 154 00:07:31,390 --> 00:07:34,760 or to which a tweet was sent as well. 155 00:07:34,760 --> 00:07:37,880 So, with that in mind we can go ahead 156 00:07:37,880 --> 00:07:41,940 and do a demonstration of some Python, 157 00:07:41,940 --> 00:07:44,640 or not Python, excuse me, Twitter Search operators. 158 00:07:44,640 --> 00:07:49,570 So, let's say that we would like to search for tweets 159 00:07:49,570 --> 00:07:53,410 from the NASA account since a particular date. 160 00:07:53,410 --> 00:07:55,690 Now, I've gone ahead here 161 00:07:55,690 --> 00:08:00,150 and changed this to June 6th of 2019. 162 00:08:00,150 --> 00:08:03,990 Again, this is June 12th of 2019, 163 00:08:03,990 --> 00:08:07,290 and I'm looking for a maximum of three tweets in this case, 164 00:08:07,290 --> 00:08:09,450 so I'll go ahead and execute that. 165 00:08:09,450 --> 00:08:12,780 And once again, I will call my print_tweets 166 00:08:12,780 --> 00:08:15,600 function to display those tweets, 167 00:08:15,600 --> 00:08:19,360 so although I'm not showing dates with each of these tweets, 168 00:08:19,360 --> 00:08:23,220 all of these would've been within seven days 169 00:08:23,220 --> 00:08:27,560 of the time that I'm doing this particular recording. 170 00:08:27,560 --> 00:08:30,720 Now, you can also search for things like hashtags, 171 00:08:30,720 --> 00:08:33,610 so of course if you're familiar with Twitter 172 00:08:33,610 --> 00:08:37,350 hashtags are a way that people 173 00:08:37,350 --> 00:08:40,690 point out important things in a tweet. 174 00:08:40,690 --> 00:08:44,500 So, for example if I want to do a search 175 00:08:44,500 --> 00:08:46,290 for the hashtag #collegefootball 176 00:08:47,660 --> 00:08:51,410 I can use a pound sign as part of my query string, 177 00:08:51,410 --> 00:08:54,647 and now it's going to look for tweets that include 178 00:08:54,647 --> 00:08:58,790 #collegefootball as part of the tweet text, 179 00:08:58,790 --> 00:09:02,160 and in this case we're just going to get two tweets, 180 00:09:02,160 --> 00:09:04,383 and we'll call print_tweets once again. 181 00:09:05,640 --> 00:09:08,240 So, in this case it looks like three, 182 00:09:08,240 --> 00:09:11,210 but in reality one of these is a tweet 183 00:09:11,210 --> 00:09:15,320 that has a line break in the middle of it, 184 00:09:15,320 --> 00:09:17,320 so it's not really three tweets that came back. 185 00:09:17,320 --> 00:09:20,063 It's two tweets in this particular case.