I need your help | I get an error with my code

Hello.

Basically,

I have a script that downloads songs. However, at the moment I have it asking for raw_input. Instead of this, I’d just like a txt file with

song
song
song
song
song

and it download each then go to the next song (then download that one).

(title = raw_input('\n\nPlease enter the song title: ‘).replace(’ ', ‘-’)_

HMU if you can help me out with this.

I’m also happy to share the file if you like.

Thanks

Edit:

Here’s the error i now get:

Traceback (most recent call last):
File “C:\Python27\Files\Song Download\download.py”, line 34, in
req = urllib2.Request(base_url + title)
TypeError: cannot concatenate ‘str’ and ‘list’ objects

and heres my code -

#!/usr/bin/python
import requests
import urllib2
import re
import sys
import os

min_runtime = 2

# Functions
def log(string, error = False):
    print string, '\n'
    if(error):
        sys.exit()

def downloadSong(url, path):
    if url != '':
        try:
            req = urllib2.urlopen(url)
            with open(path, 'wb') as lf:
                lf.write(req.read())
            return True
        except urllib2.HTTPError, e:
            log('HTTP Error: ' + str(e.code), True)
    else:
        log('Oops nothing to download.', True)

base_url = 'http://www.mp3olimp.net/'
with open('songlist.txt', 'r') as f:
    title = f.read().split('\n') #read the file, split at newlines, return a list
    del title[-1] #Splitting at newlines gives an extra empty string at the end, remove it 
    #or you can just ignore it.
try:
    req = urllib2.Request(base_url + title)
    req.add_header('User-agent', 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.153 Safari/537.36')
    html = urllib2.urlopen(req).read()
except urllib2.HTTPError, e:
    log('HTTP Error: ' + str(e.code), True)

# Regex to parse direct links
res = re.findall('direct_link=\"(.*?)\"', html)
# Regex to parse runtimes of the links
playtime = re.findall('(\d+|)\:(?:\d+|)\<br\/\>', html)

if res and playtime:


    for index, item in enumerate(playtime):
        if int(item) >= min_runtime:
            url = res[index]
            title = os.path.split(url)[1].replace('_', ' ')
            break

    path = path = os.getcwd() + '/' + title
    log('Located a match: ' + title)
    log('Attempting to download.')
    if downloadSong(url, path):
        log('Download successful.')
else:
    log('Oops we could not find a match.', True)

Just wondered if you could fix it and help me.

Many Python developers have tried to fix it, but nobody fixed it.

It is telling you that you are concatenating a string with a list, which is disallowed. Clearly title is a list, while base_url is a string. To solve the problem, you have to use the join method on your title list. Do something like this:

req = urllib2.Request(base_url + ''.join(title))

This should do the trick, the method join on an empty string will converts your list into a string, thus you can concatenate it with your base_url string. This assumes your title is a list of strings, if it’s a list of other objects, you have to convert each element of the list to string individually. Look at this question on stackoverflow for reference if this is the case:
http://stackoverflow.com/questions/5618878/how-to-convert-list-to-string

1 Like