From 007ddaafc81c751ea962633c996cf1f57539bc27 Mon Sep 17 00:00:00 2001 From: Dar9586 Date: Mon, 14 Jan 2019 14:51:45 +0100 Subject: [PATCH] NClientV2 1.6.4 + Switch to HTML parsing * Bug fixes --- .../com/dar/nclientv2/GalleryActivity.java | 37 ++++++++++--------- .../dar/nclientv2/adapters/ListAdapter.java | 8 ++-- .../java/com/dar/nclientv2/api/Inspector.java | 32 +++++++++++++--- .../com/dar/nclientv2/api/RandomLoader.java | 10 ++++- .../dar/nclientv2/api/components/Gallery.java | 36 +++++++++++++++--- .../dar/nclientv2/async/database/Queries.java | 21 +++++++++++ 6 files changed, 108 insertions(+), 36 deletions(-) diff --git a/app/src/main/java/com/dar/nclientv2/GalleryActivity.java b/app/src/main/java/com/dar/nclientv2/GalleryActivity.java index 0ff4caa1..bb0b3be7 100644 --- a/app/src/main/java/com/dar/nclientv2/GalleryActivity.java +++ b/app/src/main/java/com/dar/nclientv2/GalleryActivity.java @@ -56,6 +56,7 @@ protected void onCreate(Bundle savedInstanceState) { recycler=findViewById(R.id.recycler); refresher=findViewById(R.id.refresher); gallery= getIntent().getParcelableExtra(getPackageName()+".GALLERY"); + int id= getIntent().getIntExtra(getPackageName()+".ID",0); Log.d(Global.LOGTAG,""+gallery); if(getIntent().getBooleanExtra(getPackageName()+".INSTANTDOWNLOAD",false))downloadGallery(); isLocal=getIntent().getBooleanExtra(getPackageName()+".ISLOCAL",false); @@ -65,7 +66,9 @@ protected void onCreate(Bundle savedInstanceState) { Uri data = getIntent().getData(); int isZoom=0; - if(data != null && data.getPathSegments().size() >= 2){ + if(id!=0){//if from main list + new Inspector(this,0,""+id,ApiRequestType.BYSINGLE); + }else if(data != null && data.getPathSegments().size() >= 2){//if using an URL List params = data.getPathSegments(); for(String x:params)Log.i(Global.LOGTAG,x); if(params.size()>2){ @@ -76,7 +79,7 @@ protected void onCreate(Bundle savedInstanceState) { } } new Inspector(this,isZoom,params.get(1),ApiRequestType.BYSINGLE); - }else loadGallery(gallery,zoom); + }else loadGallery(gallery,zoom);//if already has gallery } private void lookup(){ @@ -105,37 +108,35 @@ private void loadGallery(GenericGallery gall,int zoom) { private boolean isFavorite; + private Menu menu; + public void loadMenu(){ + if(menu.findItem(R.id.add_online_gallery).isVisible()){ + boolean x=Login.isOnlineFavorite(gallery.getId()); + menu.findItem(R.id.add_online_gallery).setTitle(x?R.string.remove_from_online_favorites:R.string.add_to_online_favorite); + menu.findItem(R.id.add_online_gallery).setIcon(x?R.drawable.ic_star:R.drawable.ic_star_border); + } + menu.findItem(R.id.add_online_gallery).setTitle(Login.isOnlineFavorite(gallery.getId())?R.string.remove_from_online_favorites:R.string.add_to_online_favorite); + menu.findItem(R.id.share).setVisible(gallery!=null&&gallery.isValid()); + menu.findItem(R.id.favorite_manager).setIcon((isFavorite=Favorites.isFavorite(gallery))?R.drawable.ic_favorite:R.drawable.ic_favorite_border); + menu.findItem(R.id.load_internet).setVisible(isLocal&&gallery!=null&&gallery.getId()!=-1); + } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.gallery, menu); - if(!isLocal&&Login.isLogged()){ - MenuItem item= menu.findItem(R.id.add_online_gallery); - item.setVisible(true); - boolean x=Login.isOnlineFavorite(gallery.getId()); - item.setIcon(x?R.drawable.ic_star_border:R.drawable.ic_star); - item.setTitle(x?R.string.add_to_online_favorite:R.string.remove_from_online_favorites); - } + this.menu=menu; menu.findItem(R.id.add_online_gallery).setVisible(!isLocal&&Login.isLogged()); - if(menu.findItem(R.id.add_online_gallery).isVisible()){ - boolean x=Login.isOnlineFavorite(gallery.getId()); - menu.findItem(R.id.add_online_gallery).setTitle(x?R.string.remove_from_online_favorites:R.string.add_to_online_favorite); - menu.findItem(R.id.add_online_gallery).setIcon(x?R.drawable.ic_star:R.drawable.ic_star_border); - } - menu.findItem(R.id.add_online_gallery).setTitle(Login.isOnlineFavorite(gallery.getId())?R.string.remove_from_online_favorites:R.string.add_to_online_favorite); Global.setTint(menu.findItem(R.id.download_gallery).getIcon()); Global.setTint(menu.findItem(R.id.load_internet).getIcon()); Global.setTint(menu.findItem(R.id.change_view).getIcon()); Global.setTint(menu.findItem(R.id.share).getIcon()); Global.setTint(menu.findItem(R.id.related).getIcon()); - menu.findItem(R.id.share).setVisible(gallery!=null&&gallery.isValid()); - menu.findItem(R.id.favorite_manager).setIcon((isFavorite=Favorites.isFavorite(gallery))?R.drawable.ic_favorite:R.drawable.ic_favorite_border); Global.setTint(menu.findItem(R.id.favorite_manager).getIcon()); menu.findItem(R.id.favorite_manager).setVisible(!isLocal||isFavorite); menu.findItem(R.id.download_gallery).setVisible(!isLocal); menu.findItem(R.id.related).setVisible(!isLocal); - menu.findItem(R.id.load_internet).setVisible(isLocal&&gallery!=null&&gallery.getId()!=-1); + if(gallery!=null)loadMenu(); updateColumnCount(false); return true; } diff --git a/app/src/main/java/com/dar/nclientv2/adapters/ListAdapter.java b/app/src/main/java/com/dar/nclientv2/adapters/ListAdapter.java index 1857547e..93f5ae65 100644 --- a/app/src/main/java/com/dar/nclientv2/adapters/ListAdapter.java +++ b/app/src/main/java/com/dar/nclientv2/adapters/ListAdapter.java @@ -11,13 +11,13 @@ import com.dar.nclientv2.GalleryActivity; import com.dar.nclientv2.R; import com.dar.nclientv2.api.components.Gallery; +import com.dar.nclientv2.api.enums.TitleType; import com.dar.nclientv2.components.BaseActivity; import com.dar.nclientv2.settings.Global; import com.dar.nclientv2.settings.TagV2; import java.io.File; import java.util.List; -import java.util.Locale; import androidx.annotation.NonNull; import androidx.cardview.widget.CardView; @@ -77,7 +77,8 @@ public void onBindViewHolder(@NonNull final GenericAdapter.ViewHolder holder, in if(black)holder.layout.setBackgroundColor(Color.BLACK); holder.overlay.setVisibility((queryString!=null&&ent.hasIgnoredTags(queryString))?View.VISIBLE:View.GONE); loadGallery(holder,ent); - holder.title.setText(ent.getTitle()); + holder.pages.setVisibility(View.GONE); + holder.title.setText(ent.getTitle(TitleType.ENGLISH)); if(Global.getOnlyLanguage()==null||context instanceof GalleryActivity) { switch (ent.getLanguage()) { case CHINESE: holder.flag.setText("\uD83C\uDDE8\uD83C\uDDF3");break; @@ -86,7 +87,6 @@ public void onBindViewHolder(@NonNull final GenericAdapter.ViewHolder holder, in case UNKNOWN: holder.flag.setText("\uD83C\uDFF3"); } }else holder.flag.setVisibility(View.GONE); - holder.pages.setText(String.format(Locale.US, "%d", ent.getPageCount())); holder.title.setOnClickListener(v -> { Layout layout = holder.title.getLayout(); if(layout.getEllipsisCount(layout.getLineCount()-1)>0)holder.title.setMaxLines(7); @@ -95,7 +95,7 @@ public void onBindViewHolder(@NonNull final GenericAdapter.ViewHolder holder, in }); holder.layout.setOnClickListener(v -> { Intent intent = new Intent(context, GalleryActivity.class); - intent.putExtra(context.getPackageName() + ".GALLERY", ent); + intent.putExtra(context.getPackageName() + ".ID", ent.getId()); context.startActivity(intent); holder.overlay.setVisibility((queryString!=null&&ent.hasIgnoredTags(queryString))?View.VISIBLE:View.GONE); }); diff --git a/app/src/main/java/com/dar/nclientv2/api/Inspector.java b/app/src/main/java/com/dar/nclientv2/api/Inspector.java index 2a8462fd..d788029d 100644 --- a/app/src/main/java/com/dar/nclientv2/api/Inspector.java +++ b/app/src/main/java/com/dar/nclientv2/api/Inspector.java @@ -14,8 +14,14 @@ import com.dar.nclientv2.settings.Global; import com.dar.nclientv2.settings.TagV2; +import org.jsoup.Jsoup; +import org.jsoup.nodes.Document; +import org.jsoup.nodes.Element; +import org.jsoup.select.Elements; + import java.io.IOException; import java.io.Reader; +import java.io.StringReader; import java.util.ArrayList; import java.util.List; @@ -88,7 +94,7 @@ public Inspector(final BaseActivity activity, final int page, final String query actualQuery=query; actualRequestType=requestType; } - createUrl(); + url=getUsableURL(); client.newCall(new Request.Builder().url(url).build()).enqueue(new Callback() { @Override public void onFailure(@NonNull Call call, @NonNull IOException e) { @@ -107,7 +113,8 @@ public void onFailure(@NonNull Call call, @NonNull IOException e) { @Override public void onResponse(@NonNull Call call,@NonNull Response response) throws IOException { Log.d(Global.LOGTAG,"Response of "+url); - parseGalleries(response.body().charStream()); + Document d=Jsoup.parse(response.body().byteStream(),"UTF-8","https://nhentai.net"); + parseGalleries(requestType==ApiRequestType.BYSINGLE?d.getElementsByTag("script"):d.getElementsByClass("gallery")); for (Gallery x:galleries)if(x.getId()>Global.getMaxId())Global.updateMaxId(activity,x.getId()); activity.runOnUiThread(() -> { if(requestType!=ApiRequestType.BYSINGLE){ @@ -115,8 +122,7 @@ public void onResponse(@NonNull Call call,@NonNull Response response) throws IOE else activity.getRecycler().setAdapter(new ListAdapter(activity, galleries,Global.getRemoveIgnoredGalleries()?null:query)); ((MainActivity)activity).setInspector(Inspector.this); ((MainActivity)activity).showPageSwitcher(Inspector.this.page,Inspector.this.pageCount); - } - else{ + }else{ Intent intent=new Intent(activity, GalleryActivity.class); intent.putExtra(activity.getPackageName()+".GALLERY",galleries.get(0)); intent.putExtra(activity.getPackageName()+".ZOOM",page-1); @@ -125,6 +131,7 @@ public void onResponse(@NonNull Call call,@NonNull Response response) throws IOE if(page!=-1)activity.finish(); } activity.getRefresher().setRefreshing(false); + Log.d(Global.LOGTAG,"EECUTED"); }); } }); @@ -143,7 +150,20 @@ public String toString() { ", galleries=" + galleries + '}'; } - + private void parseGalleries(Elements e)throws IOException{ + galleries=new ArrayList<>(requestType==ApiRequestType.BYSINGLE?1:e.size()); + if(requestType!=ApiRequestType.BYSINGLE){ + for(Element el:e)galleries.add(new Gallery(el)); + }else{ + String x=e.last().html(); + int s=x.indexOf("new N.gallery(")+14; + Log.d(Global.LOGTAG,"SINGLE: "+x); + x=x.substring(s,x.indexOf('\n',s)-2); + Log.d(Global.LOGTAG,"SINGLE: "+x); + galleries.add(new Gallery(new JsonReader(new StringReader(x)))); + } + } + @Deprecated private void parseGalleries(Reader s) throws IOException { JsonReader reader=new JsonReader(s); if (requestType == ApiRequestType.BYSINGLE){ @@ -190,7 +210,7 @@ private String appendedLanguage(){ } return ""; } - + @Deprecated private void createUrl() { StringBuilder builder = new StringBuilder("https://nhentai.net/api/"); String tagQuery=Global.getRemoveIgnoredGalleries()?TagV2.getQueryString(query):""; diff --git a/app/src/main/java/com/dar/nclientv2/api/RandomLoader.java b/app/src/main/java/com/dar/nclientv2/api/RandomLoader.java index 7e857658..01498f1f 100644 --- a/app/src/main/java/com/dar/nclientv2/api/RandomLoader.java +++ b/app/src/main/java/com/dar/nclientv2/api/RandomLoader.java @@ -7,7 +7,10 @@ import com.dar.nclientv2.api.components.Gallery; import com.dar.nclientv2.settings.Global; +import org.jsoup.Jsoup; + import java.io.IOException; +import java.io.StringReader; import java.util.ArrayList; import java.util.List; import java.util.Random; @@ -35,7 +38,7 @@ public RandomLoader(RandomActivity activity) { private void loadRandomGallery(){ if(galleries.size()>=MAXLOADED)return; final int id=random.nextInt(Global.getMaxId())+1; - Global.client.newCall(new Request.Builder().url("https://nhentai.net/api/gallery/" + id).build()).enqueue(new Callback() { + Global.client.newCall(new Request.Builder().url("https://nhentai.net/g/" + id).build()).enqueue(new Callback() { @Override public void onFailure(@NonNull Call call, @NonNull IOException e) { @@ -44,7 +47,10 @@ public void onFailure(@NonNull Call call, @NonNull IOException e) { @Override public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException { Log.d(Global.LOGTAG,"Random: "+id); - Gallery x = new Gallery(new JsonReader(response.body().charStream())); + String str=Jsoup.parse(response.body().byteStream(),"UTF-8","https://nhentai.net").getElementsByTag("script").last().html(); + int s=str.indexOf("new N.gallery(")+14; + str=str.substring(s,str.indexOf('\n',s)-2); + Gallery x = new Gallery(new JsonReader(new StringReader(str))); if (!x.isValid()) { loadRandomGallery(); return; diff --git a/app/src/main/java/com/dar/nclientv2/api/components/Gallery.java b/app/src/main/java/com/dar/nclientv2/api/components/Gallery.java index 621b5a52..21add94e 100644 --- a/app/src/main/java/com/dar/nclientv2/api/components/Gallery.java +++ b/app/src/main/java/com/dar/nclientv2/api/components/Gallery.java @@ -14,9 +14,12 @@ import com.dar.nclientv2.api.enums.TagType; import com.dar.nclientv2.api.enums.TitleType; import com.dar.nclientv2.async.database.Queries; +import com.dar.nclientv2.settings.Database; import com.dar.nclientv2.settings.Global; import com.dar.nclientv2.settings.TagV2; +import org.jsoup.nodes.Element; + import java.io.IOException; import java.io.StringReader; import java.io.StringWriter; @@ -34,6 +37,25 @@ import okhttp3.Response; public class Gallery extends GenericGallery{ + public Gallery(Element e){ + String temp; + String tags=e.attr("data-tags").replace(' ',','); + Log.d(Global.LOGTAG,"TAGS: "+tags); + this.tags=Queries.TagTable.getTags(Database.getDatabase(),tags); + loadLanguage(); + + Element a=e.getElementsByTag("a").first(); + temp=a.attr("href"); + id=Integer.parseInt(temp.substring(3,temp.length()-1)); + Log.d(Global.LOGTAG,"LANGUAGE FOR ID: "+id+"= "+language); + a=e.getElementsByTag("img").first(); + temp=a.attr("data-src"); + //32=https://t.nhentai.net/galleries/ + mediaId=Integer.parseInt(temp.substring(32,temp.lastIndexOf('/'))); + thumbnail=charToExt(temp.charAt(temp.length()-3)); + titles[TitleType.ENGLISH.ordinal()]=e.getElementsByTag("div").first().text(); + } + private enum ImageExt{PNG,JPG,GIF} private Date uploadDate; private int favoriteCount,id,pageCount,mediaId; @@ -92,7 +114,7 @@ private static char extToChar(ImageExt ext){ } return '\0'; } - private static ImageExt chatToExt(int ext){ + private static ImageExt charToExt(int ext){ switch(ext){ case 'g':return ImageExt.GIF; case 'p':return ImageExt.PNG; @@ -101,7 +123,7 @@ private static ImageExt chatToExt(int ext){ return null; } private static ImageExt stringToExt(String ext){ - return chatToExt(ext.charAt(0)); + return charToExt(ext.charAt(0)); } public String createPagePath(){ @@ -137,9 +159,9 @@ public void readPagePath(String path)throws IOException{ }else{ if(pages==null){ pages=new ImageExt[pageCount=val]; - cover=chatToExt(act); - thumbnail=chatToExt(reader.read()); - }else for(int j=0;j[]tt=new ArrayList[TagType.values().length]; + for(int a=0;a(); + for(Tag s:all){ + tt[s.getType().ordinal()].add(s); + } + for(TagType type:TagType.values()){ + tags[type.ordinal()]=tt[type.ordinal()].toArray(new Tag[0]); + } + return tags; + } } static class BridgeTable{ static final String TABLE_NAME="GalleryTags";