Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

Alright, i put this together and it seems to work.

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...

Thanks.

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.1channel.ch" + a['href']

                    elif a.text=="Version 10":
                        print "(9) - %s\n" % (li.text)
                        link9 = "http://www.1channel.ch" + a['href']

                    elif a.text=="Version 11":
                        print "(10) - %s\n" % (li.text)
                        link10 = "http://www.1channel.ch" + a['href']

        user = raw_input(">>> ")

        if user == '1':
            webbrowser.open(link1)
        elif user == '2':
            webbrowser.open(link2)
        elif user == '3':
            webbrowser.open(link3)
        elif user == '4':
            webbrowser.open(link4)
        elif user == '5':
            webbrowser.open(link5)
        elif user == '6':
            webbrowser.open(link6)
        elif user == '7':
            webbrowser.open(link7)
        elif user == '8':
            webbrowser.open(link8)
        elif user == '9':
            webbrowser.open(link9)
        elif user == '10':
            webbrowser.open(link10)
        else:
            exit(0)

    except urllib2.HTTPError:
            print "- HTTP Error!"
    except urllib2.URLError:
            print "- Connection Faliure!"
    except UnboundLocalError:
            print "Episode does not exist!"

def Main():
    if len(sys.argv) !=3:
        print "1channel.py -show episode#"
        sys.exit()

    elif sys.argv[1] == '-breakingbad':
        tvshow = 'http://www.1channel.ch/tv-4128-Breaking-Bad/season-4-episode-' + sys.argv[2]

    elif sys.argv[1] == '-walkingdead':
        tvshow = 'http://www.1channel.ch/tv-2490619-The-Walking-Dead/season-3-episode-' + sys.argv[2]

    elif sys.argv[1] == '-poi':
        tvshow = 'http://www.1channel.ch/tv-2727923-Person-of-Interest/season-2-episode-' + sys.argv[2]

    else:
        sys.exit()

    Work(tvshow)

if __name__ == '__main__':
    Main()
share|improve this question

1 Answer 1

Here's what I would start with:

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 findAll becomes find_all.

I haven't tested this code either, but it should work.

share|improve this answer
    
Wow thanks! there is a problem with raw_input have any ideas? –  Joe_Bonanno Feb 2 '13 at 22:31
    
int(raw_input(...)) expects a number. Are you typing one in? –  Blender Feb 3 '13 at 1:38
    
Yeah, it goes straight to the else: –  Joe_Bonanno Feb 3 '13 at 3:17
    
Changed it to number = raw_input(...) and that works. Not sure why int(raw_input(...)) doesn't... –  Joe_Bonanno Feb 3 '13 at 20:45
    
@Joe_Bonanno: Oh, whoops. I never converted number to an int. –  Blender Feb 3 '13 at 20:46

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.