title: How to return a value (tuple) using the following Class script?
body: <p>The script below is taken <a href="https://pythonadventures.wordpress.com/2011/03/26/get-the-rottentomatoes-rating-of-a-movie/" rel="nofollow">from this site</a>. It doesn't currently work, but I have made it work at my own computer (not currently accessible). However, what I really want is to utilize this script to <strong>return</strong> a tuple <code>(self.tomatometer, self.audience)</code> (Look at the function <code>def _process(self)</code>).</p>
  
  <p>What I want to do is pass this script a list of movie titles (in a <code>for</code> loop) and have it return the <code>self.tomatometer</code> and <code>self.audience</code> variables to the caller.</p>
  
  <p>I managed to do this, however it seems unrecommended and convoluted: Let's say I call this script <code>convrt.py</code>, this is what I've done:</p>
  
  <pre><code>import convrt
  # this is what I'm doing, it's working, but seems weird.
  convrt.RottenTomatoesRating("Movie Title Here")._process()
  </code></pre>
  
  <p>PyCharm is warning me that I'm accessing a private method of a class. I know there isn't really anything private <s>in Python and this is what is known as "name mangling"</s>, but I still think this might not be the best way to have a tuple returned from using this script?</p>
  
  <p>The original script:</p>
  
  <pre><code>#!/usr/bin/env python
  # RottenTomatoesRating
  # Laszlo Szathmary, 2011 (jabba.laci@gmail.com)
  
  from BeautifulSoup import BeautifulSoup
  import sys
  import re
  import urllib
  import urlparse
  
  class MyOpener(urllib.FancyURLopener):
      version = 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.15) Gecko/20110303 Firefox/3.6.15'
  
  class RottenTomatoesRating:
      # title of the movie
      title = None
      # RT URL of the movie
      url = None
      # RT tomatometer rating of the movie
      tomatometer = None
      # RT audience rating of the movie
      audience = None
      # Did we find a result?
      found = False
  
      # for fetching webpages
      myopener = MyOpener()
      # Should we search and take the first hit?
      search = True
  
      # constant
      BASE_URL = 'http://www.rottentomatoes.com'
      SEARCH_URL = '%s/search/full_search.php?search=' % BASE_URL
  
      def __init__(self, title, search=True):
          self.title = title
          self.search = search
          self._process()
  
      def _search_movie(self):
          movie_url = ""
  
          url = self.SEARCH_URL + self.title
          page = self.myopener.open(url)
          result = re.search(r'(/m/.*)', page.geturl())
          if result:
              # if we are redirected
              movie_url = result.group(1)
          else:
              # if we get a search list
              soup = BeautifulSoup(page.read())
              ul = soup.find('ul', {'id' : 'movie_results_ul'})
              if ul:
                  div = ul.find('div', {'class' : 'media_block_content'})
                  if div:
                      movie_url = div.find('a', href=True)['href']
  
          return urlparse.urljoin( self.BASE_URL, movie_url )
  
      def _process(self):
          if not self.search:
              movie = '_'.join(self.title.split())
  
              url = "%s/m/%s" % (self.BASE_URL, movie)
              soup = BeautifulSoup(self.myopener.open(url).read())
              if soup.find('title').contents[0] == "Page Not Found":
                  url = self._search_movie()
          else:
              url = self._search_movie()
  
          try:
              self.url = url
              soup = BeautifulSoup( self.myopener.open(url).read() )
              self.title = soup.find('meta', {'property' : 'og:title'})['content']
              if self.title: self.found = True
  
              self.tomatometer = soup.find('span', {'id' : 'all-critics-meter'}).contents[0]
              self.audience = soup.find('span', {'class' : 'meter popcorn numeric '}).contents[0]
  
              if self.tomatometer.isdigit():
                  self.tomatometer += "%"
              if self.audience.isdigit():
                  self.audience += "%"
          except:
              pass
  
  if __name__ == "__main__":
      if len(sys.argv) == 1:
          print "Usage: %s 'Movie title'" % (sys.argv[0])
      else:
          rt = RottenTomatoesRating(sys.argv[1])
          if rt.found:
              print rt.url
              print rt.title
              print rt.tomatometer
              print rt.audience
  </code></pre>