package com.yutou.nas.utils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yutou.nas.Services.IBangumiService;
import com.yutou.nas.bangumi.AnimationData;
import com.yutou.nas.interfaces.DownloadInterface;
import com.yutou.nas.mybatis.model.BangumiItem;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;

public class BTDownloadManager implements ApplicationContextAware {
    private static String tmpJson = "{\"status\":\"ok\",\"feed\":{\"url\":\"http://share.dmhy.org/topics/rss/page/1/rss.xml?keyword=无职转生&order=date-desc&sort_id=2&team_id=604\",\"title\":\"c.c动漫-動畫-動漫花園資源網\",\"link\":\"http://share.dmhy.org/\",\"author\":\"\",\"description\":\"動漫花園資訊網是一個動漫愛好者交流的平台,提供最及時,最全面的動畫,漫畫,動漫音樂,動漫下載,BT,ED,動漫遊戲,資訊,分享,交流,讨论.\",\"image\":\"\"},\"items\":[{\"title\":\"\u200B[c.c動漫][1月新番][無職轉生～到了異世界就拿出真本事～][11][BIG5][1080P][MP4]\",\"pubDate\":\"2021-03-25 07:11:12\",\"link\":\"http://share.dmhy.org/topics/view/563676_c_c_1_11_BIG5_1080P_MP4.html\",\"guid\":\"http://share.dmhy.org/topics/view/563676_c_c_1_11_BIG5_1080P_MP4.html\",\"author\":\"lleeopen\",\"thumbnail\":\"https://img.vim-cn.com/43/a8c19df5571ea68c64ed4945cc0187bd82e2f1.jpg\",\"description\":\"\\n<p>P.s.請使用正規的BT軟件下載(utorrent、qbittorrent、bitcomet)，下載完成之後不要立即關閉下載軟件，盡量保持上傳一段時間~~</p>\\n<p><img src=\\\"https://img.vim-cn.com/43/a8c19df5571ea68c64ed4945cc0187bd82e2f1.jpg\\\"></p>\\n\",\"content\":\"\\n<p>P.s.請使用正規的BT軟件下載(utorrent、qbittorrent、bitcomet)，下載完成之後不要立即關閉下載軟件，盡量保持上傳一段時間~~</p>\\n<p><img src=\\\"https://img.vim-cn.com/43/a8c19df5571ea68c64ed4945cc0187bd82e2f1.jpg\\\"></p>\\n\",\"enclosure\":{\"link\":\"magnet:?xt=urn:btih:LVYN5MDZTBFRNNSPYYXKOKJA5PNGB4FD&amp;dn=&amp;tr=http://104.238.198.186:8000/announce&amp;tr=udp://104.238.198.186:8000/announce&amp;tr=http://tracker.openbittorrent.com:80/announce&amp;tr=udp://tracker3.itzmx.com:6961/announce&amp;tr=http://tracker4.itzmx.com:2710/announce&amp;tr=http://tracker.publicbt.com:80/announce&amp;tr=http://tracker.prq.to/announce&amp;tr=http://open.acgtracker.com:1096/announce&amp;tr=https://t-115.rhcloud.com/only_for_ylbud&amp;tr=http://tracker1.itzmx.com:8080/announce&amp;tr=http://tracker2.itzmx.com:6961/announce&amp;tr=udp://tracker1.itzmx.com:8080/announce&amp;tr=udp://tracker2.itzmx.com:6961/announce&amp;tr=udp://tracker3.itzmx.com:6961/announce&amp;tr=udp://tracker4.itzmx.com:2710/announce&amp;tr=http://tr.bangumi.moe:6969/announce&amp;tr=http://t.nyaatracker.com/announce&amp;tr=http://open.nyaatorrents.info:6544/announce&amp;tr=http://t2.popgo.org:7456/annonce&amp;tr=http://share.camoe.cn:8080/announce&amp;tr=http://opentracker.acgnx.se/announce&amp;tr=http://tracker.acgnx.se/announce&amp;tr=http://nyaa.tracker.wf:7777/announce&amp;tr=http://t.acg.rip:6699/announce&amp;tr=http://opentracker.acgnx.com:6869/announce\",\"type\":\"application/x-bittorrent\",\"length\":1},\"categories\":[\"動畫\"]},{\"title\":\"\u200B[c.c動漫][1月新番][無職轉生～到了異世界就拿出真本事～][10][BIG5][1080P][MP4]\",\"pubDate\":\"2021-03-15 05:12:42\",\"link\":\"http://share.dmhy.org/topics/view/562725_c_c_1_10_BIG5_1080P_MP4.html\",\"guid\":\"http://share.dmhy.org/topics/view/562725_c_c_1_10_BIG5_1080P_MP4.html\",\"author\":\"lleeopen\",\"thumbnail\":\"https://img.vim-cn.com/43/a8c19df5571ea68c64ed4945cc0187bd82e2f1.jpg\",\"description\":\"\\n<p>P.s.請使用正規的BT軟件下載(utorrent、qbittorrent、bitcomet)，下載完成之後不要立即關閉下載軟件，盡量保持上傳一段時間~~<br></p>\\n<p><img src=\\\"https://img.vim-cn.com/43/a8c19df5571ea68c64ed4945cc0187bd82e2f1.jpg\\\"><br></p>\\n\",\"content\":\"\\n<p>P.s.請使用正規的BT軟件下載(utorrent、qbittorrent、bitcomet)，下載完成之後不要立即關閉下載軟件，盡量保持上傳一段時間~~<br></p>\\n<p><img src=\\\"https://img.vim-cn.com/43/a8c19df5571ea68c64ed4945cc0187bd82e2f1.jpg\\\"><br></p>\\n\",\"enclosure\":{\"link\":\"magnet:?xt=urn:btih:QWEVVDWHSWISWGLOQJWD2PRHP3OYJBD3&amp;dn=&amp;tr=http://104.238.198.186:8000/announce&amp;tr=udp://104.238.198.186:8000/announce&amp;tr=http://tracker.openbittorrent.com:80/announce&amp;tr=udp://tracker3.itzmx.com:6961/announce&amp;tr=http://tracker4.itzmx.com:2710/announce&amp;tr=http://tracker.publicbt.com:80/announce&amp;tr=http://tracker.prq.to/announce&amp;tr=http://open.acgtracker.com:1096/announce&amp;tr=https://t-115.rhcloud.com/only_for_ylbud&amp;tr=http://tracker1.itzmx.com:8080/announce&amp;tr=http://tracker2.itzmx.com:6961/announce&amp;tr=udp://tracker1.itzmx.com:8080/announce&amp;tr=udp://tracker2.itzmx.com:6961/announce&amp;tr=udp://tracker3.itzmx.com:6961/announce&amp;tr=udp://tracker4.itzmx.com:2710/announce&amp;tr=http://tr.bangumi.moe:6969/announce&amp;tr=http://t.nyaatracker.com/announce&amp;tr=http://open.nyaatorrents.info:6544/announce&amp;tr=http://t2.popgo.org:7456/annonce&amp;tr=http://share.camoe.cn:8080/announce&amp;tr=http://opentracker.acgnx.se/announce&amp;tr=http://tracker.acgnx.se/announce&amp;tr=http://nyaa.tracker.wf:7777/announce&amp;tr=http://t.acg.rip:6699/announce&amp;tr=http://opentracker.acgnx.com:6869/announce\",\"type\":\"application/x-bittorrent\",\"length\":1},\"categories\":[\"動畫\"]},{\"title\":\"\u200B[c.c動漫][1月新番][無職轉生～到了異世界就拿出真本事～][09][BIG5][1080P][MP4]\",\"pubDate\":\"2021-03-14 12:56:28\",\"link\":\"http://share.dmhy.org/topics/view/562653_c_c_1_09_BIG5_1080P_MP4.html\",\"guid\":\"http://share.dmhy.org/topics/view/562653_c_c_1_09_BIG5_1080P_MP4.html\",\"author\":\"lleeopen\",\"thumbnail\":\"https://img.vim-cn.com/43/a8c19df5571ea68c64ed4945cc0187bd82e2f1.jpg\",\"description\":\"\\n<p>P.s.請使用正規的BT軟件下載(utorrent、qbittorrent、bitcomet)，下載完成之後不要立即關閉下載軟件，盡量保持上傳一段時間~~<br></p>\\n<p><img src=\\\"https://img.vim-cn.com/43/a8c19df5571ea68c64ed4945cc0187bd82e2f1.jpg\\\"><br></p>\\n\",\"content\":\"\\n<p>P.s.請使用正規的BT軟件下載(utorrent、qbittorrent、bitcomet)，下載完成之後不要立即關閉下載軟件，盡量保持上傳一段時間~~<br></p>\\n<p><img src=\\\"https://img.vim-cn.com/43/a8c19df5571ea68c64ed4945cc0187bd82e2f1.jpg\\\"><br></p>\\n\",\"enclosure\":{\"link\":\"magnet:?xt=urn:btih:Z6M3M7Y5YTIKY4GGCG7PGEP3WWGORS7T&amp;dn=&amp;tr=http://104.238.198.186:8000/announce&amp;tr=udp://104.238.198.186:8000/announce&amp;tr=http://tracker.openbittorrent.com:80/announce&amp;tr=udp://tracker3.itzmx.com:6961/announce&amp;tr=http://tracker4.itzmx.com:2710/announce&amp;tr=http://tracker.publicbt.com:80/announce&amp;tr=http://tracker.prq.to/announce&amp;tr=http://open.acgtracker.com:1096/announce&amp;tr=https://t-115.rhcloud.com/only_for_ylbud&amp;tr=http://tracker1.itzmx.com:8080/announce&amp;tr=http://tracker2.itzmx.com:6961/announce&amp;tr=udp://tracker1.itzmx.com:8080/announce&amp;tr=udp://tracker2.itzmx.com:6961/announce&amp;tr=udp://tracker3.itzmx.com:6961/announce&amp;tr=udp://tracker4.itzmx.com:2710/announce&amp;tr=http://tr.bangumi.moe:6969/announce&amp;tr=http://t.nyaatracker.com/announce&amp;tr=http://open.nyaatorrents.info:6544/announce&amp;tr=http://t2.popgo.org:7456/annonce&amp;tr=http://share.camoe.cn:8080/announce&amp;tr=http://opentracker.acgnx.se/announce&amp;tr=http://tracker.acgnx.se/announce&amp;tr=http://nyaa.tracker.wf:7777/announce&amp;tr=http://t.acg.rip:6699/announce&amp;tr=http://opentracker.acgnx.com:6869/announce\",\"type\":\"application/x-bittorrent\",\"length\":1},\"categories\":[\"動畫\"]},{\"title\":\"\u200B[c.c動漫][1月新番][無職轉生～到了異世界就拿出真本事～][08][BIG5][1080P][MP4]\",\"pubDate\":\"2021-03-14 12:56:00\",\"link\":\"http://share.dmhy.org/topics/view/562652_c_c_1_08_BIG5_1080P_MP4.html\",\"guid\":\"http://share.dmhy.org/topics/view/562652_c_c_1_08_BIG5_1080P_MP4.html\",\"author\":\"lleeopen\",\"thumbnail\":\"https://img.vim-cn.com/43/a8c19df5571ea68c64ed4945cc0187bd82e2f1.jpg\",\"description\":\"\\n<p>P.s.請使用正規的BT軟件下載(utorrent、qbittorrent、bitcomet)，下載完成之後不要立即關閉下載軟件，盡量保持上傳一段時間~~<br></p>\\n<p><img src=\\\"https://img.vim-cn.com/43/a8c19df5571ea68c64ed4945cc0187bd82e2f1.jpg\\\"><br></p>\\n\",\"content\":\"\\n<p>P.s.請使用正規的BT軟件下載(utorrent、qbittorrent、bitcomet)，下載完成之後不要立即關閉下載軟件，盡量保持上傳一段時間~~<br></p>\\n<p><img src=\\\"https://img.vim-cn.com/43/a8c19df5571ea68c64ed4945cc0187bd82e2f1.jpg\\\"><br></p>\\n\",\"enclosure\":{\"link\":\"magnet:?xt=urn:btih:BMXA6AZF7BCYQ2CIZOS5I6XPTXUZ2W3E&amp;dn=&amp;tr=http://104.238.198.186:8000/announce&amp;tr=udp://104.238.198.186:8000/announce&amp;tr=http://tracker.openbittorrent.com:80/announce&amp;tr=udp://tracker3.itzmx.com:6961/announce&amp;tr=http://tracker4.itzmx.com:2710/announce&amp;tr=http://tracker.publicbt.com:80/announce&amp;tr=http://tracker.prq.to/announce&amp;tr=http://open.acgtracker.com:1096/announce&amp;tr=https://t-115.rhcloud.com/only_for_ylbud&amp;tr=http://tracker1.itzmx.com:8080/announce&amp;tr=http://tracker2.itzmx.com:6961/announce&amp;tr=udp://tracker1.itzmx.com:8080/announce&amp;tr=udp://tracker2.itzmx.com:6961/announce&amp;tr=udp://tracker3.itzmx.com:6961/announce&amp;tr=udp://tracker4.itzmx.com:2710/announce&amp;tr=http://tr.bangumi.moe:6969/announce&amp;tr=http://t.nyaatracker.com/announce&amp;tr=http://open.nyaatorrents.info:6544/announce&amp;tr=http://t2.popgo.org:7456/annonce&amp;tr=http://share.camoe.cn:8080/announce&amp;tr=http://opentracker.acgnx.se/announce&amp;tr=http://tracker.acgnx.se/announce&amp;tr=http://nyaa.tracker.wf:7777/announce&amp;tr=http://t.acg.rip:6699/announce&amp;tr=http://opentracker.acgnx.com:6869/announce\",\"type\":\"application/x-bittorrent\",\"length\":1},\"categories\":[\"動畫\"]},{\"title\":\"\u200B[c.c動漫][1月新番][無職轉生～到了異世界就拿出真本事～][07][BIG5][1080P][MP4]\",\"pubDate\":\"2021-02-23 09:41:47\",\"link\":\"http://share.dmhy.org/topics/view/560768_c_c_1_07_BIG5_1080P_MP4.html\",\"guid\":\"http://share.dmhy.org/topics/view/560768_c_c_1_07_BIG5_1080P_MP4.html\",\"author\":\"lleeopen\",\"thumbnail\":\"https://img.vim-cn.com/43/a8c19df5571ea68c64ed4945cc0187bd82e2f1.jpg\",\"description\":\"\\n<p>P.s.請使用正規的BT軟件下載(utorrent、qbittorrent、bitcomet)，下載完成之後不要立即關閉下載軟件，盡量保持上傳一段時間~~<br></p>\\n<p><img src=\\\"https://img.vim-cn.com/43/a8c19df5571ea68c64ed4945cc0187bd82e2f1.jpg\\\"><br></p>\\n\",\"content\":\"\\n<p>P.s.請使用正規的BT軟件下載(utorrent、qbittorrent、bitcomet)，下載完成之後不要立即關閉下載軟件，盡量保持上傳一段時間~~<br></p>\\n<p><img src=\\\"https://img.vim-cn.com/43/a8c19df5571ea68c64ed4945cc0187bd82e2f1.jpg\\\"><br></p>\\n\",\"enclosure\":{\"link\":\"magnet:?xt=urn:btih:DDIM47E2UJAXRV54JJQFFPJ7SLYZZYQJ&amp;dn=&amp;tr=http://104.238.198.186:8000/announce&amp;tr=udp://104.238.198.186:8000/announce&amp;tr=http://tracker.openbittorrent.com:80/announce&amp;tr=udp://tracker3.itzmx.com:6961/announce&amp;tr=http://tracker4.itzmx.com:2710/announce&amp;tr=http://tracker.publicbt.com:80/announce&amp;tr=http://tracker.prq.to/announce&amp;tr=http://open.acgtracker.com:1096/announce&amp;tr=https://t-115.rhcloud.com/only_for_ylbud&amp;tr=http://tracker1.itzmx.com:8080/announce&amp;tr=http://tracker2.itzmx.com:6961/announce&amp;tr=udp://tracker1.itzmx.com:8080/announce&amp;tr=udp://tracker2.itzmx.com:6961/announce&amp;tr=udp://tracker3.itzmx.com:6961/announce&amp;tr=udp://tracker4.itzmx.com:2710/announce&amp;tr=http://tr.bangumi.moe:6969/announce&amp;tr=http://t.nyaatracker.com/announce&amp;tr=http://open.nyaatorrents.info:6544/announce&amp;tr=http://t2.popgo.org:7456/annonce&amp;tr=http://share.camoe.cn:8080/announce&amp;tr=http://opentracker.acgnx.se/announce&amp;tr=http://tracker.acgnx.se/announce&amp;tr=http://nyaa.tracker.wf:7777/announce&amp;tr=http://t.acg.rip:6699/announce&amp;tr=http://opentracker.acgnx.com:6869/announce\",\"type\":\"application/x-bittorrent\",\"length\":1},\"categories\":[\"動畫\"]},{\"title\":\"\u200B[c.c動漫][1月新番][無職轉生～到了異世界就拿出真本事～][06][BIG5][1080P][MP4]\",\"pubDate\":\"2021-02-14 16:51:36\",\"link\":\"http://share.dmhy.org/topics/view/559914_c_c_1_06_BIG5_1080P_MP4.html\",\"guid\":\"http://share.dmhy.org/topics/view/559914_c_c_1_06_BIG5_1080P_MP4.html\",\"author\":\"lleeopen\",\"thumbnail\":\"https://img.vim-cn.com/43/a8c19df5571ea68c64ed4945cc0187bd82e2f1.jpg\",\"description\":\"\\n<p>P.s.請使用正規的BT軟件下載(utorrent、qbittorrent、bitcomet)，下載完成之後不要立即關閉下載軟件，盡量保持上傳一段時間~~<br></p>\\n<p><img src=\\\"https://img.vim-cn.com/43/a8c19df5571ea68c64ed4945cc0187bd82e2f1.jpg\\\"><br></p>\\n\",\"content\":\"\\n<p>P.s.請使用正規的BT軟件下載(utorrent、qbittorrent、bitcomet)，下載完成之後不要立即關閉下載軟件，盡量保持上傳一段時間~~<br></p>\\n<p><img src=\\\"https://img.vim-cn.com/43/a8c19df5571ea68c64ed4945cc0187bd82e2f1.jpg\\\"><br></p>\\n\",\"enclosure\":{\"link\":\"magnet:?xt=urn:btih:MFQR2G2AEZLHXONGKII47FXP2DPE4C3G&amp;dn=&amp;tr=http://104.238.198.186:8000/announce&amp;tr=udp://104.238.198.186:8000/announce&amp;tr=http://tracker.openbittorrent.com:80/announce&amp;tr=udp://tracker3.itzmx.com:6961/announce&amp;tr=http://tracker4.itzmx.com:2710/announce&amp;tr=http://tracker.publicbt.com:80/announce&amp;tr=http://tracker.prq.to/announce&amp;tr=http://open.acgtracker.com:1096/announce&amp;tr=https://t-115.rhcloud.com/only_for_ylbud&amp;tr=http://tracker1.itzmx.com:8080/announce&amp;tr=http://tracker2.itzmx.com:6961/announce&amp;tr=udp://tracker1.itzmx.com:8080/announce&amp;tr=udp://tracker2.itzmx.com:6961/announce&amp;tr=udp://tracker3.itzmx.com:6961/announce&amp;tr=udp://tracker4.itzmx.com:2710/announce&amp;tr=http://tr.bangumi.moe:6969/announce&amp;tr=http://t.nyaatracker.com/announce&amp;tr=http://open.nyaatorrents.info:6544/announce&amp;tr=http://t2.popgo.org:7456/annonce&amp;tr=http://share.camoe.cn:8080/announce&amp;tr=http://opentracker.acgnx.se/announce&amp;tr=http://tracker.acgnx.se/announce&amp;tr=http://nyaa.tracker.wf:7777/announce&amp;tr=http://t.acg.rip:6699/announce&amp;tr=http://opentracker.acgnx.com:6869/announce\",\"type\":\"application/x-bittorrent\",\"length\":1},\"categories\":[\"動畫\"]},{\"title\":\"\u200B[c.c動漫][1月新番][無職轉生～到了異世界就拿出真本事～][05][BIG5][1080P][MP4]\",\"pubDate\":\"2021-02-07 16:33:23\",\"link\":\"http://share.dmhy.org/topics/view/559282_c_c_1_05_BIG5_1080P_MP4.html\",\"guid\":\"http://share.dmhy.org/topics/view/559282_c_c_1_05_BIG5_1080P_MP4.html\",\"author\":\"lleeopen\",\"thumbnail\":\"https://img.vim-cn.com/43/a8c19df5571ea68c64ed4945cc0187bd82e2f1.jpg\",\"description\":\"\\n<p>P.s.請使用正規的BT軟件下載(utorrent、qbittorrent、bitcomet)，下載完成之後不要立即關閉下載軟件，盡量保持上傳一段時間~~<br></p>\\n<p><img src=\\\"https://img.vim-cn.com/43/a8c19df5571ea68c64ed4945cc0187bd82e2f1.jpg\\\"><br></p>\\n\",\"content\":\"\\n<p>P.s.請使用正規的BT軟件下載(utorrent、qbittorrent、bitcomet)，下載完成之後不要立即關閉下載軟件，盡量保持上傳一段時間~~<br></p>\\n<p><img src=\\\"https://img.vim-cn.com/43/a8c19df5571ea68c64ed4945cc0187bd82e2f1.jpg\\\"><br></p>\\n\",\"enclosure\":{\"link\":\"magnet:?xt=urn:btih:TUDQWT6OAHYS2LFTCURHRLNZS6PVZXX5&amp;dn=&amp;tr=http://104.238.198.186:8000/announce&amp;tr=udp://104.238.198.186:8000/announce&amp;tr=http://tracker.openbittorrent.com:80/announce&amp;tr=udp://tracker3.itzmx.com:6961/announce&amp;tr=http://tracker4.itzmx.com:2710/announce&amp;tr=http://tracker.publicbt.com:80/announce&amp;tr=http://tracker.prq.to/announce&amp;tr=http://open.acgtracker.com:1096/announce&amp;tr=https://t-115.rhcloud.com/only_for_ylbud&amp;tr=http://tracker1.itzmx.com:8080/announce&amp;tr=http://tracker2.itzmx.com:6961/announce&amp;tr=udp://tracker1.itzmx.com:8080/announce&amp;tr=udp://tracker2.itzmx.com:6961/announce&amp;tr=udp://tracker3.itzmx.com:6961/announce&amp;tr=udp://tracker4.itzmx.com:2710/announce&amp;tr=http://tr.bangumi.moe:6969/announce&amp;tr=http://t.nyaatracker.com/announce&amp;tr=http://open.nyaatorrents.info:6544/announce&amp;tr=http://t2.popgo.org:7456/annonce&amp;tr=http://share.camoe.cn:8080/announce&amp;tr=http://opentracker.acgnx.se/announce&amp;tr=http://tracker.acgnx.se/announce&amp;tr=http://nyaa.tracker.wf:7777/announce&amp;tr=http://t.acg.rip:6699/announce&amp;tr=http://opentracker.acgnx.com:6869/announce\",\"type\":\"application/x-bittorrent\",\"length\":1},\"categories\":[\"動畫\"]},{\"title\":\"\u200B[c.c動漫][1月新番][無職轉生～到了異世界就拿出真本事～][04][BIG5][1080P][MP4]\",\"pubDate\":\"2021-01-31 16:36:34\",\"link\":\"http://share.dmhy.org/topics/view/558624_c_c_1_04_BIG5_1080P_MP4.html\",\"guid\":\"http://share.dmhy.org/topics/view/558624_c_c_1_04_BIG5_1080P_MP4.html\",\"author\":\"lleeopen\",\"thumbnail\":\"https://img.vim-cn.com/43/a8c19df5571ea68c64ed4945cc0187bd82e2f1.jpg\",\"description\":\"\\n<p>P.s.請使用正規的BT軟件下載(utorrent、qbittorrent、bitcomet)，下載完成之後不要立即關閉下載軟件，盡量保持上傳一段時間~~<br></p>\\n<p><img src=\\\"https://img.vim-cn.com/43/a8c19df5571ea68c64ed4945cc0187bd82e2f1.jpg\\\"><br></p>\\n\",\"content\":\"\\n<p>P.s.請使用正規的BT軟件下載(utorrent、qbittorrent、bitcomet)，下載完成之後不要立即關閉下載軟件，盡量保持上傳一段時間~~<br></p>\\n<p><img src=\\\"https://img.vim-cn.com/43/a8c19df5571ea68c64ed4945cc0187bd82e2f1.jpg\\\"><br></p>\\n\",\"enclosure\":{\"link\":\"magnet:?xt=urn:btih:MVJ3F3DGW5Y4Z7LONWEWN6GBSQAC3TKQ&amp;dn=&amp;tr=http://104.238.198.186:8000/announce&amp;tr=udp://104.238.198.186:8000/announce&amp;tr=http://tracker.openbittorrent.com:80/announce&amp;tr=udp://tracker3.itzmx.com:6961/announce&amp;tr=http://tracker4.itzmx.com:2710/announce&amp;tr=http://tracker.publicbt.com:80/announce&amp;tr=http://tracker.prq.to/announce&amp;tr=http://open.acgtracker.com:1096/announce&amp;tr=https://t-115.rhcloud.com/only_for_ylbud&amp;tr=http://tracker1.itzmx.com:8080/announce&amp;tr=http://tracker2.itzmx.com:6961/announce&amp;tr=udp://tracker1.itzmx.com:8080/announce&amp;tr=udp://tracker2.itzmx.com:6961/announce&amp;tr=udp://tracker3.itzmx.com:6961/announce&amp;tr=udp://tracker4.itzmx.com:2710/announce&amp;tr=http://tr.bangumi.moe:6969/announce&amp;tr=http://t.nyaatracker.com/announce&amp;tr=http://open.nyaatorrents.info:6544/announce&amp;tr=http://t2.popgo.org:7456/annonce&amp;tr=http://share.camoe.cn:8080/announce&amp;tr=http://opentracker.acgnx.se/announce&amp;tr=http://tracker.acgnx.se/announce&amp;tr=http://nyaa.tracker.wf:7777/announce&amp;tr=http://t.acg.rip:6699/announce&amp;tr=http://opentracker.acgnx.com:6869/announce\",\"type\":\"application/x-bittorrent\",\"length\":1},\"categories\":[\"動畫\"]},{\"title\":\"\u200B[c.c動漫][1月新番][無職轉生～到了異世界就拿出真本事～][03][BIG5][1080P][MP4]\",\"pubDate\":\"2021-01-25 05:27:50\",\"link\":\"http://share.dmhy.org/topics/view/558007_c_c_1_03_BIG5_1080P_MP4.html\",\"guid\":\"http://share.dmhy.org/topics/view/558007_c_c_1_03_BIG5_1080P_MP4.html\",\"author\":\"lleeopen\",\"thumbnail\":\"https://img.vim-cn.com/43/a8c19df5571ea68c64ed4945cc0187bd82e2f1.jpg\",\"description\":\"\\n<p>[c.c動漫][1月新番][無職轉生～到了異世界就拿出真本事～][03][BIG5][1080P][MP4]</p>\\n<p>P.s.請使用正規的BT軟件下載(utorrent、qbittorrent、bitcomet)，下載完成之後不要立即關閉下載軟件，盡量保持上傳一段時間~~</p>\\n<p><img src=\\\"https://img.vim-cn.com/43/a8c19df5571ea68c64ed4945cc0187bd82e2f1.jpg\\\"><br></p>\\n\",\"content\":\"\\n<p>[c.c動漫][1月新番][無職轉生～到了異世界就拿出真本事～][03][BIG5][1080P][MP4]</p>\\n<p>P.s.請使用正規的BT軟件下載(utorrent、qbittorrent、bitcomet)，下載完成之後不要立即關閉下載軟件，盡量保持上傳一段時間~~</p>\\n<p><img src=\\\"https://img.vim-cn.com/43/a8c19df5571ea68c64ed4945cc0187bd82e2f1.jpg\\\"><br></p>\\n\",\"enclosure\":{\"link\":\"magnet:?xt=urn:btih:OR6WDFDYDAPOGFR3FY3LLCF25P3PHUTG&amp;dn=&amp;tr=http://104.238.198.186:8000/announce&amp;tr=udp://104.238.198.186:8000/announce&amp;tr=http://tracker.openbittorrent.com:80/announce&amp;tr=udp://tracker3.itzmx.com:6961/announce&amp;tr=http://tracker4.itzmx.com:2710/announce&amp;tr=http://tracker.publicbt.com:80/announce&amp;tr=http://tracker.prq.to/announce&amp;tr=http://open.acgtracker.com:1096/announce&amp;tr=https://t-115.rhcloud.com/only_for_ylbud&amp;tr=http://tracker1.itzmx.com:8080/announce&amp;tr=http://tracker2.itzmx.com:6961/announce&amp;tr=udp://tracker1.itzmx.com:8080/announce&amp;tr=udp://tracker2.itzmx.com:6961/announce&amp;tr=udp://tracker3.itzmx.com:6961/announce&amp;tr=udp://tracker4.itzmx.com:2710/announce&amp;tr=http://tr.bangumi.moe:6969/announce&amp;tr=http://t.nyaatracker.com/announce&amp;tr=http://open.nyaatorrents.info:6544/announce&amp;tr=http://t2.popgo.org:7456/annonce&amp;tr=http://share.camoe.cn:8080/announce&amp;tr=http://opentracker.acgnx.se/announce&amp;tr=http://tracker.acgnx.se/announce&amp;tr=http://nyaa.tracker.wf:7777/announce&amp;tr=http://t.acg.rip:6699/announce&amp;tr=http://opentracker.acgnx.com:6869/announce\",\"type\":\"application/x-bittorrent\",\"length\":1},\"categories\":[\"動畫\"]},{\"title\":\"\u200B[c.c動漫][1月新番][無職轉生～到了異世界就拿出真本事～][02][BIG5][1080P][MP4]\",\"pubDate\":\"2021-01-17 16:35:55\",\"link\":\"http://share.dmhy.org/topics/view/557306_c_c_1_02_BIG5_1080P_MP4.html\",\"guid\":\"http://share.dmhy.org/topics/view/557306_c_c_1_02_BIG5_1080P_MP4.html\",\"author\":\"lleeopen\",\"thumbnail\":\"https://img.vim-cn.com/43/a8c19df5571ea68c64ed4945cc0187bd82e2f1.jpg\",\"description\":\"\\n<p>[c.c動漫][1月新番][無職轉生～到了異世界就拿出真本事～][02][BIG5][1080P][MP4]</p>\\n<p>P.s.請使用正規的BT軟件下載(utorrent、qbittorrent、bitcomet)，下載完成之後不要立即關閉下載軟件，盡量保持上傳一段時間~~</p>\\n<p><img src=\\\"https://img.vim-cn.com/43/a8c19df5571ea68c64ed4945cc0187bd82e2f1.jpg\\\"><br></p>\\n\",\"content\":\"\\n<p>[c.c動漫][1月新番][無職轉生～到了異世界就拿出真本事～][02][BIG5][1080P][MP4]</p>\\n<p>P.s.請使用正規的BT軟件下載(utorrent、qbittorrent、bitcomet)，下載完成之後不要立即關閉下載軟件，盡量保持上傳一段時間~~</p>\\n<p><img src=\\\"https://img.vim-cn.com/43/a8c19df5571ea68c64ed4945cc0187bd82e2f1.jpg\\\"><br></p>\\n\",\"enclosure\":{\"link\":\"magnet:?xt=urn:btih:TGON2SU75VGPYQBOHMCXF7M6MNSYS7J3&amp;dn=&amp;tr=http://104.238.198.186:8000/announce&amp;tr=udp://104.238.198.186:8000/announce&amp;tr=http://tracker.openbittorrent.com:80/announce&amp;tr=udp://tracker3.itzmx.com:6961/announce&amp;tr=http://tracker4.itzmx.com:2710/announce&amp;tr=http://tracker.publicbt.com:80/announce&amp;tr=http://tracker.prq.to/announce&amp;tr=http://open.acgtracker.com:1096/announce&amp;tr=https://t-115.rhcloud.com/only_for_ylbud&amp;tr=http://tracker1.itzmx.com:8080/announce&amp;tr=http://tracker2.itzmx.com:6961/announce&amp;tr=udp://tracker1.itzmx.com:8080/announce&amp;tr=udp://tracker2.itzmx.com:6961/announce&amp;tr=udp://tracker3.itzmx.com:6961/announce&amp;tr=udp://tracker4.itzmx.com:2710/announce&amp;tr=http://tr.bangumi.moe:6969/announce&amp;tr=http://t.nyaatracker.com/announce&amp;tr=http://open.nyaatorrents.info:6544/announce&amp;tr=http://t2.popgo.org:7456/annonce&amp;tr=http://share.camoe.cn:8080/announce&amp;tr=http://opentracker.acgnx.se/announce&amp;tr=http://tracker.acgnx.se/announce&amp;tr=http://nyaa.tracker.wf:7777/announce&amp;tr=http://t.acg.rip:6699/announce&amp;tr=http://opentracker.acgnx.com:6869/announce\",\"type\":\"application/x-bittorrent\",\"length\":1},\"categories\":[\"動畫\"]}]}";
    private static ApplicationContext applicationContext;
    @Resource
    IBangumiService bangumiService;
    private static BTDownloadManager manager;

    public static BTDownloadManager getInstance() {
        if (manager == null) {
            manager = new BTDownloadManager();
            if (manager.bangumiService == null) {
                manager.bangumiService = getBean(IBangumiService.class);
            }
        }
        return manager;
    }

    private BTDownloadManager() {
    }

    public void start() {
        List<BangumiItem> list = bangumiService.getAllBangumi();
        AnimationData data = new AnimationData();
        Log.i("BT","启动BT检测，总数:"+list.size());
        for (BangumiItem item : list) {
            item.setAuthor(data.nameToValue(item.getAuthor(), true) + "");
            item.setCategories(data.nameToValue(item.getCategories(), false) + "");
            String url = getRSSUrl(item);
            String _json = HttpTools.get(url);
            if (!StringUtils.isEmpty(_json)) {
                JSONObject json = JSONObject.parseObject(_json);
                download(item, json);
            } else {
                QQBotManager.getInstance().sendMessage(item.getTitle() + "\n下载失败");
                Log.i(item.getTitle() + "\n下载失败");
            }
        }
    }

    private String getRSSUrl(BangumiItem item) {
        String url = "https://api.rss2json.com/v1/api.json?rss_url=%s&api_key=wtfm5pebya13pnl8rtu51wfgfpte0mb9sap1foll&count=500";
        String dmhyUrl = "http://share.dmhy.org/topics/rss/page/1/rss.xml?keyword=%s%s%s&order=date-desc";
        dmhyUrl = String.format(dmhyUrl,
                item.getTitlekey().replace(" ", "+"),
                "-1".equals(item.getCategories()) ? "" : String.format("&sort_id=%s", item.getCategories()),
                "-1".equals(item.getAuthor()) ? "" : String.format("&team_id=%s", item.getAuthor()));
        try {
            return String.format(url, URLEncoder.encode(dmhyUrl, "UTF-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            return null;
        }
    }

    private void download(BangumiItem bangumiItem, JSONObject json) {
        if ("ok".equals(json.getString("status"))) {
            JSONArray items = json.getJSONArray("items");
            for (Object _item : items) {
                JSONObject item = (JSONObject) _item;
                //确认是为下载过的
                if (!isDownload(bangumiItem.getTitle(), item.getString("title"))) {
                    //添加到下载器
                    if (download(bangumiItem.getTitle(), item.getJSONObject("enclosure").getString("link"))) {
                        //发通知到QQ
                        JSONArray array = getDownload(bangumiItem.getTitle());
                        array.add(item.getString("title"));
                        RedisTools.set(bangumiItem.getTitle(), array.toJSONString());
                        if (item.containsKey("thumbnail")) {
                            File file = HttpTools.syncDownload(item.getString("thumbnail"), bangumiItem.getTitle() + ".jpg");
                            onSend(file, item);
                        } else {
                            onSend(null, item);
                        }
                    }
                }
            }
        }else{
            Log.i("BT","rss2json转换失败:"+bangumiItem.getTitle());
        }
    }

    private void onSend(File file, JSONObject item) {
        String text = "启动下载器\n已提交到下载:" + item.getString("title");
        Log.i(text);
        if (file == null) {
            QQBotManager.getInstance().sendMessage(text);
        } else {
            QQBotManager.getInstance().sendMessage(file, text);
        }
    }

    public boolean download(String title, String url) {
        try {
            String exec = String.format("transmission-remote -n yutou:34864394  -w /media/yutou/4t/public/download/anim/%s -a \"%s\" "
                    , title
                    , url
            );
            Log.i(exec);
            Runtime.getRuntime().exec(new String[]{"sh"
                    , "-c"
                    , exec});
            return true;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return false;
    }

    private JSONArray getDownload(String title) {
        String _title = RedisTools.get(title);
        if (StringUtils.isEmpty(_title)) {
            return new JSONArray();
        }
        return JSONArray.parseArray(_title);
    }

    private boolean isDownload(String title, String key) {
        String _title = RedisTools.get(title);
        JSONArray array;
        if (StringUtils.isEmpty(_title)) {
            return false;
        } else {
            array = JSONArray.parseArray(_title);
        }
        return array.contains(key);
    }

    public static void main(String[] args) {
        AnimationData data = new AnimationData();

        BangumiItem item = new BangumiItem();
        item.setAuthor(data.nameToValue("lleeopen", true) + "");
        item.setCategories(data.nameToValue("動畫", false) + "");
        item.setTitle("無職轉生～到了異世界就拿出真本事～");
        item.setTitlekey("无职转生");
        String url = new BTDownloadManager().getRSSUrl(item);
        com.yutou.nas.utils.Log.i(url);
    }

    @Override
    public void setApplicationContext(@NotNull ApplicationContext applicationContext) throws BeansException {
        if (BTDownloadManager.applicationContext == null) {
            BTDownloadManager.applicationContext = applicationContext;
        }
    }

    private static <T> T getBean(Class<T> clazz) {
        return applicationContext.getBean(clazz);
    }
}
