|
|
@ -6,10 +6,12 @@ import org.apache.commons.io.FileUtils; |
|
|
|
import org.jsoup.Connection; |
|
|
|
import org.jsoup.Connection; |
|
|
|
import org.jsoup.Jsoup; |
|
|
|
import org.jsoup.Jsoup; |
|
|
|
import org.jsoup.nodes.Document; |
|
|
|
import org.jsoup.nodes.Document; |
|
|
|
|
|
|
|
import org.jsoup.select.Elements; |
|
|
|
import org.pqh.core.util.LogManger; |
|
|
|
import org.pqh.core.util.LogManger; |
|
|
|
import org.pqh.gif.gifmaker.AnimatedGifEncoder; |
|
|
|
import org.pqh.gif.gifmaker.AnimatedGifEncoder; |
|
|
|
|
|
|
|
|
|
|
|
import javax.imageio.ImageIO; |
|
|
|
import javax.imageio.ImageIO; |
|
|
|
|
|
|
|
import javax.net.ssl.HttpsURLConnection; |
|
|
|
import java.awt.*; |
|
|
|
import java.awt.*; |
|
|
|
import java.awt.image.BufferedImage; |
|
|
|
import java.awt.image.BufferedImage; |
|
|
|
import java.io.*; |
|
|
|
import java.io.*; |
|
|
@ -26,17 +28,35 @@ import static org.pqh.gif.PixivUtil.Image.small; |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public class PixivUtil implements LogManger{ |
|
|
|
public class PixivUtil implements LogManger{ |
|
|
|
private static final String host="http://i4.pixiv.net"; |
|
|
|
private static final String host="http://i4.pixiv.net"; |
|
|
|
|
|
|
|
private static final String baseUrl="https://www.pixiv.net/member_illust.php?mode=medium&illust_id="; |
|
|
|
private static final int buffer=10240; |
|
|
|
private static final int buffer=10240; |
|
|
|
|
|
|
|
|
|
|
|
public static void downLoadImg(String imageUrl){ |
|
|
|
private static String phpsessid; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* 合成动图 |
|
|
|
|
|
|
|
* @param illust_id |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
public static void downLoadImg(int illust_id,String phpsessid){ |
|
|
|
|
|
|
|
|
|
|
|
InputStream is = null; |
|
|
|
InputStream is = null; |
|
|
|
FileOutputStream fos = null; |
|
|
|
FileOutputStream fos = null; |
|
|
|
BufferedOutputStream bos = null; |
|
|
|
BufferedOutputStream bos = null; |
|
|
|
ZipFile zipFile = null; |
|
|
|
ZipFile zipFile = null; |
|
|
|
|
|
|
|
String imageUrl=baseUrl+illust_id; |
|
|
|
try { |
|
|
|
try { |
|
|
|
log.info("图片链接:"+imageUrl); |
|
|
|
log.info("图片链接:"+imageUrl); |
|
|
|
Document document=Jsoup.connect(imageUrl).header("Cookie","PHPSESSID=11866657_32c3f92f0e2bf8b1607dabc04eecf787;").get(); |
|
|
|
Connection.Response response=Jsoup.connect(imageUrl).cookie("PHPSESSID",phpsessid).execute(); |
|
|
|
|
|
|
|
if(response.statusCode()== HttpsURLConnection.HTTP_OK) { |
|
|
|
|
|
|
|
Document document=response.parse(); |
|
|
|
|
|
|
|
Elements imgEle=document.select("img.original-image"); |
|
|
|
|
|
|
|
if(imgEle.size()==1){ |
|
|
|
|
|
|
|
String src=imgEle.first().attr("data-src"); |
|
|
|
|
|
|
|
downLoadImg(src); |
|
|
|
|
|
|
|
return; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
String script = document.select("#wrapper>script").html(); |
|
|
|
String script = document.select("#wrapper>script").html(); |
|
|
|
int start = script.indexOf(small.keyword); |
|
|
|
int start = script.indexOf(small.keyword); |
|
|
|
int end = script.indexOf(big.keyword); |
|
|
|
int end = script.indexOf(big.keyword); |
|
|
@ -46,14 +66,8 @@ public class PixivUtil implements LogManger{ |
|
|
|
JsonNode node = objectMapper.readTree(json); |
|
|
|
JsonNode node = objectMapper.readTree(json); |
|
|
|
String src = node.get("src").asText().replace(small.size, big.size); |
|
|
|
String src = node.get("src").asText().replace(small.size, big.size); |
|
|
|
log.info("获取到图包地址:" + src); |
|
|
|
log.info("获取到图包地址:" + src); |
|
|
|
Connection connection= Jsoup.connect(src).header("referer",host).ignoreContentType(true); |
|
|
|
|
|
|
|
connection.request().maxBodySize(1024*1024*1024); |
|
|
|
File file=downLoadImg(src); |
|
|
|
Connection.Response response=connection.execute(); |
|
|
|
|
|
|
|
log.info("图包体积:"+response.header("content-length")); |
|
|
|
|
|
|
|
String zipName=src.substring(src.lastIndexOf("/")+1); |
|
|
|
|
|
|
|
File file=new File("tmp/images/"+zipName); |
|
|
|
|
|
|
|
FileUtils.writeByteArrayToFile(file,response.bodyAsBytes()); |
|
|
|
|
|
|
|
log.info("图包下载到:"+file.getAbsolutePath()); |
|
|
|
|
|
|
|
zipFile = new ZipFile(file); |
|
|
|
zipFile = new ZipFile(file); |
|
|
|
Enumeration entrys = zipFile.entries(); |
|
|
|
Enumeration entrys = zipFile.entries(); |
|
|
|
|
|
|
|
|
|
|
@ -88,6 +102,11 @@ public class PixivUtil implements LogManger{ |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
log.error("图包地址获取失败"); |
|
|
|
log.error("图包地址获取失败"); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
}else if(response.statusCode()==HttpsURLConnection.HTTP_NOT_FOUND){ |
|
|
|
|
|
|
|
log.info("图片链接:"+imageUrl+"不存在"); |
|
|
|
|
|
|
|
}else{ |
|
|
|
|
|
|
|
log.error("图片链接:"+imageUrl+"访问异常"); |
|
|
|
|
|
|
|
} |
|
|
|
} catch (IOException e) { |
|
|
|
} catch (IOException e) { |
|
|
|
e.printStackTrace(); |
|
|
|
e.printStackTrace(); |
|
|
|
}finally { |
|
|
|
}finally { |
|
|
@ -111,6 +130,18 @@ public class PixivUtil implements LogManger{ |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static File downLoadImg(String src) throws IOException { |
|
|
|
|
|
|
|
Connection connection = Jsoup.connect(src).header("referer", host).ignoreContentType(true); |
|
|
|
|
|
|
|
connection.request().maxBodySize(1024 * 1024 * 1024); |
|
|
|
|
|
|
|
Connection.Response response = connection.execute(); |
|
|
|
|
|
|
|
log.info("图片资源体积:" + response.header("content-length")); |
|
|
|
|
|
|
|
String zipName = src.substring(src.lastIndexOf("/") + 1); |
|
|
|
|
|
|
|
File file = new File("tmp/images/" + zipName); |
|
|
|
|
|
|
|
FileUtils.writeByteArrayToFile(file, response.bodyAsBytes()); |
|
|
|
|
|
|
|
log.info("图片资源下载到:" + file.getAbsolutePath()); |
|
|
|
|
|
|
|
return file; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* jpg合成gif |
|
|
|
* jpg合成gif |
|
|
|
* @param gifName gif文件名 |
|
|
|
* @param gifName gif文件名 |
|
|
@ -118,6 +149,7 @@ public class PixivUtil implements LogManger{ |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public static void jpgToGif(String gifName,String jpgPath,JsonNode node){ |
|
|
|
public static void jpgToGif(String gifName,String jpgPath,JsonNode node){ |
|
|
|
AnimatedGifEncoder animatedGifEncoder=new AnimatedGifEncoder(); |
|
|
|
AnimatedGifEncoder animatedGifEncoder=new AnimatedGifEncoder(); |
|
|
|
|
|
|
|
|
|
|
|
OutputStream outputStream= null; |
|
|
|
OutputStream outputStream= null; |
|
|
|
InputStream inputStream=null; |
|
|
|
InputStream inputStream=null; |
|
|
|
File file=new File(jpgPath); |
|
|
|
File file=new File(jpgPath); |
|
|
@ -142,6 +174,7 @@ public class PixivUtil implements LogManger{ |
|
|
|
//把帧添加进去合成
|
|
|
|
//把帧添加进去合成
|
|
|
|
animatedGifEncoder.addFrame(image); |
|
|
|
animatedGifEncoder.addFrame(image); |
|
|
|
animatedGifEncoder.setDelay(delay); |
|
|
|
animatedGifEncoder.setDelay(delay); |
|
|
|
|
|
|
|
|
|
|
|
log.info(gifName+":正在合成第"+((index+1)+"帧")); |
|
|
|
log.info(gifName+":正在合成第"+((index+1)+"帧")); |
|
|
|
} |
|
|
|
} |
|
|
|
//添加完所有帧开始合成
|
|
|
|
//添加完所有帧开始合成
|
|
|
|