设为首页收藏本站

Scripts 学盟

 找回密码
 加入学盟

QQ登录

只需一步,快速开始

查看: 5352|回复: 0
打印 上一主题 下一主题

从千千静听的歌词服务器查询音乐的 LRC 歌词 [复制链接]

管理员

超级大菜鸟

Rank: 9Rank: 9Rank: 9

跳转到指定楼层
1#
Alvin 实名认证  发表于 2011-4-30 12:40:06 |只看该作者 |倒序浏览
最近要搞这个,百度一下,发现有 php 版本的代码,搞来翻译一下。
  1. package org.iscripts.services;

  2. import java.io.IOException;
  3. import java.io.StringReader;
  4. import java.io.UnsupportedEncodingException;

  5. import org.dom4j.Document;
  6. import org.dom4j.DocumentException;
  7. import org.dom4j.Element;
  8. import org.dom4j.Node;
  9. import org.dom4j.io.SAXReader;
  10. import org.iscripts.common.net.HttpGet;

  11. /**
  12. * @author 林俊海(ialvin.cn) 广东·普宁·里湖
  13. */
  14. public class LrcGet {
  15.   public static void main(String[] args) throws Exception {
  16.     System.out.println(query("你把爱情给了谁", "王强"));
  17.   }

  18.   public static String query(String title, String artist)
  19.       throws DocumentException, NumberFormatException, IOException {
  20.     String URL = "http://ttlrcct.qianqian.com/dll/lyricsvr.dll?sh?Artist={ar}&Title={ti}&Flags=0";
  21.     URL = URL.replace("{ar}", encode(artist))
  22.         .replace("{ti}", encode(title));
  23.     String result = "";
  24.     try {
  25.       result = HttpGet.get(URL);
  26.     } catch (Exception e) {
  27.       e.printStackTrace();
  28.     }
  29.     // 需要 dom4j-1.6.1.jar , 使用可参考 dom4j 手册
  30.     Document doc = new SAXReader().read(new StringReader(result));
  31.     // XML中可能包含多个匹配结果,我们只取一个
  32.     Node n = doc.selectSingleNode("/result/lrc");
  33.     if (n == null)
  34.       return null;
  35.     Element e = (Element) n;
  36.     result = e.attributeValue("id");
  37.     artist = e.attributeValue("artist");
  38.     title = e.attributeValue("title");

  39.     URL = "http://ttlrcct2.qianqian.com/dll/lyricsvr.dll?dl?Id={id}&Code={code}";
  40.     URL = URL.replace("{id}", result).replace("{code}",
  41.         verifyCode(artist, title, Integer.parseInt(result, 10)));
  42.     return HttpGet.get(URL);
  43.   }

  44.   public static String verifyCode(String artist, String title, int lrcId)
  45.       throws UnsupportedEncodingException {
  46.     byte[] bytes = (artist + title).getBytes("UTF-8");
  47.     int[] song = new int[bytes.length];
  48.     for (int i = 0; i < bytes.length; i++)
  49.       song[i] = bytes[i] & 0xff;
  50.     int intVal1 = 0, intVal2 = 0, intVal3 = 0;
  51.     intVal1 = (lrcId & 0xFF00) >> 8;
  52.     if ((lrcId & 0xFF0000) == 0) {
  53.       intVal3 = 0xFF & ~intVal1;
  54.     } else {
  55.       intVal3 = 0xFF & ((lrcId & 0x00FF0000) >> 16);
  56.     }
  57.     intVal3 = intVal3 | ((0xFF & lrcId) << 8);
  58.     intVal3 = intVal3 << 8;
  59.     intVal3 = intVal3 | (0xFF & intVal1);
  60.     intVal3 = intVal3 << 8;
  61.     if ((lrcId & 0xFF000000) == 0) {
  62.       intVal3 = intVal3 | (0xFF & (~lrcId));
  63.     } else {
  64.       intVal3 = intVal3 | (0xFF & (lrcId >> 24));
  65.     }
  66.     int uBound = bytes.length - 1;
  67.     while (uBound >= 0) {
  68.       int c = song[uBound];
  69.       if (c >= 0x80)
  70.         c = c - 0x100;
  71.       intVal1 = c + intVal2;
  72.       intVal2 = intVal2 << (uBound % 2 + 4);
  73.       intVal2 = intVal1 + intVal2;
  74.       uBound -= 1;
  75.     }
  76.     uBound = 0;
  77.     intVal1 = 0;
  78.     while (uBound <= bytes.length - 1) {
  79.       int c = song[uBound];
  80.       if (c >= 128)
  81.         c = c - 256;
  82.       int intVal4 = c + intVal1;
  83.       intVal1 = intVal1 << (uBound % 2 + 3);
  84.       intVal1 = intVal1 + intVal4;
  85.       uBound += 1;
  86.     }
  87.     int intVal5 = intVal2 ^ intVal3;
  88.     intVal5 = intVal5 + (intVal1 | lrcId);
  89.     intVal5 = intVal5 * (intVal1 | intVal3);
  90.     intVal5 = intVal5 * (intVal2 ^ lrcId);
  91.     return String.valueOf(intVal5);
  92.   }

  93.   private static String encode(String value) {
  94.     if (value == null)
  95.       value = "";
  96.     value = value.replace(" ", "").replace("'", "").toLowerCase();
  97.     byte[] bytes = null;
  98.     try {
  99.       bytes = value.getBytes("UTF-16LE");
  100.     } catch (UnsupportedEncodingException e) {
  101.       e.printStackTrace();
  102.       bytes = value.getBytes();
  103.     }
  104.     return stringify(bytes);
  105.   }

  106.   final private static char[] digit = { '0', '1', '2', '3', '4', '5', '6',
  107.       '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };

  108.   private static String stringify(byte[] bytes) {
  109.     char[] str = new char[2];
  110.     StringBuilder builder = new StringBuilder();
  111.     for (byte byteValue : bytes) {
  112.       str[0] = digit[(byteValue >>> 4) & 0X0F];
  113.       str[1] = digit[byteValue & 0X0F];
  114.       builder.append(str);
  115.     }
  116.     return builder.toString();
  117.   }
  118. }
复制代码
其中 HttpGet.get(.....)  方法是 HTTP 抓取的功能,参考:
http://www.iscripts.org/bbs/viewthread.php?tid=84

ASP 版的千千静听 LRC 歌词查询 请点这里
分享到: QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
分享分享0 收藏收藏0
您需要登录后才可以回帖 登录 | 加入学盟

手机版|Scripts 学盟   |

GMT+8, 2024-12-19 01:59 , Processed in 1.093000 second(s), 14 queries .

Powered by Discuz! X2

© 2001-2011 Comsenz Inc.

回顶部