|
16 | 16 | package jd.plugins.decrypter; |
17 | 17 |
|
18 | 18 | import java.util.ArrayList; |
| 19 | +import java.util.HashMap; |
19 | 20 | import java.util.List; |
20 | 21 | import java.util.Map; |
21 | 22 | import java.util.regex.Pattern; |
22 | 23 |
|
23 | 24 | import org.appwork.storage.TypeRef; |
24 | | -import org.appwork.utils.StringUtils; |
25 | 25 | import org.appwork.utils.parser.UrlQuery; |
26 | 26 | import org.jdownloader.scripting.JavaScriptEngineFactory; |
27 | 27 |
|
|
40 | 40 | import jd.plugins.PluginForDecrypt; |
41 | 41 | import jd.plugins.hoster.AllDebridCom; |
42 | 42 |
|
43 | | -@DecrypterPlugin(revision = "$Revision: 49909 $", interfaceVersion = 3, names = {}, urls = {}) |
| 43 | +@DecrypterPlugin(revision = "$Revision: 49981 $", interfaceVersion = 3, names = {}, urls = {}) |
44 | 44 | public class AlldebridComFolder extends PluginForDecrypt { |
45 | 45 | public AlldebridComFolder(PluginWrapper wrapper) { |
46 | 46 | super(wrapper); |
@@ -91,75 +91,79 @@ public ArrayList<DownloadLink> decryptIt(final CryptedLink param, ProgressContro |
91 | 91 | query.appendEncoded("agent", AllDebridCom.agent_raw); |
92 | 92 | query.appendEncoded("apikey", AllDebridCom.getStoredApiKey(account)); |
93 | 93 | query.appendEncoded("id", magnetID); |
94 | | - br.getPage(AllDebridCom.api_base + "/magnet/status?" + query.toString()); |
| 94 | + /* When "jd" parameter is supplied, information which is unnecessary for JD will be excluded from json response. */ |
| 95 | + query.appendEncoded("jd", ""); |
| 96 | + br.getPage(AllDebridCom.api_base_41 + "/magnet/status?" + query.toString()); |
95 | 97 | final Map<String, Object> entries = restoreFromString(br.getRequest().getHtmlCode(), TypeRef.MAP); |
96 | 98 | if (entries.containsKey("error")) { |
97 | 99 | throw new PluginException(LinkStatus.ERROR_FILE_NOT_FOUND); |
98 | 100 | } |
99 | 101 | final Map<String, Object> magnet = (Map<String, Object>) JavaScriptEngineFactory.walkJson(entries, "data/magnets"); |
100 | 102 | final String torrentName = (String) magnet.get("filename"); |
101 | 103 | final String torrentNameEscaped = Pattern.quote(torrentName); |
102 | | - final List<Map<String, Object>> resourcelist = (List<Map<String, Object>>) magnet.get("links"); |
| 104 | + final List<Map<String, Object>> resourcelist = (List<Map<String, Object>>) magnet.get("files"); |
103 | 105 | if (resourcelist == null || resourcelist.isEmpty()) { |
104 | 106 | /* Probably unfinished torrent download */ |
105 | 107 | throw new PluginException(LinkStatus.ERROR_FILE_NOT_FOUND); |
106 | 108 | } |
107 | | - final ArrayList<DownloadLink> ret = new ArrayList<DownloadLink>(); |
108 | 109 | final String folderRoot = torrentName; |
109 | 110 | final FilePackage fpRoot = FilePackage.getInstance(); |
110 | 111 | fpRoot.setName(folderRoot); |
111 | | - for (final Map<String, Object> resource : resourcelist) { |
112 | | - final String url = resource.get("link").toString(); |
113 | | - final String filename = resource.get("filename").toString(); |
114 | | - final DownloadLink dl = this.createDownloadlink(url); |
115 | | - dl.setName(filename); |
116 | | - dl.setDownloadSize(((Number) resource.get("size")).longValue()); |
117 | | - final boolean isSpecialRar = filename.matches("^" + torrentNameEscaped + "(\\.rar|\\.part\\d+\\.rar)$"); |
118 | | - if (isSpecialRar) { |
119 | | - dl.setRelativeDownloadFolderPath(folderRoot); |
120 | | - dl._setFilePackage(fpRoot); |
121 | | - } else { |
122 | | - /* Check whether or not this file goes into a deeper subfolder level. */ |
123 | | - String filePath = getFilePath((List<Object>) resource.get("files"), ""); |
124 | | - /* Path is full path with filename at the end -> Remove that */ |
125 | | - filePath = filePath.replaceAll("/" + Pattern.quote(filename) + "$", ""); |
126 | | - if (!StringUtils.isEmpty(filePath)) { |
127 | | - /* File that goes into (nested) subfolder. */ |
128 | | - dl.setRelativeDownloadFolderPath(folderRoot + filePath); |
129 | | - final FilePackage nestedFilePackage = FilePackage.getInstance(); |
130 | | - nestedFilePackage.setName(folderRoot + filePath); |
131 | | - dl._setFilePackage(nestedFilePackage); |
132 | | - } else { |
133 | | - /* File that is in the root of the torrent main folder (named after torrent name). */ |
134 | | - dl.setRelativeDownloadFolderPath(folderRoot); |
135 | | - dl._setFilePackage(fpRoot); |
136 | | - } |
137 | | - } |
138 | | - dl.setAvailable(true); |
139 | | - ret.add(dl); |
140 | | - } |
| 112 | + final ArrayList<DownloadLink> ret = new ArrayList<DownloadLink>(); |
| 113 | + crawlRecursive(ret, new HashMap<String, FilePackage>(), resourcelist, torrentNameEscaped, torrentName); |
141 | 114 | return ret; |
142 | 115 | } |
143 | 116 |
|
144 | | - /** Recursive function which returns the complete path to a file inside nested json arrays. */ |
145 | | - private String getFilePath(final List<Object> subfolderList, String path) { |
146 | | - if (subfolderList.isEmpty()) { |
| 117 | + private List<DownloadLink> crawlRecursive(final List<DownloadLink> results, final Map<String, FilePackage> fps, Object o, final String torrentNameEscaped, final String path) { |
| 118 | + if (o == null) { |
147 | 119 | return null; |
148 | 120 | } |
149 | | - final Map<String, Object> entries = (Map<String, Object>) subfolderList.get(0); |
150 | | - final Object subfolderNameO = entries.get("n"); |
151 | | - if (subfolderNameO == null) { |
152 | | - return null; |
153 | | - } |
154 | | - final String subfolderName = (String) subfolderNameO; |
155 | | - path += "/" + subfolderName; |
156 | | - final Object nextSubFolderLevel = entries.get("e"); |
157 | | - if (nextSubFolderLevel == null) { |
158 | | - /* We've reached the end */ |
159 | | - return path; |
| 121 | + if (o instanceof List) { |
| 122 | + for (final Object resourceO : (List<Object>) o) { |
| 123 | + this.crawlRecursive(results, fps, resourceO, torrentNameEscaped, path); |
| 124 | + } |
| 125 | + return results; |
160 | 126 | } else { |
161 | | - /* Go deeper */ |
162 | | - return this.getFilePath((List<Object>) nextSubFolderLevel, path); |
| 127 | + final Map<String, Object> resource = (Map<String, Object>) o; |
| 128 | + final String url = (String) resource.get("l"); |
| 129 | + final String name = resource.get("n").toString(); |
| 130 | + if (url != null) { |
| 131 | + /* Single file */ |
| 132 | + final String folderRoot = new Regex(path, "^([^/]+)").getMatch(0); |
| 133 | + final DownloadLink dl = this.createDownloadlink(url); |
| 134 | + dl.setName(name); |
| 135 | + dl.setDownloadSize(((Number) resource.get("s")).longValue()); |
| 136 | + final boolean isSpecialRar = name.matches("^" + torrentNameEscaped + "(\\.rar|\\.part\\d+\\.rar)$"); |
| 137 | + if (isSpecialRar) { |
| 138 | + FilePackage fpRoot = fps.get(folderRoot); |
| 139 | + if (fpRoot == null) { |
| 140 | + fpRoot = FilePackage.getInstance(); |
| 141 | + fpRoot.setName(folderRoot); |
| 142 | + fps.put(folderRoot, fpRoot); |
| 143 | + } |
| 144 | + dl.setRelativeDownloadFolderPath(folderRoot); |
| 145 | + dl._setFilePackage(fpRoot); |
| 146 | + } else { |
| 147 | + /* Remove filename from path. */ |
| 148 | + final String filePath = path.replaceAll("/" + Pattern.quote(name) + "$", ""); |
| 149 | + /* File that goes into (nested) subfolder. */ |
| 150 | + dl.setRelativeDownloadFolderPath(filePath); |
| 151 | + FilePackage nestedFilePackage = fps.get(filePath); |
| 152 | + if (nestedFilePackage == null) { |
| 153 | + nestedFilePackage = FilePackage.getInstance(); |
| 154 | + nestedFilePackage.setName(filePath); |
| 155 | + fps.put(filePath, nestedFilePackage); |
| 156 | + } |
| 157 | + dl._setFilePackage(nestedFilePackage); |
| 158 | + } |
| 159 | + dl.setAvailable(true); |
| 160 | + results.add(dl); |
| 161 | + } else { |
| 162 | + final List<Map<String, Object>> nextSubFolderLevel = (List<Map<String, Object>>) resource.get("e"); |
| 163 | + /* Go deeper */ |
| 164 | + return this.crawlRecursive(results, fps, nextSubFolderLevel, torrentNameEscaped, path + "/" + name); |
| 165 | + } |
163 | 166 | } |
| 167 | + return results; |
164 | 168 | } |
165 | 169 | } |
0 commit comments