Skip to content

Instantly share code, notes, and snippets.

@mplewis
Created December 26, 2012 05:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mplewis/4378123 to your computer and use it in GitHub Desktop.
Save mplewis/4378123 to your computer and use it in GitHub Desktop.
Opens all YouTube links in a 4chan thread in a browser window.
#!/usr/bin/env python2
# this script grabs all youtube links in a 4chan thread and opens them in your browser.
# the regex is probably pretty shitty but it seems to work
# i dunno if it handles more than one youtube link in a post because i'm lazy
# usage:
# python chanTube.py [threadUrl]
# example:
# python chanTube.py http://boards.4chan.org/v/res/168482467
import sys
import json
import urllib2
import re
import webbrowser
rawUrl = sys.argv[1]
parts = rawUrl.split('/')
splitLoc = parts.index('res')
board = parts[splitLoc - 1]
threadNum = parts[splitLoc + 1]
url = 'http://api.4chan.org/' + board + '/res/' + str(threadNum) + '.json'
ytRegEx = '(http(s)?://)?(www\.)?youtube\.com/watch\?v=[A-Za-z0-9_-]{11}'
ytCheck = re.compile(ytRegEx, re.IGNORECASE)
print 'Fetching data from ' + url + '.'
jsonHandle = urllib2.urlopen(url)
threadData = json.load(jsonHandle)
posts = threadData['posts']
links = 0
for post in posts:
if 'com' in post:
comment = post['com']
result = ytCheck.search(comment)
if result != None:
links += 1
ytLink = result.group()
webbrowser.open(ytLink)
print 'Done. ' + str(links) + ' links opened.'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment