Example #1
0
    def Query(self, handles, status, abort):
        result = []

        for handle in handles:
            status.Advance()

            if abort.Aborting():
                return result

            title = handle.Format("[%title%]")
            artist = handle.Format("[%artist%]")

            try:
                l = get_lyric_list(title, artist)
                m = 0xffffffffffffffff
                best = None

                for url, ti, ar in l:
                    d = LevenshteinDistance(artist, ar) + LevenshteinDistance(
                        title, ti)
                    if m > d:
                        m = d
                        best = url

                if best == None:
                    result.append('')
                else:
                    lyric = get_lyric(best)
                    result.append(lyric)
                    continue
            except Exception, e:
                traceback.print_exc(file=sys.stdout)
                result.append('')
                continue
Example #2
0
    def Query(self, handles, status, abort):
        result = []

        for handle in handles:
            status.Advance()

            if abort.Aborting():
                return result

            try:
                artist = handle.Format("[%artist%]")
                title = handle.Format("[%title%]")
                s = urllib.urlopen(
                    "http://ttlrcct2.qianqian.com/dll/lyricsvr.dll?sh?Artist=%s&Title=%s&Flags=0"
                    % (self.ToQianQianHexString(artist),
                       self.ToQianQianHexString(title))).read()  ##这里是utf-8编码
                doc = minidom.parseString(s)
                m = 0xFFFFFFFFFFFFFFFF
                best = None

                for e in doc.getElementsByTagName("lrc"):
                    #    i = LevenshteinDistance(artist, e.getAttribute("artist")) + LevenshteinDistance(title, e.getAttribute("title"))#原来对比的是不同编码的文本
                    i = LevenshteinDistance(
                        artist,
                        e.getAttribute("artist").encode(
                            "utf-8")) + LevenshteinDistance(
                                title,
                                e.getAttribute("title").encode("utf-8"))
                    if m > i:
                        m = i
                        best = e.getAttribute("id"), e.getAttribute(
                            "artist"), e.getAttribute("title")

                if best == None:
                    result.append('')
                    continue

                Id, artist, title = best
                code = self.CreateQianQianCode(Id, artist, title)
                txheaders = {
                    'User-agent':
                    'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)'
                }
                req = urllib2.Request(
                    "http://ttlrcct2.qianqian.com/dll/lyricsvr.dll?dl?Id=%s&Code=%d"
                    % (Id, code), None, txheaders)
                lyric = urllib2.urlopen(req).read()

                if lyric.find("Search ID or Code error!") >= 0:
                    result.append('')
                    continue
                else:
                    result.append(lyric)
            except Exception, e:
                traceback.print_exc(file=sys.stdout)
                result.append('')
                continue
Example #3
0
 def __init__(self):
     self.keyword = ContextIdentifier().getKeyword()
     self.distance_counter = LevenshteinDistance()