tags:

views:

76

answers:

1

Hi,

I'm trying to compile a python program using jython and it's throwing below error

C:\jython2.2.1>jython test.py Traceback (innermost last): (no code object) at line 0 File "test.py", line 30 html = html if html else download(url, user_agent).read() ^ SyntaxError: invalid syntax

Below is my python program. Please let me know how to resolve this.


import sys
import re
import urllib2
import urlparse
from optparse import OptionParser


# regular expression data for each website
VIDEO_DATA = [
    ('youtube.com', '%7C(.*?videoplayback.*?)%2C'),
    ('metacafe.com', '&mediaURL=(.*?)&'),
]
# default user agent to use when downloading
USER_AGENT = 'pytube'
# size of file to download
CHUNK_SIZE = 1024 * 1024



def scrape(url, html=None, user_agent=None, output=None):
    """Scrape video location from given url. 

    Use html instead of downloading if passed.
    Download file to output if passed.
    Return url of video if found, else None
    """
    netloc = urlparse.urlsplit(url).netloc
    for domain, video_re in VIDEO_DATA:
        if domain in netloc:
            html = html if html else download(url, user_agent).read()
            search = re.search(video_re, html)
            if search:
                flash_url = urllib2.unquote(search.group(1))
                if output:
                    print "Downloading flash to `%s'" % output,
                    #open(output, 'wb').write(download(flash_url, user_agent).read())
                    req = download(flash_url, user_agent)
                # testing with keyword in python
                    with open(output, 'wb') as fp:
                        chunk = True
                        while chunk:
                            chunk = req.read(CHUNK_SIZE)
                            if chunk:
                                fp.write(chunk)
                                #fp.flush()
                                print '.',
                                sys.stdout.flush()
                    print
                return flash_url
            else:
                raise PyTubeException('Failed to locate video regular expression in downloaded HTML')
    raise PyTubeException('URL did not match available domains')


def download(url, user_agent=None):
    """Download url and return data
    """
    headers = {'User-Agent' : user_agent}
    req = urllib2.Request(url, None, headers)
    return urllib2.urlopen(req)


class PyTubeException(Exception):
    pass



if __name__ == '__main__':
    # parse command line options
    parser = OptionParser(usage='usage: %prog, [-o <file.flv> -a <user_agent> -s -h] url')
    parser.add_option('-o', '--output', dest='output', help='Output file to download flash file to. If this is not specified file will not be downloaded.')
    parser.add_option('-s', '--sites', action='store_true', default=False, dest='sites', help='Display sites that pytube supports, then quit.')
    parser.add_option('-a', '--agent', dest='user_agent', default=USER_AGENT, help='Set user-agent for downloads.') 
    options, args = parser.parse_args()
    if options.sites:
        print '\n'.join(domain for (domain, reg) in VIDEO_DATA)
    else:
        if args:
            flash_url = scrape(args[0], user_agent=options.user_agent, output=options.output)
            if flash_url:
                print flash_url
        else:
            print 'Need to pass the url of the video you want to download'
            parser.print_help()

+1  A: 

Jython 2.2.1 is (AFAIK) equivalent to Cpython 2.2.1 as far as syntax is concerned. The line that causes the problem uses the ternary operator which was introduced later. the solution is to replace it with an if statement.

if not html:
     html = download(url, user_agent).read() 

That should take care of that syntax error. There is also a with clause that you will need to replace.

with open(output, 'wb') as fp:
    chunk = True
    while chunk:
        chunk = req.read(CHUNK_SIZE)
        if chunk:
            fp.write(chunk)
            #fp.flush()
            print '.',
            sys.stdout.flush()

You can replace this with

try:
    fp = open(output, 'w')
    chunk = True
    while chunk:
        chunk = req.read(CHUNK_SIZE)
            if chunk:
                fp.write(chunk)
                #fp.flush()
                print '.',
                sys.stdout.flush()
finally:
    fp.close()
aaronasterling
Now It's throwing error like ^SyntaxError: inconsistent dedentC:\jython2.2.1>jython test.pyTraceback (innermost last): (no code object) at line 0 File "test.py", line 30 if not html: html = download(url, user_agent).read() ^SyntaxError: inconsistent dedent
Sukumar Ramadugu
Sukumar: in Python, indentation is important. You're not indenting something correctly.
Wooble
Actually i'm new to python and still trying to figure it out what's going wrong with this..no luck yet..giving the same error
Sukumar Ramadugu
@Sukumar Porting from CPython to Jython might be a little ambitious. You need to study python indentation and learn to read the error messages. here is a link to help with the indentation:http://www.secnetix.de/~olli/Python/block_indentation.hawk
aaronasterling
@AaronMcSmooth Thanks a lot. I will study Identation and try to resolve. I'll let you know if i face any problem.
Sukumar Ramadugu
Indentation issue is probably mixing tabs and spaces - very hard to detect visually, and common newbie problem. Change your editor to show whitespace, or use your editor commands to convert tabs to spaces, and then align indents properly. Also, might as well set your editor to insert spaces for tabs, to help avoid this problem in the future.
Paul McGuire
Guys, Just now i resolved that problem by copying the same code to notepad and deleting some spaces and then running it again. it worked like a charm. Thanks for all your help.
Sukumar Ramadugu
Also one last question, after running will JYthon create some kind of class file like for ex: if we run test.py..will it generate test.class ?
Sukumar Ramadugu
@Sukumar Ramadugu. You'll have to check the docs for that. This is the behavior for CPython for imported modules. YMMV with Jython. in CPython, the resulting files have the extension 'pyc'. In Cpython, your module would not have the compiled code stored if you ran it from the command line. but would if you used `import mymod` to access the 'scrape' function from another module. just make sure the `mymod` is on python path.
aaronasterling