Skipping an iteration in a for loop using an if condition does not work

54 views Asked by At

I'm writing a python script that downloads podcasts from a rss feed.

One of the features I would like to include in that script is skipping episodes that have already been downloaded in the past. This is made by printing the link to the episode into a log file before downloading it and checking that the link to the episode does not already exist in the log. I perform the test using an if condition within the for loop that goes through all the episodes.

The problem is that although the script detects that the episode has already been downloaded before, it does not refer to the continue but continues and downloads the episode again

#imports required libraries
import feedparser
import requests

#get user inputs
pod_link = input("rss feed link : ")
max_loop = input("The maximum number of podcasts to download : ")
 
#parsing the rss file
print("parsing the rss file...")
feed = feedparser.parse(pod_link)

#validating the feed
if feed['feed'] == {}:
    raise RuntimeError("Something bad happened")
 
#initialize the counter
loop = 0

#preparing the log file
pod_list = open("pod_list.lst", 'a')
pod_list.write("list of all podcasts links" + '\n')
 
#starts working
with open("pod_list.lst", 'r') as f:
    content = f.read()
    for entry in feed.entries:
        if loop == int(max_loop):
            break
        else:
            link = entry.links[0]
            pod_title = entry.title
            link1 = link["href"]

        #check the log for existing podcasts
        if link1 in content:
            print("skipping podcast number " + str(loop+1))
            continue
        else:
            print("downloading podcast number " + str(loop+1))
            pod_list = open("pod_list.lst", 'a')
            pod_list.write(link1 + '\n')
            podcast = requests.get(link1)
            open(pod_title + ".mp3", 'wb').write(podcast.content)
            loop += 1
1

There are 1 answers

0
imburningbabe On

You are incrementing the loop variable only if the if goes into the else part of code. You have to take the loop += 1 out of it:

if link1 in content:
        print("skipping podcast number " + str(loop+1))
        continue
else:
    print("downloading podcast number " + str(loop+1))
    pod_list = open("pod_list.lst", 'a')
    pod_list.write(link1 + '\n')
    podcast = requests.get(link1)
    open(pod_title + ".mp3", 'wb').write(podcast.content)
loop += 1