正则表达式判断是否是QQ表情,返回超链接。
/**
* QQface处理器
* @author 熊诗言
*
*/
public class QQFaceTextHandler extends DefaultMessageHandler {
@Override
public boolean canDo(Map<String, String> requestMap) {
String content = requestMap.get("Content").trim();
String msgType = requestMap.get("MsgType");
return MessageUtil.REQ_MESSAGE_TYPE_TEXT.equals(msgType) && ContentUtil.isQqFace(content);
}
@Override
public BaseMessage handleByMe(Map<String, String> requestMap) {
String content = requestMap.get("Content").trim();
content = content+"<a href=\"http://1.xxssyyyyssxx.sinaapp.com/index.html\">主页</a>";
return MessageFactory.createTextMessage(fromUserName, toUserName,content);
}
}
public static boolean isQqFace(String content) {
boolean result = false;
// 判断QQ 表情的正则表达式
String qqfaceRegex = "/::\\)|/::~|/::B|/::\\||/:8-\\)|/::<|/::$|/::X|/::Z|/::'\\(|/::-\\||/::@|/::P|/::D|/::O|/::\\(|/::\\+|/:--b|/::Q|/::T|/:,@P|/:,@-D|/::d|/:,@o|/::g|/:\\|-\\)|/::!|/::L|/::>|/::,@|/:,@f|/::-S|/:\\?|/:,@x|/:,@@|/::8|/:,@!|/:!!!|/:xx|/:bye|/:wipe|/:dig|/:handclap|/:&-\\(|/:B-\\)|/:<@|/:@>|/::-O|/:>-\\||/:P-\\(|/::'\\||/:X-\\)|/::\\*|/:@x|/:8\\*|/:pd|/:<W>|/:beer|/:basketb|/:oo|/:coffee|/:eat|/:pig|/:rose|/:fade|/:showlove|/:heart|/:break|/:cake|/:li|/:bome|/:kn|/:footb|/:ladybug|/:shit|/:moon|/:sun|/:gift|/:hug|/:strong|/:weak|/:share|/:v|/:@\\)|/:jj|/:@@|/:bad|/:lvu|/:no|/:ok|/:love|/:<L>|/:jump|/:shake|/:<O>|/:circle|/:kotow|/:turn|/:skip|/:oY|/:#-0|/:hiphot|/:kiss|/:<&|/:&>";
Pattern p = Pattern.compile(qqfaceRegex);
Matcher m = p.matcher(content);
if (m.matches()) {
result = true;
}
return result;
}
从百度音乐上搜索音乐
/**
* 搜索音乐的处理器
* @author 熊诗言
*
*/
public class QueryMusicTextHandler extends DefaultMessageHandler {
@Override
public boolean canDo(Map<String, String> requestMap) {
String content = requestMap.get("Content").trim();
String msgType = requestMap.get("MsgType");
return MessageUtil.REQ_MESSAGE_TYPE_TEXT.equals(msgType) && content.startsWith("歌曲");
}
@Override
public BaseMessage handleByMe(Map<String, String> requestMap) {
String content = requestMap.get("Content").trim();
// 将歌曲2 个字及歌曲后面的+、空格、-等特殊符号去掉
//String keyWord = content.replaceAll("^歌曲", "");//[\\+ ~!@#%^-_=]?
// 如果歌曲名称为空
if ("歌曲".equals(content)) {
return MessageFactory.createTextMessage(fromUserName, toUserName, ContentUtil.getMusicUsage());
}else {
String[] kwArr = content.split("@");//歌曲@歌名@作者
// 歌曲名称
String musicTitle = kwArr[1];
// 演唱者默认为空
String musicAuthor = "";
if (3 == kwArr.length)
musicAuthor = kwArr[2];
// 搜索音乐
Music music = BaiduMusicService.searchMusic(musicTitle, musicAuthor);
// 未搜索到音乐
if (null == music) {
return MessageFactory.createTextMessage(fromUserName, toUserName,"对不起,没有找到你想听的歌曲<" + musicTitle + ">。");
}else {
return MessageFactory.createMusicMessage(fromUserName, toUserName, music);
}
}
}
}
/**
* 歌曲点播使用指南
*
* @return
*/
public static String getMusicUsage() {
StringBuffer buffer = new StringBuffer();
buffer.append("歌曲点播操作指南").append("\n\n");
buffer.append("回复:歌曲@歌名").append("\n");
buffer.append("例如:歌曲@存在").append("\n");
buffer.append("或者:歌曲@存在@汪峰").append("\n\n");
buffer.append("回复“?/?”显示主菜单");
return buffer.toString();
}
/**
* 百度音乐搜索API 操作类
*XML解析
* @author 熊诗言
* @date 2015-09-05
*/
public class BaiduMusicService {
/**
* 根据名称和作者搜索音乐
*
* @param musicTitle 音乐名称
* @param musicAuthor 音乐作者
* @return Music
*/
public static Music searchMusic(String musicTitle, String musicAuthor) {
// 百度音乐搜索地址
String requestUrl = "http://box.zhangmen.baidu.com/x?op=12&count=1&title={title}$${author}$$$$";
// 对音乐名称、作者进URL 编码
requestUrl = requestUrl.replace("{title}", ContentUtil.urlEncodeUTF8(musicTitle));
requestUrl = requestUrl.replace("{author}", ContentUtil.urlEncodeUTF8(musicAuthor));
// 处理名称、作者中间的空格
requestUrl = requestUrl.replaceAll("\\+", "%20");
// 查询并获取返回结果
InputStream inputStream = httpRequest(requestUrl);
// 从返回结果中解析出Music
Music music = parseMusic(inputStream);
// 如果music 不为null,设置标题和描述
if (null != music) {
music.setTitle(musicTitle);
// 如果作者不为"",将描述设置为作者
if (!"".equals(musicAuthor))
music.setDescription(musicAuthor);
else
music.setDescription("来自百度音乐");
}
return music;
}
/**
* 发送http 请求取得返回的输入流
*
* @param requestUrl 请求地址
* @return InputStream
*/
private static InputStream httpRequest(String requestUrl) {
InputStream inputStream = null;
try {
URL url = new URL(requestUrl);
HttpURLConnection httpUrlConn = (HttpURLConnection) url.openConnection();
httpUrlConn.setDoInput(true);
httpUrlConn.setRequestMethod("GET");
httpUrlConn.connect();
// 获得返回的输入流
inputStream = httpUrlConn.getInputStream();
} catch (Exception e) {
e.printStackTrace();
}
return inputStream;
}
/**
* 解析音乐参数
*
* @param inputStream 百度音乐搜索API 返回的输入流
* @return Music
*/
@SuppressWarnings("unchecked")
private static Music parseMusic(InputStream inputStream) {
Music music = null;
try {
// 使用dom4j 解析xml 字符串
SAXReader reader = new SAXReader();
Document document = reader.read(inputStream);
// 得到xml 根元素
Element root = document.getRootElement();
// count 表示搜索到的歌曲数
String count = root.element("count").getText();
// 当搜索到的歌曲数大于0 时
if (!"0".equals(count)) {
// 普通品质
List<Element> urlList = root.elements("url");
// 高品质
List<Element> durlList = root.elements("durl");
// 普通品质的encode、decode
String urlEncode = urlList.get(0).element("encode").getText();
String urlDecode = urlList.get(0).element("decode").getText();
// 普通品质音乐的URL
String url = urlEncode.substring(0, urlEncode.lastIndexOf("/") + 1) + urlDecode;
if (-1 != urlDecode.lastIndexOf("&"))
url = urlEncode.substring(0, urlEncode.lastIndexOf("/")+ 1) + urlDecode.substring(0, urlDecode.lastIndexOf("&"));
// 默认情况下,高音质音乐的URL 等于普通品质音乐的URL
String durl = url;
// 判断高品质节点是否存在
Element durlElement = durlList.get(0).element("encode");
if (null != durlElement) {
// 高品质的encode、decode
String durlEncode = durlList.get(0).element("encode").getText();
String durlDecode = durlList.get(0).element("decode").getText();
// 高品质音乐的URL
durl = durlEncode.substring(0, durlEncode.lastIndexOf("/") + 1) + durlDecode;
if (-1 != durlDecode.lastIndexOf("&"))
durl = durlEncode.substring(0, durlEncode.lastIndexOf("/") + 1) + durlDecode.substring(0, durlDecode.lastIndexOf("&"));
}
music = new Music();
// 设置普通品质音乐链接
music.setMusicUrl(url);
// 设置高品质音乐链接
music.setHQMusicUrl(durl);
}
} catch (Exception e) {
e.printStackTrace();
}
return music;
}
// 测试方法
public static void main(String[] args) {
Music music = searchMusic("相信自己", "零点乐队");
System.out.println("音乐名称:" + music.getTitle());
System.out.println("音乐描述:" + music.getDescription());
System.out.println("普通品质链接:" + music.getMusicUrl());
System.out.println("高品质链接:" + music.getHQMusicUrl());
}
}
/**
* QQface处理器
* @author 熊诗言
*
*/
public class QQFaceTextHandler extends DefaultMessageHandler {
@Override
public boolean canDo(Map<String, String> requestMap) {
String content = requestMap.get("Content").trim();
String msgType = requestMap.get("MsgType");
return MessageUtil.REQ_MESSAGE_TYPE_TEXT.equals(msgType) && ContentUtil.isQqFace(content);
}
@Override
public BaseMessage handleByMe(Map<String, String> requestMap) {
String content = requestMap.get("Content").trim();
content = content+"<a href=\"http://1.xxssyyyyssxx.sinaapp.com/index.html\">主页</a>";
return MessageFactory.createTextMessage(fromUserName, toUserName,content);
}
}
public static boolean isQqFace(String content) {
boolean result = false;
// 判断QQ 表情的正则表达式
String qqfaceRegex = "/::\\)|/::~|/::B|/::\\||/:8-\\)|/::<|/::$|/::X|/::Z|/::'\\(|/::-\\||/::@|/::P|/::D|/::O|/::\\(|/::\\+|/:--b|/::Q|/::T|/:,@P|/:,@-D|/::d|/:,@o|/::g|/:\\|-\\)|/::!|/::L|/::>|/::,@|/:,@f|/::-S|/:\\?|/:,@x|/:,@@|/::8|/:,@!|/:!!!|/:xx|/:bye|/:wipe|/:dig|/:handclap|/:&-\\(|/:B-\\)|/:<@|/:@>|/::-O|/:>-\\||/:P-\\(|/::'\\||/:X-\\)|/::\\*|/:@x|/:8\\*|/:pd|/:<W>|/:beer|/:basketb|/:oo|/:coffee|/:eat|/:pig|/:rose|/:fade|/:showlove|/:heart|/:break|/:cake|/:li|/:bome|/:kn|/:footb|/:ladybug|/:shit|/:moon|/:sun|/:gift|/:hug|/:strong|/:weak|/:share|/:v|/:@\\)|/:jj|/:@@|/:bad|/:lvu|/:no|/:ok|/:love|/:<L>|/:jump|/:shake|/:<O>|/:circle|/:kotow|/:turn|/:skip|/:oY|/:#-0|/:hiphot|/:kiss|/:<&|/:&>";
Pattern p = Pattern.compile(qqfaceRegex);
Matcher m = p.matcher(content);
if (m.matches()) {
result = true;
}
return result;
}
从百度音乐上搜索音乐
/**
* 搜索音乐的处理器
* @author 熊诗言
*
*/
public class QueryMusicTextHandler extends DefaultMessageHandler {
@Override
public boolean canDo(Map<String, String> requestMap) {
String content = requestMap.get("Content").trim();
String msgType = requestMap.get("MsgType");
return MessageUtil.REQ_MESSAGE_TYPE_TEXT.equals(msgType) && content.startsWith("歌曲");
}
@Override
public BaseMessage handleByMe(Map<String, String> requestMap) {
String content = requestMap.get("Content").trim();
// 将歌曲2 个字及歌曲后面的+、空格、-等特殊符号去掉
//String keyWord = content.replaceAll("^歌曲", "");//[\\+ ~!@#%^-_=]?
// 如果歌曲名称为空
if ("歌曲".equals(content)) {
return MessageFactory.createTextMessage(fromUserName, toUserName, ContentUtil.getMusicUsage());
}else {
String[] kwArr = content.split("@");//歌曲@歌名@作者
// 歌曲名称
String musicTitle = kwArr[1];
// 演唱者默认为空
String musicAuthor = "";
if (3 == kwArr.length)
musicAuthor = kwArr[2];
// 搜索音乐
Music music = BaiduMusicService.searchMusic(musicTitle, musicAuthor);
// 未搜索到音乐
if (null == music) {
return MessageFactory.createTextMessage(fromUserName, toUserName,"对不起,没有找到你想听的歌曲<" + musicTitle + ">。");
}else {
return MessageFactory.createMusicMessage(fromUserName, toUserName, music);
}
}
}
}
/**
* 歌曲点播使用指南
*
* @return
*/
public static String getMusicUsage() {
StringBuffer buffer = new StringBuffer();
buffer.append("歌曲点播操作指南").append("\n\n");
buffer.append("回复:歌曲@歌名").append("\n");
buffer.append("例如:歌曲@存在").append("\n");
buffer.append("或者:歌曲@存在@汪峰").append("\n\n");
buffer.append("回复“?/?”显示主菜单");
return buffer.toString();
}
/**
* 百度音乐搜索API 操作类
*XML解析
* @author 熊诗言
* @date 2015-09-05
*/
public class BaiduMusicService {
/**
* 根据名称和作者搜索音乐
*
* @param musicTitle 音乐名称
* @param musicAuthor 音乐作者
* @return Music
*/
public static Music searchMusic(String musicTitle, String musicAuthor) {
// 百度音乐搜索地址
String requestUrl = "http://box.zhangmen.baidu.com/x?op=12&count=1&title={title}$${author}$$$$";
// 对音乐名称、作者进URL 编码
requestUrl = requestUrl.replace("{title}", ContentUtil.urlEncodeUTF8(musicTitle));
requestUrl = requestUrl.replace("{author}", ContentUtil.urlEncodeUTF8(musicAuthor));
// 处理名称、作者中间的空格
requestUrl = requestUrl.replaceAll("\\+", "%20");
// 查询并获取返回结果
InputStream inputStream = httpRequest(requestUrl);
// 从返回结果中解析出Music
Music music = parseMusic(inputStream);
// 如果music 不为null,设置标题和描述
if (null != music) {
music.setTitle(musicTitle);
// 如果作者不为"",将描述设置为作者
if (!"".equals(musicAuthor))
music.setDescription(musicAuthor);
else
music.setDescription("来自百度音乐");
}
return music;
}
/**
* 发送http 请求取得返回的输入流
*
* @param requestUrl 请求地址
* @return InputStream
*/
private static InputStream httpRequest(String requestUrl) {
InputStream inputStream = null;
try {
URL url = new URL(requestUrl);
HttpURLConnection httpUrlConn = (HttpURLConnection) url.openConnection();
httpUrlConn.setDoInput(true);
httpUrlConn.setRequestMethod("GET");
httpUrlConn.connect();
// 获得返回的输入流
inputStream = httpUrlConn.getInputStream();
} catch (Exception e) {
e.printStackTrace();
}
return inputStream;
}
/**
* 解析音乐参数
*
* @param inputStream 百度音乐搜索API 返回的输入流
* @return Music
*/
@SuppressWarnings("unchecked")
private static Music parseMusic(InputStream inputStream) {
Music music = null;
try {
// 使用dom4j 解析xml 字符串
SAXReader reader = new SAXReader();
Document document = reader.read(inputStream);
// 得到xml 根元素
Element root = document.getRootElement();
// count 表示搜索到的歌曲数
String count = root.element("count").getText();
// 当搜索到的歌曲数大于0 时
if (!"0".equals(count)) {
// 普通品质
List<Element> urlList = root.elements("url");
// 高品质
List<Element> durlList = root.elements("durl");
// 普通品质的encode、decode
String urlEncode = urlList.get(0).element("encode").getText();
String urlDecode = urlList.get(0).element("decode").getText();
// 普通品质音乐的URL
String url = urlEncode.substring(0, urlEncode.lastIndexOf("/") + 1) + urlDecode;
if (-1 != urlDecode.lastIndexOf("&"))
url = urlEncode.substring(0, urlEncode.lastIndexOf("/")+ 1) + urlDecode.substring(0, urlDecode.lastIndexOf("&"));
// 默认情况下,高音质音乐的URL 等于普通品质音乐的URL
String durl = url;
// 判断高品质节点是否存在
Element durlElement = durlList.get(0).element("encode");
if (null != durlElement) {
// 高品质的encode、decode
String durlEncode = durlList.get(0).element("encode").getText();
String durlDecode = durlList.get(0).element("decode").getText();
// 高品质音乐的URL
durl = durlEncode.substring(0, durlEncode.lastIndexOf("/") + 1) + durlDecode;
if (-1 != durlDecode.lastIndexOf("&"))
durl = durlEncode.substring(0, durlEncode.lastIndexOf("/") + 1) + durlDecode.substring(0, durlDecode.lastIndexOf("&"));
}
music = new Music();
// 设置普通品质音乐链接
music.setMusicUrl(url);
// 设置高品质音乐链接
music.setHQMusicUrl(durl);
}
} catch (Exception e) {
e.printStackTrace();
}
return music;
}
// 测试方法
public static void main(String[] args) {
Music music = searchMusic("相信自己", "零点乐队");
System.out.println("音乐名称:" + music.getTitle());
System.out.println("音乐描述:" + music.getDescription());
System.out.println("普通品质链接:" + music.getMusicUrl());
System.out.println("高品质链接:" + music.getHQMusicUrl());
}
}