2727import jd .controlling .ProgressController ;
2828import jd .http .Browser ;
2929import jd .http .Cookie ;
30+ import jd .http .Request ;
3031import jd .parser .Regex ;
3132import jd .plugins .CryptedLink ;
3233import jd .plugins .DecrypterPlugin ;
4243import org .appwork .storage .TypeRef ;
4344import org .appwork .utils .StringUtils ;
4445import org .jdownloader .plugins .components .config .EightChanMoeConfig ;
46+ import org .jdownloader .plugins .components .config .EightChanMoeConfig .POSTANCHORMODE ;
4547import org .jdownloader .plugins .config .PluginJsonConfig ;
4648
47- @ DecrypterPlugin (revision = "$Revision: 51151 $" , interfaceVersion = 2 , names = {}, urls = {})
49+ @ DecrypterPlugin (revision = "$Revision: 51187 $" , interfaceVersion = 2 , names = {}, urls = {})
4850public class EightChanMoe extends PluginForDecrypt {
4951 /**
5052 * https://gitgud.io/LynxChan/LynxChan/-/blob/master/doc/Json.txt
@@ -91,14 +93,22 @@ public int getMaxConcurrentProcessingInstances() {
9193 }
9294
9395 private static final String TYPE_THREAD = "https?://([^/]+)/([^/]+)/res/(\\ d+)\\ .html(#q?(\\ d+))?" ;
96+ private EightChanMoeConfig config = null ;
9497
9598 public ArrayList <DownloadLink > decryptIt (CryptedLink param , ProgressController progress ) throws Exception {
99+ config = PluginJsonConfig .get (this .getConfigInterface ());
96100 if (param .getCryptedUrl ().matches (TYPE_THREAD )) {
97101 return crawlSingleThreadAPI (param );
98102 }
99103 throw new PluginException (LinkStatus .ERROR_PLUGIN_DEFECT );
100104 }
101105
106+ @ Override
107+ public void clean () {
108+ super .clean ();
109+ config = null ;
110+ }
111+
102112 private Browser prepBrAPI (final Browser br ) {
103113 br .setFollowRedirects (true );
104114 br .getHeaders ().put ("User-Agent" , "JDownloader" );
@@ -135,7 +145,7 @@ private void getPage(Browser br, String url) throws PluginException, IOException
135145 }
136146
137147 private List <DownloadLink > parseFiles (final Browser br , List <Map <String , Object >> files ) throws PluginException , IOException {
138- final boolean preferServerFilenames = PluginJsonConfig . get ( this . getConfigInterface ()) .isPreferServerFilenamesOverPluginDefaultFilenames ();
148+ final boolean preferServerFilenames = config .isPreferServerFilenamesOverPluginDefaultFilenames ();
139149 final ArrayList <DownloadLink > ret = new ArrayList <DownloadLink >();
140150 if (files == null || files .size () == 0 ) {
141151 return ret ;
@@ -162,17 +172,17 @@ private List<DownloadLink> parseFiles(final Browser br, List<Map<String, Object>
162172 return ret ;
163173 }
164174
165- private ArrayList <DownloadLink > crawlSingleThreadAPI (final CryptedLink param ) throws IOException , PluginException , DecrypterRetryException {
175+ private ArrayList <DownloadLink > parseResponse (final CryptedLink param , final Request request , POSTANCHORMODE postMode ) throws IOException , PluginException , DecrypterRetryException {
166176 final String boardDomain = new Regex (param .getCryptedUrl (), TYPE_THREAD ).getMatch (0 );
167177 final String boardShort = new Regex (param .getCryptedUrl (), TYPE_THREAD ).getMatch (1 );
168178 final String threadID = new Regex (param .getCryptedUrl (), TYPE_THREAD ).getMatch (2 );
169- final String requestedPostId = new Regex (param .getCryptedUrl (), TYPE_THREAD ).getMatch (4 );
179+ String requestedPostId = new Regex (param .getCryptedUrl (), TYPE_THREAD ).getMatch (4 );
180+ if (POSTANCHORMODE .THREAD .equals (postMode )) {
181+ requestedPostId = null ;
182+ }
170183 if (boardDomain == null || boardShort == null || threadID == null ) {
171184 throw new PluginException (LinkStatus .ERROR_PLUGIN_DEFECT );
172- }
173- prepBrAPI (this .br );
174- getPage (br , "https://" + boardDomain + "/" + boardShort + "/res/" + threadID + ".json" );
175- if (br .getHttpConnection ().getResponseCode () == 404 ) {
185+ } else if (br .getHttpConnection ().getResponseCode () == 404 ) {
176186 throw new PluginException (LinkStatus .ERROR_FILE_NOT_FOUND );
177187 }
178188 final ArrayList <DownloadLink > ret = new ArrayList <DownloadLink >();
@@ -195,13 +205,7 @@ private ArrayList<DownloadLink> crawlSingleThreadAPI(final CryptedLink param) th
195205 ret .addAll (postItems );
196206 }
197207 }
198- if (ret .size () == 0 ) {
199- if (requestedPostId != null ) {
200- throw new DecrypterRetryException (RetryReason .EMPTY_FOLDER , "EMPTY_THREAD_POST" + threadID + "_" + requestedPostId , "Thread " + threadID + " doesn't contain any media for post " + requestedPostId );
201- } else {
202- throw new DecrypterRetryException (RetryReason .EMPTY_FOLDER , "EMPTY_THREAD" + threadID , "Thread " + threadID + " doesn't contain any media" );
203- }
204- } else {
208+ if (ret .size () > 0 ) {
205209 final FilePackage fp = FilePackage .getInstance ();
206210 if (requestedPostId != null ) {
207211 fp .setName (getHost () + " - " + boardName + " - " + threadID + " - " + requestedPostId );
@@ -213,6 +217,31 @@ private ArrayList<DownloadLink> crawlSingleThreadAPI(final CryptedLink param) th
213217 return ret ;
214218 }
215219
220+ private ArrayList <DownloadLink > crawlSingleThreadAPI (final CryptedLink param ) throws IOException , PluginException , DecrypterRetryException {
221+ final String boardDomain = new Regex (param .getCryptedUrl (), TYPE_THREAD ).getMatch (0 );
222+ final String boardShort = new Regex (param .getCryptedUrl (), TYPE_THREAD ).getMatch (1 );
223+ final String threadID = new Regex (param .getCryptedUrl (), TYPE_THREAD ).getMatch (2 );
224+ final String requestedPostId = new Regex (param .getCryptedUrl (), TYPE_THREAD ).getMatch (4 );
225+ if (boardDomain == null || boardShort == null || threadID == null ) {
226+ throw new PluginException (LinkStatus .ERROR_PLUGIN_DEFECT );
227+ }
228+ prepBrAPI (this .br );
229+ getPage (br , "https://" + boardDomain + "/" + boardShort + "/res/" + threadID + ".json" );
230+ final POSTANCHORMODE postMode = config .getPostAnchorMode ();
231+ ArrayList <DownloadLink > ret = parseResponse (param , br .getRequest (), postMode );
232+ if (ret .size () == 0 && POSTANCHORMODE .POST_OR_THREAD .equals (postMode )) {
233+ ret = parseResponse (param , br .getRequest (), POSTANCHORMODE .THREAD );
234+ }
235+ if (ret .size () == 0 ) {
236+ if (requestedPostId != null ) {
237+ throw new DecrypterRetryException (RetryReason .EMPTY_FOLDER , "EMPTY_THREAD_POST" + threadID + "_" + requestedPostId , "Thread " + threadID + " doesn't contain any media for post " + requestedPostId );
238+ } else {
239+ throw new DecrypterRetryException (RetryReason .EMPTY_FOLDER , "EMPTY_THREAD" + threadID , "Thread " + threadID + " doesn't contain any media" );
240+ }
241+ }
242+ return ret ;
243+ }
244+
216245 public boolean hasCaptcha (CryptedLink link , jd .plugins .Account acc ) {
217246 return false ;
218247 }
0 commit comments