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":
                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':
                            room.message('cant find ' + word + ' sorry')            
4 Years
Discussion Span
Last Post by Cravver

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
>>> url = 'max-results=01&vq=' + word
>>> url

This will work for longere sentence or a single word.

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

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.

#Python 3  
from urllib import request,error

    url = request.urlopen('http://google.com111').read()
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 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.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.