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 ...
- 06-05-2009 #1Just 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:
Error: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()
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
- 06-05-2009 #2
In Python White Space is Significant
You don't have indentation after "if condition"
This is corrent syntax
To avoid silly mistakes like indentation, try to use IDEs like Komodo, WingIDE, NetBeans 6.5, SPE or Gedit with Python Indentation plugin.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()
- 06-06-2009 #3Just 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 isI'm running the script as it was edited by b2bwild (excepting it has the correct download paths and feedurl).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
Any idea what might be triggering it?
Thanks again...
- 06-06-2009 #4Just 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()


Reply With Quote
