<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/extracting-data-from-twitter-for-hrbrmstrs-nom-foodie-images/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://jasdumas.github.io/2018-01-16-twitter-bobs-noms/"> Jasmine Dumas' R Blog</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p><a rel="nofollow" href="https://rud.is/" target="_blank">Bob Rudis</a> (@hrbrmstr) is a famed expert, author and developer in Data Security and the Chief Security Data Scientist at Rapid7. Bob also creates the most deliciously vivid images of his meals documented by the #nom hashtag. I’m going to use a similar method used in my previous projects (<a rel="nofollow" href="https://github.com/jasdumas/hipster-veggies" target="_blank">Hipster Veggies</a> &amp; <a rel="nofollow" href="https://jasdumas.github.io/2017-05-02-twitter-ml-flashcards/" target="_blank">Machine Learning Flashcards</a>) to wrangle all those images into a nice collection – mostly for me to look at for inspiration in recipe planning.</p>
<blockquote class="twitter-tweet" data-lang="en">
<p lang="en" dir="ltr">Yum! Have you ever thought about collecting all these recipes &amp; images into a cookbook?!</p>
<p>— Jasmine Dumas (@jasdumas) <a rel="nofollow" href="https://twitter.com/jasdumas/status/952971103990898689?ref_src=twsrc%5Etfw" target="_blank">January 15, 2018</a></p></blockquote>
<p></p>
<p>Source Repository: <a rel="nofollow" href="https://github.com/jasdumas/bobs-noms" target="_blank">jasdumas/bobs-noms</a></p>
<h2>Analysis</h2>
<div>
<div>
<pre><code><span>library</span><span>(</span><span>rtweet</span><span>)</span><span> </span><span># devtools::install_github(&quot;mkearney/rtweet&quot;)</span><span>
</span><span>library</span><span>(</span><span>tidyverse</span><span>)</span><span>
</span><span>library</span><span>(</span><span>dplyr</span><span>)</span><span>
</span><span>library</span><span>(</span><span>stringr</span><span>)</span><span>
</span><span>library</span><span>(</span><span>magick</span><span>)</span><span>
</span><span>library</span><span>(</span><span>knitr</span><span>)</span><span>
</span><span>library</span><span>(</span><span>kableExtra</span><span>)</span><span>
</span></code></pre>
</div>
</div>
<div>
<div>
<pre><code><span># get all of bob's recent tweets</span><span>
</span><span>bobs_tweets</span><span> </span><span>&lt;-</span><span> </span><span>get_timeline</span><span>(</span><span>user</span><span> </span><span>=</span><span> </span><span>&quot;hrbrmstr&quot;</span><span>,</span><span> </span><span>n</span><span> </span><span>=</span><span> </span><span>3200</span><span>)</span><span>
</span><span>#filter noms with images only</span><span>
</span><span>bobs_noms</span><span> </span><span>&lt;-</span><span> </span><span>bobs_tweets</span><span> </span><span>%>%</span><span> </span><span>dplyr</span><span>::</span><span>filter</span><span>(</span><span>str_detect</span><span>(</span><span>hashtags</span><span>,</span><span> </span><span>&quot;nom&quot;</span><span>),</span><span> </span><span>!</span><span>is.na</span><span>(</span><span>media_url</span><span>))</span><span>
</span></code></pre>
</div>
</div>
<div>
<div>
<pre><code><span>bobs_noms</span><span>$</span><span>clean_text</span><span> </span><span>&lt;-</span><span> </span><span>bobs_noms</span><span>$</span><span>text</span><span>
</span><span>bobs_noms</span><span>$</span><span>clean_text</span><span> </span><span>&lt;-</span><span> </span><span>str_replace</span><span>(</span><span>bobs_noms</span><span>$</span><span>clean_text</span><span>,</span><span>&quot;#[a-zA-Z0-9]{1,}&quot;</span><span>,</span><span> </span><span>&quot;&quot;</span><span>)</span><span> </span><span># remove the hashtag</span><span>
</span><span>bobs_noms</span><span>$</span><span>clean_text</span><span> </span><span>&lt;-</span><span> </span><span>str_replace</span><span>(</span><span>bobs_noms</span><span>$</span><span>clean_text</span><span>,</span><span> </span><span>&quot; ?(f|ht)(tp)(s?)(://)(.*)[.|/](.*)&quot;</span><span>,</span><span> </span><span>&quot;&quot;</span><span>)</span><span> </span><span># remove the url link</span><span>
</span><span>bobs_noms</span><span>$</span><span>clean_text</span><span> </span><span>&lt;-</span><span> </span><span>str_replace</span><span>(</span><span>bobs_noms</span><span>$</span><span>clean_text</span><span>,</span><span> </span><span>&quot;[[:punct:]]&quot;</span><span>,</span><span> </span><span>&quot;&quot;</span><span>)</span><span> </span><span># remove punctuation</span><span>
</span></code></pre>
</div>
</div>
<div>
<div>
<pre><code><span># let's look at these images in a smaller data set</span><span>
</span><span>bobs_noms_small</span><span> </span><span>&lt;-</span><span> </span><span>bobs_noms</span><span> </span><span>%>%</span><span> </span><span>select</span><span>(</span><span>created_at</span><span>,</span><span> </span><span>clean_text</span><span>,</span><span> </span><span>media_url</span><span>)</span><span>
</span><span>bobs_noms_small</span><span>$</span><span>img_md</span><span> </span><span>&lt;-</span><span> </span><span>paste0</span><span>(</span><span>&quot;![&quot;</span><span>,</span><span> </span><span>bobs_noms_small</span><span>$</span><span>clean_text</span><span>,</span><span> </span><span>&quot;](&quot;</span><span>,</span><span> </span><span>bobs_noms_small</span><span>$</span><span>media_url</span><span>,</span><span> </span><span>&quot;)&quot;</span><span>)</span><span>
</span></code></pre>
</div>
</div>
<div>
<div>
<pre><code><span>data.frame</span><span>(</span><span>images</span><span> </span><span>=</span><span> </span><span>bobs_noms_small</span><span>$</span><span>img_md</span><span>)</span><span> </span><span>%>%</span><span> </span><span>kable</span><span>(</span><span> </span><span>format</span><span> </span><span>=</span><span> </span><span>&quot;markdown&quot;</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>kable_styling</span><span>(</span><span>full_width</span><span> </span><span>=</span><span> </span><span>F</span><span>,</span><span> </span><span>position</span><span> </span><span>=</span><span> </span><span>'center'</span><span>)</span><span> </span></code></pre>
</div>
</div>
<p>|images |<br>
|:———————————————————————————————————————————————————-|<br>
|<img alt="Moroccaninspired lamb meatballs prepped. Naan dough is kneading. Going to be a  sup tonight." src="https://i2.wp.com/pbs.twimg.com/media/DTmdeptVoAAZYpp.jpg?w=456" data-recalc-dims="1"> |<br>
|<img alt="Tsukune with tare tonight " src="https://i0.wp.com/pbs.twimg.com/media/DTTF55oX0AMyyVL.jpg?w=456" data-recalc-dims="1"> |<br>
|<img alt="Lamb roast isnt too shabby either " src="https://i1.wp.com/pbs.twimg.com/media/DS4_fRFU0AA3LVL.jpg?w=456" data-recalc-dims="1"> |<br>
|<img alt="The pain de mie thankfully came out well " src="https://i2.wp.com/pbs.twimg.com/media/DS48BemVAAAN38J.jpg?w=456" data-recalc-dims="1"> |<br>
|<img alt="Sage rosemary &amp; espresso infused salt rubbed roast lamb. Goose fat roasted potatoes _almost _ done " src="https://i1.wp.com/pbs.twimg.com/media/DR7j-HMV4AAzXpH.jpg?w=456" data-recalc-dims="1"> |<br>
|<img alt="" src="https://i0.wp.com/pbs.twimg.com/media/DQDDt9qXcAAg_Hb.jpg?w=456" data-recalc-dims="1"> |<br>
|<img alt="Ham amp; turkey frittata time! " src="https://i2.wp.com/pbs.twimg.com/media/DPfXiVBXkAAkKAK.jpg?w=456" data-recalc-dims="1"> |<br>
|<img alt="Postconfit " src="https://i0.wp.com/pbs.twimg.com/media/DO34I4oXkAEvmDS.jpg?w=456" data-recalc-dims="1"> |<br>
|<img alt="PostPBC " src="https://i0.wp.com/pbs.twimg.com/media/DO32TmIX0AAmkT7.jpg?w=456" data-recalc-dims="1"> |<br>
|<img alt="" src="https://i2.wp.com/pbs.twimg.com/media/DO2xnqcXcAAGBZO.jpg?w=456" data-recalc-dims="1"> |<br>
|<img src="https://i2.wp.com/pbs.twimg.com/media/DO2D1HQVwAEZQzk.jpg?w=456" alt="🦃" class="wp-smiley">
#joy
#nom&quot; data-recalc-dims=&quot;1&quot; /> |<br>
|<img alt="Definitely an Indonesian spring rolls kind of night " src="https://i2.wp.com/pbs.twimg.com/media/DNVuMHWWkAI4biV.jpg?w=456" data-recalc-dims="1"> |<br>
|<img alt="Homemade breadsticks for the homemade pasta and meatballs tonight " src="https://i0.wp.com/pbs.twimg.com/media/DNBMw2NWAAEwGft.jpg?w=456" data-recalc-dims="1"> |<br>
|<img alt="" src="https://i2.wp.com/pbs.twimg.com/media/DM8GWXYXcAEAFkJ.jpg?w=456" data-recalc-dims="1"> |<br>
|<img alt="Bonein PBC smoked pork roast " src="https://i2.wp.com/pbs.twimg.com/media/DMniz1TX0AAboZw.jpg?w=456" data-recalc-dims="1"> |<br>
|<img alt="Prosciutto de Parma Cacio di Bosco &amp; spinach omelettes this morning " src="https://i0.wp.com/pbs.twimg.com/media/DMlFfC0X0AAPuVf.jpg?w=456" data-recalc-dims="1"> |<br>
|<img alt="Our Friday night is shaping up well How’s yours going? " src="https://i0.wp.com/pbs.twimg.com/media/DMDWHUdX4AEJF5r.jpg?w=456" data-recalc-dims="1"> |<br>
|<img alt="Pork tenderloin on the PBC tonight " src="https://i1.wp.com/pbs.twimg.com/media/DLz-HotX4AEVVY1.jpg?w=456" data-recalc-dims="1"> |<br>
|<img alt="Overnight nutmeg-infused yeast waffles with sautéd local picked Maine apples &amp; Maine maple syrup " src="https://i2.wp.com/pbs.twimg.com/media/DK-fDgRWkAAGuhe.jpg?w=456" data-recalc-dims="1"> |</p>
<div>
<div>
<pre><code><span># create a function to save these images!</span><span>
</span><span>save_image</span><span> </span><span>&lt;-</span><span> </span><span>function</span><span>(</span><span>df</span><span>){</span><span>
</span><span>for</span><span> </span><span>(</span><span>i</span><span> </span><span>in</span><span> </span><span>c</span><span>(</span><span>1</span><span>:</span><span>nrow</span><span>(</span><span>df</span><span>))){</span><span>
</span><span>image</span><span> </span><span>&lt;-</span><span> </span><span>try</span><span>(</span><span>image_read</span><span>(</span><span>df</span><span>$</span><span>media_url</span><span>[[</span><span>i</span><span>]]),</span><span> </span><span>silent</span><span> </span><span>=</span><span> </span><span>F</span><span>)</span><span>
</span><span>if</span><span>(</span><span>class</span><span>(</span><span>image</span><span>)[</span><span>1</span><span>]</span><span> </span><span>!=</span><span> </span><span>&quot;try-error&quot;</span><span>){</span><span>
</span><span>image</span><span> </span><span>%>%</span><span>
</span><span>image_scale</span><span>(</span><span>&quot;1200x700&quot;</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>image_write</span><span>(</span><span>paste0</span><span>(</span><span>&quot;../post_data/data/&quot;</span><span>,</span><span> </span><span>bobs_noms</span><span>$</span><span>clean_text</span><span>[</span><span>i</span><span>],</span><span>&quot;.jpg&quot;</span><span>))</span><span>
</span><span>}</span><span>
</span><span>}</span><span>
</span><span>cat</span><span>(</span><span>&quot;saved images...\n&quot;</span><span>)</span><span>
</span><span>}</span><span>
</span><span>save_image</span><span>(</span><span>bobs_noms</span><span>)</span><span>
</span></code></pre>
</div>
</div>
<div>
<div>
<pre><code>## saved images...
</code></pre>
</div>
</div>
	<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://jasdumas.github.io/2018-01-16-twitter-bobs-noms/"> Jasmine Dumas' R Blog</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/ZK3-9GVgtqI" height="1"></a></div>