4
\$\begingroup\$

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:

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()
\$\endgroup\$

1 Answer 1

2
\$\begingroup\$

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.

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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