예제 #1
0
파일: youtube.py 프로젝트: DarkDNA/Schongo
def displayMeta(ctx, data, vid):
	"""Displays a single youtube video result, given the xml node"""
	
	s = ""
	s += "Title: %s " % data.getElementsByTagName("title")[0].firstChild.data
	s += " • By: %s"  % data.getElementsByTagName("author")[0].getElementsByTagName("name")[0].firstChild.data

	showRest = True

	r = data.getElementsByTagName("yt:state")
	if len(r):
		r = r[0]
		if r.getAttribute("name") == "restricted":
			showRest = r.getAttribute("reasonCode") == "limitedSyndication"
			if showRest:
				s += " • Syndication Limited."
			else:
				s += " • Video is unavailable: %s" % r.firstChild.data

	if showRest:
		s += " • Length: %s" % prettyTime(data.getElementsByTagName("yt:duration")[0].getAttribute("seconds"))
		s += " • View Count: %s" % prettyNumber(data.getElementsByTagName("yt:statistics")[0].getAttribute("viewCount"))

		r = data.getElementsByTagName("gd:rating")
		if len(r):
			r = r[0]
			s += " • Average Rating: %1.2f/5 over %s people" % (
				float(r.getAttribute("average")),
				prettyNumber(r.getAttribute("numRaters"))
				)
		else:
			s += " • No ratings"
	
	s += " • https://youtu.be/%s" % vid
	ctx.reply(s, "YouTube")
예제 #2
0
	def search(ctx, cmd, arg, *args):
                """so <netowkr> <tag1> <tag2> <etc>\nSearch through the stackexchange network for the given tags"""
                network = args[0]
                args = args[1:]
                
                thingy = ";".join(args)
                searchQuery = 'search?tagged={}&pagesize=3'.format(thingy)
                if network in apiURLS:
                        apiURL = apiURLS[network]
                else:
                        ctx.reply("Invalid Network","StackExchange")
                        return
                searchURL = apiURL.format(searchQuery)
                data = request.urlopen(searchURL).read()
                jsonData = gzip.decompress(data)
                decoded = jsonLoad(jsonData)
                results = decoded["total"]
                if results > 0:
                        res = min(results, 3)
                        ctx.reply("Results 1-{} of {}".format(res, prettyNumber(results)), expandedNames[network])
                else:
                        ctx.reply("No results for your query", expandedNames[network])
                for q in decoded['questions']:
                        title = q['title']
                        questionURL = questionURLS[network].format('questions', q['question_id'])
                        ctx.reply('{} • {}'.format(title, questionURL), expandedNames[network])
예제 #3
0
    def search(ctx, cmd, arg, *args):
        """so <netowkr> <tag1> <tag2> <etc>\nSearch through the stackexchange network for the given tags"""
        network = args[0]
        args = args[1:]

        thingy = ";".join(args)
        searchQuery = 'search?tagged={}&pagesize=3'.format(thingy)
        if network in apiURLS:
            apiURL = apiURLS[network]
        else:
            ctx.reply("Invalid Network", "StackExchange")
            return
        searchURL = apiURL.format(searchQuery)
        data = request.urlopen(searchURL).read()
        jsonData = gzip.decompress(data)
        decoded = jsonLoad(jsonData)
        results = decoded["total"]
        if results > 0:
            res = min(results, 3)
            ctx.reply("Results 1-{} of {}".format(res, prettyNumber(results)),
                      expandedNames[network])
        else:
            ctx.reply("No results for your query", expandedNames[network])
        for q in decoded['questions']:
            title = q['title']
            questionURL = questionURLS[network].format('questions',
                                                       q['question_id'])
            ctx.reply('{} • {}'.format(title, questionURL),
                      expandedNames[network])
예제 #4
0
	def wikipedia_cmd(ctx, cmd, arg):
		"""wikipedia <search>
Searches through wikipedia for the given search string"""
		o = openUrl("http://en.wikipedia.org/w/api.php?action=query&list=search&srsearch={}&srwhat=text&srlimit=3&format=xml".format(urllib.parse.quote(arg)))

		xml = dom.parse(o)
		
		results = int(xml.getElementsByTagName("searchinfo")[0].getAttribute("totalhits"))
		
		if results > 0:
			res = min(results, 3)
			ctx.reply("Results 1-{} out of {}".format(res, prettyNumber(results)), "Wikipedia")
		else:
			ctx.reply("No results found for {}".format(arg), "Wikipedia")

		for i in xml.getElementsByTagName("p"):
			title = i.getAttribute("title")
			snippet = i.getAttribute("snippet")
			# Hilight the matched parts
			snippet = re.sub('<span class=\'searchmatch\'>(.+?)</span>', '`B\\1`B', snippet)
			# Clean it up nice and pretty now
			snippet = re.sub('<[^>]+>', '', snippet)
			snippet = re.sub(' ([.,!?\'])', '\\1', snippet)
			snippet = snippet.replace('  ', ' ')
			# And then display it. :D
			ctx.reply("`B{}`B ( {} ) • {}".format(title, "http://wikipedia.org/wiki/{}".format(urllib.parse.quote(title)), snippet), "Wikipedia")
예제 #5
0
def displayMeta(ctx, data, vid):
    """Displays a single youtube video result, given the xml node"""

    s = ""
    s += "Title: %s " % data.getElementsByTagName("title")[0].firstChild.data
    s += " • By: %s" % data.getElementsByTagName(
        "author")[0].getElementsByTagName("name")[0].firstChild.data

    showRest = True

    r = data.getElementsByTagName("yt:state")
    if len(r):
        r = r[0]
        if r.getAttribute("name") == "restricted":
            showRest = r.getAttribute("reasonCode") == "limitedSyndication"
            if showRest:
                s += " • Syndication Limited."
            else:
                s += " • Video is unavailable: %s" % r.firstChild.data

    if showRest:
        s += " • Length: %s" % prettyTime(
            data.getElementsByTagName("yt:duration")[0].getAttribute(
                "seconds"))
        s += " • View Count: %s" % prettyNumber(
            data.getElementsByTagName("yt:statistics")[0].getAttribute(
                "viewCount"))

        r = data.getElementsByTagName("gd:rating")
        if len(r):
            r = r[0]
            s += " • Average Rating: %1.2f/5 over %s people" % (
                float(r.getAttribute("average")),
                prettyNumber(r.getAttribute("numRaters")))
        else:
            s += " • No ratings"

    addStatusToArchive(ctx, s, "YouTube")
    ctx.reply(s, "YouTube")
예제 #6
0
파일: youtube.py 프로젝트: DarkDNA/Schongo
	def youtube_cmd(ctx, cmd, arg):
		"""youtube <search string>
Searches youtube for the given search string"""
		url = "http://gdata.youtube.com/feeds/api/videos?q=%s&max-results=3&v=2" % urllib.parse.quote(arg)
		r = urllib.request.urlopen(url)
		r = dom.parse(r)
			
		results = int(r.getElementsByTagName("openSearch:totalResults")[0].firstChild.data)

		if results > 0:
			res = min(results, 3)
			ctx.reply("Results 1-%d out of %s" % (res, prettyNumber(results)), "YouTube")
		else:
			ctx.reply("No results found for %s" % arg, "YouTube")

		for i in r.getElementsByTagName("entry"):
			vid = i.getElementsByTagName("id")[0].firstChild.data
			vid = vid.split(":")[-1]

			displayMeta(ctx, i, vid)
예제 #7
0
    def youtube_cmd(ctx, cmd, arg):
        """youtube <search string>
Searches youtube for the given search string"""
        url = "http://gdata.youtube.com/feeds/api/videos?q=%s&max-results=3&v=2" % urllib.parse.quote(
            arg)
        r = urllib.request.urlopen(url)
        r = dom.parse(r)

        results = int(
            r.getElementsByTagName("openSearch:totalResults")
            [0].firstChild.data)

        if results > 0:
            res = min(results, 3)
            ctx.reply("Results 1-%d out of %s" % (res, prettyNumber(results)),
                      "YouTube")
        else:
            ctx.reply("No results found for %s" % arg, "YouTube")

        for i in r.getElementsByTagName("entry"):
            vid = i.getElementsByTagName("id")[0].firstChild.data
            vid = vid.split(":")[-1]

            displayMeta(ctx, i, vid)