thallada.github.io/_posts/2013-06-04-w3m-reddit.md

102 lines
4.2 KiB
Markdown
Raw Normal View History

2014-07-24 03:21:28 +00:00
---
title: w3m-reddit
layout: post
redirect_from: "/blog/w3m-reddit/"
2014-07-24 03:21:28 +00:00
---
I've been moving a lot of my daily tasks to the command-line lately, and that
includes redditing. I probably spend far too much time on
[reddit](http://reddit.com) as it is, but I really wanted to find an efficient
way to view reddit through the command-line. [w3m](http://w3m.sourceforge.net/)
could render reddit okay, but I couldn't view my personal front-page because
that required me to login to my profile.
The solution was [cortex](http://cortex.glacicle.org/), a CLI app for viewing
reddit.
However, I kind of got tired of viewing reddit through w3m, the header alone is
a few pages long to scroll through, and the CSS for the comments doesn't load so
there isn't any sense of threading. But, then I discovered reddit's mobile
website: [http://m.reddit.com](http://m.reddit.com), and it looks absolutely
beautiful in w3m. In fact, I think I prefer it to the normal website in any
modern browser; there are no distractions, just pure content.
2014-07-28 19:13:49 +00:00
<a href="/img/blog/w3m_mobile_reddit.png"><img src="/img/blog/w3m_mobile_reddit.png" alt="m.reddit.com rendered in w3m"></a>
2014-07-24 03:21:28 +00:00
In order to get cortex to open the mobile version of reddit, I made a bash
script wrapper around w3m that takes urls and replaces `"http://reddit.com"` and
`"http://www.reddit.com"` with `"http://m.reddit.com"` before passing them to
w3m (as well as fixing a double forward slash error in the comment uri cortex
outputs that desktop reddit accepts but mobile reddit 404s on). The script:
2014-07-24 17:00:30 +00:00
```bash
#!/bin/bash
2014-07-24 03:21:28 +00:00
2014-07-24 17:00:30 +00:00
args=()
until [ -z "$1" ]; do
case "$1" in
-t|--tmux) t=1; shift ;;
--) shift ; break ;;
-*) echo "invalid option $1" 1>&2 ; shift ;; # or, error and exit 1 just like getopt does
*) args+=("$1") ; shift ;;
esac
done
2014-07-24 03:21:28 +00:00
2014-07-24 17:00:30 +00:00
args+=("$@")
for arg in "${args[@]}" ; do
# Switch to mobile reddit
url=${arg/http:\/\/reddit.com/https:\/\/m.reddit.com}
url=${url/http:\/\/www.reddit.com/https:\/\/m.reddit.com}
2014-07-24 17:00:30 +00:00
# Fix double backslash error in comment uri for mobile reddit
url=${url/\/\/comments/\/comments}
if [[ $t == "1" ]]; then
tmux new-window 'w3m "'${url}'"'
else
w3m "${url}"
fi
done
```
2014-07-24 03:21:28 +00:00
Since I regurally use [Tmux](http://tmux.sourceforge.net/) (with
[Byobu](http://byobu.co/)), I also added an optional `-t`/`--tmux` switch that
will open w3m in a temporary new tmux window that will close when w3m is closed.
I saved the script as `w3m-reddit` and made it an executable command. In Ubuntu
that's done with the following commands:
2014-07-24 17:00:30 +00:00
```bash
$ sudo mv w3m-reddit /usr/bin/
$ sudo chmod +x /usr/bin/w3m-reddit
```
2014-07-24 03:21:28 +00:00
Now cortex needs to be configured to use `w3m-reddit`, and that's done by
setting `browser-command` in the cortex config at `~/.cortex/config` to
`w3m-reddit`:
## Command to invoke the webbrowser
## If left empty will try to autodetect the system default browser
##browser-command=firefox '{0}'
browser-command=w3m-reddit '{0}'
The result is a distraction-free reddit experience right in the command-line
without having to edit cortex directly. I've found that I even prefer reddit
this way. Without image thumbnails (I need to explicitly select the image links
to view the image in w3m) I am more inclined to pay equal attention to every
post, not just mindlessly scrolling through meme-fests. Thus I'm more focused
and tend to not loose myself like I do in the infinite scrolling of [RES
reddit](http://redditenhancementsuite.com/) in a GUI browser.
There are still some improvements I could make to the w3m-reddit script. Namely,
it should pass along any arguments to itself to w3m underneath. I'm still a
newby at bash though, and I couldn't figure out an easy way to do that without
scrapping the whole thing and starting over in Python instead.
Stay tuned for more posts on how I view images and videos efficiently from the
command-line.
EDIT 04/25/2015: Reddit seems to have gotten rid of their old mobile reddit site
and replaced it with a more modern version that unfortunately doesn't look as
good in w3m. It still gets rid of a lot of header junk that is present in the
desktop version, though. It also doesn't work over http, so I edited the script
to always switch to https.