[Py] ircbot mthd : hwc search

Download | Vote Up (1) | Vote Down (0)
def hwc_search(self, serv, canal, message, author):
if message[:5] == "!hwc ":
    search = message[5:]
    if search == "" : return 

    url = 'http://hwc-crew.org/extern_search'
    res = []

    data = urllib.urlencode({'query' : search})
    req = urllib2.Request(url, data)
    response = urllib2.urlopen(req)
    d = response.read()
    types= json.loads(d)
    for unit in types:
        for elem in types[unit]:
            if unit == "codes" : unit = "code"
            elif unit == "articles" : unit = "paper"
            res.append("%s \t=>\thttp://hwc-crew.org/%s/%s" % (elem["title"], unit, elem["id"]))
            
    if len(res) == 0: serv.privmsg(author, "No result for : "+search)
    else:
        print "[!hwc] %d results" % len(res)
        for elem in res:
            d = normalize("NFKD",elem).encode('ASCII', 'ignore')
            serv.privmsg(author, d)

# The previous version is for hwc-crew irc bot, here is a standalone version

import urllib
import urllib2
import json
import unicodedata
import sys

def hwc_search(search):
    url = 'https://hwc-crew.org/extern_search'
    res = []

    data = urllib.urlencode({'query' : search})
    req = urllib2.Request(url, data)
    response = urllib2.urlopen(req)
    d = response.read()
    types= json.loads(d)
    for unit in types:
        for elem in types[unit]:
            if unit == "codes" : unit = "code"
            elif unit == "articles" : unit = "paper"
            res.append("%s \t=>\thttps://hwc-crew.org/%s/%s" % (elem["title"], unit, elem["id"]))

    if len(res) == 0: serv.privmsg(author, "No result for : "+search)
    else:
        print "[!hwc] %d results" % len(res)
        for elem in res:
            d = unicodedata.normalize("NFKD",elem).encode('ASCII', 'ignore')
            print d

hwc_search(sys.argv[1])

fr0g


Be the first to give feedback !

Please login to comment !