<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Bash on Prateek Punetha</title><link>/tags/bash/</link><description>Recent content in Bash on Prateek Punetha</description><generator>Hugo -- gohugo.io</generator><language>en</language><lastBuildDate>Wed, 01 Mar 2023 00:00:00 +0000</lastBuildDate><atom:link href="/tags/bash/index.xml" rel="self" type="application/rss+xml"/><item><title>Back to Bash: Reflections on Filtering Wikipedia Episode Links a Year Later</title><link>/posts/filter-wikipedia-episodes/</link><pubDate>Wed, 01 Mar 2023 00:00:00 +0000</pubDate><guid>/posts/filter-wikipedia-episodes/</guid><description>&lt;h1 id="how-i-extracted-tv-episode-titles-using-command-line-magic">How I Extracted TV Episode Titles Using Command Line Magic&lt;/h1>
&lt;p>I enjoy watching TV shows and prefer to torrent them for offline viewing. The issue is that these shows often have strange names that don&amp;rsquo;t make sense and can be difficult to keep track of, especially since various individuals and groups post them on torrent sites. While I can use Wikipedia to obtain the actual episode names, manually copying and renaming each episode can be time-consuming. Therefore, I decided to automate the process using command-line tools and Ranger&amp;rsquo;s bulk renaming feature.&lt;/p></description><content>&lt;h1 id="how-i-extracted-tv-episode-titles-using-command-line-magic">How I Extracted TV Episode Titles Using Command Line Magic&lt;/h1>
&lt;p>I enjoy watching TV shows and prefer to torrent them for offline viewing. The issue is that these shows often have strange names that don&amp;rsquo;t make sense and can be difficult to keep track of, especially since various individuals and groups post them on torrent sites. While I can use Wikipedia to obtain the actual episode names, manually copying and renaming each episode can be time-consuming. Therefore, I decided to automate the process using command-line tools and Ranger&amp;rsquo;s bulk renaming feature.&lt;/p>
&lt;h2 id="the-problem">The Problem&lt;/h2>
&lt;p>First, I needed to find a way to scrape the episode title from a Wikipedia page. After some research, I discovered that the best way to do this was to download the page&amp;rsquo;s source code and then extract the relevant information using a tool like grep.&lt;/p>
&lt;h2 id="the-solution">The Solution&lt;/h2>
&lt;p>I started by writing a simple curl command that would download the source code of the Wikipedia page for a given TV show:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-bash" data-lang="bash">&lt;span style="display:flex;">&lt;span>curl -s &lt;span style="color:#e6db74">&amp;#34;&lt;/span>$1&lt;span style="color:#e6db74">?action=raw&amp;#34;&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>This command uses the -s flag to suppress the output of curl, and $1 is the URL of the Wikipedia page. The ?action=raw part of the URL ensures that we get the raw source code of the page, rather than the formatted HTML.&lt;/p>
&lt;p>Next, I needed to extract the episode title from the source code. After looking through the code, I found that each episode title was surrounded by a tag that looked like this:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-bash" data-lang="bash">&lt;span style="display:flex;">&lt;span>&lt;span style="color:#f92672">==&lt;/span> &lt;span style="color:#e6db74">&amp;#34;Title&amp;#34;&lt;/span> &lt;span style="color:#f92672">==&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>To extract this information, I used grep to search for the tag and then extracted the episode title using sed. Here&amp;rsquo;s the final command:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-bash" data-lang="bash">&lt;span style="display:flex;">&lt;span>curl -s &lt;span style="color:#e6db74">&amp;#34;&lt;/span>$1&lt;span style="color:#e6db74">?action=raw&amp;#34;&lt;/span> | grep -B10 &lt;span style="color:#e6db74">&amp;#39;ShortSummary&amp;#39;&lt;/span> | grep -w &lt;span style="color:#e6db74">&amp;#39;Title&amp;#39;&lt;/span> | sed &lt;span style="color:#e6db74">&amp;#39;s/.*Title.*= //&amp;#39;&lt;/span> | nl -w6 -w1 -s &lt;span style="color:#e6db74">&amp;#34; - &amp;#34;&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>Let&amp;rsquo;s break this command down:&lt;/p>
&lt;ul>
&lt;li>
&lt;p>&lt;code>bash curl -s &amp;quot;$1?action=raw&amp;quot;&lt;/code>downloads the source code of the Wikipedia page.&lt;/p>
&lt;/li>
&lt;li>
&lt;p>&lt;code>grep -B10 'ShortSummary'&lt;/code>searches for the line that contains the string &amp;ldquo;ShortSummary&amp;rdquo; and returns the 10 lines before it.&lt;/p>
&lt;/li>
&lt;li>
&lt;p>&lt;code>grep -w 'Title'&lt;/code> searches for the line that contains the word &amp;ldquo;Title&amp;rdquo; and returns it.&lt;/p>
&lt;/li>
&lt;li>
&lt;p>&lt;code>sed 's/.*Title.*= //'&lt;/code> removes everything before the episode title.&lt;/p>
&lt;/li>
&lt;li>
&lt;p>&lt;code>nl -w6 -w1 -s &amp;quot; - &amp;quot;&lt;/code> adds numbering to the output.
With this command, I could simply pass in the URL of a TV show&amp;rsquo;s Wikipedia page, and it would return a numbered list of all the episode titles.&lt;/p>
&lt;p>Then, I can simply copy these names and use Ranger&amp;rsquo;s bulk rename feature to rename them.&lt;/p>
&lt;/li>
&lt;/ul>
&lt;h2 id="conclusion">Conclusion&lt;/h2>
&lt;p>Through some trial and error, I was able to come up with a simple command-line script that makes my life a little bit easier. I&amp;rsquo;m excited to keep exploring new ways to use command-line tools to solve everyday problems.&lt;/p>
&lt;ul>
&lt;li>&lt;a href="https://github.com/prateekpunetha/dotfiles/blob/master/bin/bin/eplist">&lt;strong>Link to the script&lt;/strong>&lt;/a>&lt;/li>
&lt;/ul>
&lt;p>I hope you found this post helpful, and feel free to reach out to me if you have any questions or feedback!&lt;/p></content></item></channel></rss>