While I know some Python language I just can seem to make thi work:

So I have this script that when you post a command: -ytb [search terms], it would look up the video in an atom feed from yuotube, could you tell me what I have wroong here? Because when I sseatch up a term with a "space" in it it cannot find the term searched, but when typed with just one word it works. Surprisingly this does not use any youtube modules, but can find videos , I just cant include spaces in the search terms.

        elif cmd == "-ytb":
            try:
                word = args
                results = []
                data = urlreq.urlopen('http://gdata.youtube.com/feeds/api/videos?max-results=01&vq=' + word)
                tree = ET.parse(data)
                ns = '{http://www.w3.org/2005/Atom}'
                for entry in tree.findall(ns + 'entry'):
                    for link in entry.findall(ns + 'link'):
                        if link.get('rel') == 'alternate':
                            results.append(link.get('href'))
                            room.message(link.get('href'))
            except:
                            room.message('cant find ' + word + ' sorry')            

A space in in gdata youtube search is %20
So if you search for car bmw,then it should look like this car%20bmw
Here is one way of do it.

>>> word = 'car bmw'
>>> word = word.split()
>>> word
['car', 'bmw']
>>> space = '%20'
>>> word = space.join(word)
>>> word
'car%20bmw'
>>> url = 'max-results=01&vq=' + word
>>> url
'max-results=01&vq=car%20bmw'

This will work for longere sentence or a single word.

>>> word = 'car'
>>> word = word.split()
>>> word
['car']
>>> word = space.join(word)
>>> word
'car'
>>> url = 'max-results=01&vq=' + word
>>> url
'max-results=01&vq=car'

You should not do error handling(try,except) in that way.
Try to make it clear what error you will catch,and do not put other stuff if try,except block.
example:

#Python 3  
from urllib import request,error

try:
    url = request.urlopen('http://google.com111').read()
    print(url)
except error.URLError:
    print('No url by that name found')

Here it is clear that we only catch error if a url adress is wrong or site is down.
If you later need error check like append stuff to a list,you write a own error chek for that.

Edited 3 Years Ago by snippsat

Thanks man you helped me out a lot the script im preparing doesnt use print exception that way so i had to change it a bit.

This article has been dead for over six months. Start a new discussion instead.