patternpythonMinor
Opening TV episodes in a web browser
Viewed 0 times
episodesbrowseropeningweb
Problem
I put this together and it seems to work.
1channel.py -breakingbad 1
The Output (1-10):
(1 opens the link in browser)
If anyone has any suggestions or wants to improve it:
```
from bs4 import BeautifulSoup
import urllib2, sys, webbrowser
def Work(tvshow):
print "\n[*] Working...\n"
try:
f = urllib2.urlopen(tvshow)
html = f.read()
soup = BeautifulSoup(html)
for table in soup.findAll('table',{"width":"100%"}):
for a in table.findAll('a',{"target":"_blank"}):
for li in table.findAll('li',{"class":"current-rating"}):
if a.text=="Version 2":
print "(1) - %s\n" % (li.text)
link1 = "http://www.1channel.ch" + a['href']
elif a.text=="Version 3":
print "(2) - %s\n" % (li.text)
link2 = "http://www.1channel.ch" + a['href']
elif a.text=="Version 4":
print "(3) - %s\n" % (li.text)
link3 = "http://www.1channel.ch" + a['href']
elif a.text=="Version 5":
print "(4) - %s\n" % (li.text)
link4 = "http://www.1channel.ch" + a['href']
elif a.text=="Version 6":
print "(5) - %s\n" % (li.text)
link5 = "http://www.1channel.ch" + a['href']
elif a.text=="Version 7":
print "(6) - %s\n" % (li.text)
link6 = "http://www.1channel.ch" + a['href']
elif a.text=="Version 8":
print "(7) - %s\n" % (li.text)
link7 = "http://www.1channel.ch" + a['href']
elif a.text=="Version 9":
print "(8) - %s\n" % (li.text)
link8 = "http://www.1chan
1channel.py -breakingbad 1
The Output (1-10):
(1) - Currently 3.80/5
(2) - Currently 3.50/5(1 opens the link in browser)
If anyone has any suggestions or wants to improve it:
```
from bs4 import BeautifulSoup
import urllib2, sys, webbrowser
def Work(tvshow):
print "\n[*] Working...\n"
try:
f = urllib2.urlopen(tvshow)
html = f.read()
soup = BeautifulSoup(html)
for table in soup.findAll('table',{"width":"100%"}):
for a in table.findAll('a',{"target":"_blank"}):
for li in table.findAll('li',{"class":"current-rating"}):
if a.text=="Version 2":
print "(1) - %s\n" % (li.text)
link1 = "http://www.1channel.ch" + a['href']
elif a.text=="Version 3":
print "(2) - %s\n" % (li.text)
link2 = "http://www.1channel.ch" + a['href']
elif a.text=="Version 4":
print "(3) - %s\n" % (li.text)
link3 = "http://www.1channel.ch" + a['href']
elif a.text=="Version 5":
print "(4) - %s\n" % (li.text)
link4 = "http://www.1channel.ch" + a['href']
elif a.text=="Version 6":
print "(5) - %s\n" % (li.text)
link5 = "http://www.1channel.ch" + a['href']
elif a.text=="Version 7":
print "(6) - %s\n" % (li.text)
link6 = "http://www.1channel.ch" + a['href']
elif a.text=="Version 8":
print "(7) - %s\n" % (li.text)
link7 = "http://www.1channel.ch" + a['href']
elif a.text=="Version 9":
print "(8) - %s\n" % (li.text)
link8 = "http://www.1chan
Solution
Here's what I would start with:
I haven't tested this code either, but it should work.
import sys
import re
import urllib2
import webbrowser
from bs4 import BeautifulSoup
def get_episodes(url):
print '[*] Working...'
soup = BeautifulSoup(urllib2.urlopen(url))
links = {}
for table in soup.find_all('table', width='100%'):
for a in table.find_all('a', target='_blank'):
for li in table.find_all('li', class_='current-rating'):
match = re.search(r'Version (\d+)', a.get_text())
if match:
number = match.group(1)
links[number] = 'http://www.1channel.ch' + a['href']
print '({}) - {}\n'.format(number, li.get_text())
return links
if __name__ == '__main__':
if len(sys.argv) != 3:
print '1channel.py -show episode#'
sys.exit()
elif sys.argv[1] == '-breakingbad':
url = 'tv-4128-Breaking-Bad/season-4-episode-'
elif sys.argv[1] == '-walkingdead':
url = 'tv-2490619-The-Walking-Dead/season-3-episode-'
elif sys.argv[1] == '-poi':
url = 'tv-2727923-Person-of-Interest/season-2-episode-'
else:
sys.exit()
try:
episodes = get_episodes('http://www.1channel.ch/' + url + sys.argv[2])
except urllib2.HTTPError:
print '- HTTP Error!'
sys.exit(0)
except urllib2.URLError:
print '- Connection Faliure!'
sys.exit(0)
number = int(raw_input('>>> '))
if number in episodes:
webbrowser.open(episodes[number])
else:
print 'Episode does not exist!'
sys.exit(0)- Keep functions simple and (generally) single-purpose. Have them do one simple task.
- Keep your code DRY. If you catch yourself writing the same code over and over, make a function for the general case and just fill in the variables.
- BeautifulSoup4 uses underscores instead of camel case, so
findAllbecomesfind_all.
I haven't tested this code either, but it should work.
Code Snippets
import sys
import re
import urllib2
import webbrowser
from bs4 import BeautifulSoup
def get_episodes(url):
print '[*] Working...'
soup = BeautifulSoup(urllib2.urlopen(url))
links = {}
for table in soup.find_all('table', width='100%'):
for a in table.find_all('a', target='_blank'):
for li in table.find_all('li', class_='current-rating'):
match = re.search(r'Version (\d+)', a.get_text())
if match:
number = match.group(1)
links[number] = 'http://www.1channel.ch' + a['href']
print '({}) - {}\n'.format(number, li.get_text())
return links
if __name__ == '__main__':
if len(sys.argv) != 3:
print '1channel.py -show episode#'
sys.exit()
elif sys.argv[1] == '-breakingbad':
url = 'tv-4128-Breaking-Bad/season-4-episode-'
elif sys.argv[1] == '-walkingdead':
url = 'tv-2490619-The-Walking-Dead/season-3-episode-'
elif sys.argv[1] == '-poi':
url = 'tv-2727923-Person-of-Interest/season-2-episode-'
else:
sys.exit()
try:
episodes = get_episodes('http://www.1channel.ch/' + url + sys.argv[2])
except urllib2.HTTPError:
print '- HTTP Error!'
sys.exit(0)
except urllib2.URLError:
print '- Connection Faliure!'
sys.exit(0)
number = int(raw_input('>>> '))
if number in episodes:
webbrowser.open(episodes[number])
else:
print 'Episode does not exist!'
sys.exit(0)Context
StackExchange Code Review Q#21239, answer score: 2
Revisions (0)
No revisions yet.