Created
December 26, 2012 05:21
-
-
Save mplewis/4378123 to your computer and use it in GitHub Desktop.
Opens all YouTube links in a 4chan thread in a browser window.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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