Sinisterly
Youtube Module - Printable Version

+- Sinisterly (https://sinister.ly)
+-- Forum: Coding (https://sinister.ly/Forum-Coding)
+--- Forum: Python (https://sinister.ly/Forum-Python)
+--- Thread: Youtube Module (/Thread-Youtube-Module)



Youtube Module - Stocking - 02-01-2014

NOTE
I did not create this. I merely found it on the internet and cleaned it up a bit. I am also not a professional Python programmer so as far as the cleaning goes there may or may not be tiny errors.

Okay, so I found a Youtube module on the internets but it was a little unorganized. It was fine but needed some cleaning if you will. Probably my OCD talking. Here's my fix:

Code:
from urllib.request import urlopen
import re

class YoutubeException(Exception):
    pass

def tag_regex_construct(tag):
    return re.compile(r"<%s ?[^>]*>(.*?)</%s>" % (tag,tag), re.IGNORECASE | re.DOTALL )

title_re = tag_regex_construct("title")
desc_re  = tag_regex_construct("content")
auth_re  = tag_regex_construct("name")

class Video:
    def __init__(self,vid,update = True):
        self.vid = vid
        if(update): self.update()

    def update(self):
        resp = urlopen(self.get_api_link())
        if(resp):
            data       = resp.read().decode()
            self.title = title_re.search(data).group(1)
            try:
                self.desc = desc_re.search(data).group(1)
            except:
                self.desc = "No description available."
            self.auth  = auth_re.search(data).group(1)
        else:
            raise YoutubeException("Video ID seems invalid.")

    def get_api_link(self):
        return "http://gdata.youtube.com/feeds/api/videos/%s" % (self.vid)

    def get_id(self):
        return self.vid

    def get_title(self):
        return self.title

    def get_desc(self):
        return self.desc

    def get_auth(self):
        return self.auth

Duubz, the fuck is this shit used for?

I don't fucking know, maybe a bot. Intellect uses it. Jump off my dick.