Find the answer to your Linux question:
Results 1 to 4 of 4
I'm trying to get a python script to run on my Netgear ReadyNas NV+, but keeping running into an indentation error. The script should be using feedparser to monitor an ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Jun 2009
    Posts
    4

    Python script errors...


    I'm trying to get a python script to run on my Netgear ReadyNas NV+, but keeping running into an indentation error. The script should be using feedparser to monitor an RSS feed (like a yahoo pipe) and automatically download the .torrent files. I've checked the script, and I do not believe I am mixing spaces and tabs anywhere, but I'm still not sure why it will not run.

    I've already installed python and the python feedparser (required for this script). Or, at least I believe I have the feedparser installed--I'm not sure how to verify it was installed successfully without running this script.

    I'd love to know if there is something glaringly obvious that I'm missing here. Script and error message below:

    Script:
    Code:
    #!/usr/bin/python
    import feedparser
    import urllib2
    
    DownloadPath = '/Path/to/downloads/'
    DownloadLog = '/Path/to/downloads/.logfile'
    FeedUrl = 'cant://post.urls.yet/'
    
    def is_downloaded(link):
        return link in open(DownloadLog, 'r+').read()
    
    def write_log(link):
        open(DownloadLog, 'a+').write('%s\\n' % link)
    
    def get_tvtorrents():
        parser = feedparser.parse(FeedUrl)
    for item in parser['items']:
        if not is_downloaded(item['link']):
        torrentfile = DownloadPath + item['title'].replace(' ', '_') + '.torrent'
        torrentdata = urllib2.urlopen(item['link']).read()
    
    open(torrentfile, 'wb').write(torrentdata)
    write_log(item['link'])
    
    print torrentfile
    
    if __name__=='__main__':
    get_tvtorrents()
    Error:
    Code:
    # python /c/delhux/rss_script3
      File "/c/delhux/rss_script3", line 10
        return link in open(DownloadLog, 'r+').read()
             ^
    IndentationError: expected an indented block

  2. #2
    Linux Engineer b2bwild's Avatar
    Join Date
    Jul 2008
    Location
    Behind You!
    Posts
    1,108
    In Python White Space is Significant

    You don't have indentation after "if condition"

    This is corrent syntax
    Code:
    #!/usr/bin/env python
    import feedparser
    import urllib2
    
    DownloadPath = '/Path/to/downloads/'
    DownloadLog = '/Path/to/downloads/.logfile'
    FeedUrl = 'cant://post.urls.yet/'
    
    def is_downloaded(link):
        return link in open(DownloadLog, 'r+').read()
    
    def write_log(link):
        open(DownloadLog, 'a+').write('%s\\n' % link)
    
    def get_tvtorrents():
        parser = feedparser.parse(FeedUrl)
    for item in parser['items']:
        if not is_downloaded(item['link']):
            torrentfile = DownloadPath + item['title'].replace(' ', '_') + '.torrent'
            torrentdata = urllib2.urlopen(item['link']).read()
    
    open(torrentfile, 'wb').write(torrentdata)
    write_log(item['link'])
    
    print torrentfile
    
    if __name__=='__main__':
        get_tvtorrents()
    To avoid silly mistakes like indentation, try to use IDEs like Komodo, WingIDE, NetBeans 6.5, SPE or Gedit with Python Indentation plugin.
    Never make any misteaks.

    Read my Blog at --> Penguin Inside Subscribe Feed

  3. #3
    Just Joined!
    Join Date
    Jun 2009
    Posts
    4
    b2bwild, thank you so much for the help. I know I never would've figured that out on my own.

    I tried the script again and it got a bit further, but it started spitting out another error. I tried troubleshooting it in TextWrangler and Komodo, but couldn't resolve it. The current error I get is
    Code:
    # python rss_script
    Traceback (most recent call last):
      File "rss_script", line 17, in <module>
        for item in parser['items']:
    NameError: name 'parser' is not defined
    I'm running the script as it was edited by b2bwild (excepting it has the correct download paths and feedurl).

    Any idea what might be triggering it?

    Thanks again...

  4. $spacer_open
    $spacer_close
  5. #4
    Just Joined!
    Join Date
    Jun 2009
    Posts
    4
    Scratch that. I managed to figure it out:
    Code:
    #!/usr/bin/python
    import feedparser
    import urllib2
    
    DownloadPath = '/Path/to/downloads/'
    DownloadLog = '/Path/to/downloads/.logfile'
    FeedUrl = 'cant://post.urls.yet/'
    
    def is_downloaded(link):
            return link in open(DownloadLog, 'r+').read()
    
    def write_log(link):
            open(DownloadLog, 'a+').write('%s\n' % link)
    
    def get_tvtorrents():
            parser = feedparser.parse(FeedUrl)
            for item in parser['items']:
                    if not is_downloaded(item['link']):
                            torrentfile = DownloadPath + item['title'].replace(' ', '_') + '.torrent'
                            torrentdata = urllib2.urlopen(item['link']).read()
                            print "Writing to: %s" % torrentfile
                            open(torrentfile, 'wb').write(torrentdata)
                            write_log(item['link'])
    
    if __name__=='__main__':
        get_tvtorrents()

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •