Skip to content

Commit

Permalink
NClientV2 1.6.4
Browse files Browse the repository at this point in the history
+ Switch to HTML parsing
* Bug fixes
  • Loading branch information
Dar9586 committed Jan 14, 2019
1 parent 7163fd7 commit 007ddaa
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 36 deletions.
37 changes: 19 additions & 18 deletions app/src/main/java/com/dar/nclientv2/GalleryActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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<String> params = data.getPathSegments();
for(String x:params)Log.i(Global.LOGTAG,x);
if(params.size()>2){
Expand All @@ -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(){
Expand Down Expand Up @@ -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;
}
Expand Down
8 changes: 4 additions & 4 deletions app/src/main/java/com/dar/nclientv2/adapters/ListAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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);
Expand All @@ -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);
});
Expand Down
32 changes: 26 additions & 6 deletions app/src/main/java/com/dar/nclientv2/api/Inspector.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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) {
Expand All @@ -107,16 +113,16 @@ 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){
if(update&&activity.getRecycler().getAdapter()!=null)((ListAdapter)activity.getRecycler().getAdapter()).addGalleries(galleries);
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);
Expand All @@ -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");
});
}
});
Expand All @@ -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){
Expand Down Expand Up @@ -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):"";
Expand Down
10 changes: 8 additions & 2 deletions app/src/main/java/com/dar/nclientv2/api/RandomLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {

Expand All @@ -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;
Expand Down
36 changes: 30 additions & 6 deletions app/src/main/java/com/dar/nclientv2/api/components/Gallery.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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(){
Expand Down Expand Up @@ -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<val;j++)pages[i++]=chatToExt(act);
cover= charToExt(act);
thumbnail= charToExt(reader.read());
}else for(int j=0;j<val;j++)pages[i++]= charToExt(act);
val=0;
}
}
Expand Down Expand Up @@ -297,8 +319,10 @@ private void readTags(JsonReader jr)throws IOException {
tags[type.ordinal()]=new Tag[t.size()-i];
tags[type.ordinal()]=t.subList(i,t.size()).toArray(tags[type.ordinal()]);
loadLanguage();
for(Tag[]t1:tags)if(t1!=null)for(Tag t2:t1)Queries.TagTable.insert(Database.getDatabase(),t2);
}
private void loadLanguage(){
Log.d(Global.LOGTAG,Arrays.toString(tags[TagType.LANGUAGE.ordinal()]));
//CHINESE 29963 ENGLISH 12227 JAPANESE 6346
for(Tag tag:tags[TagType.LANGUAGE.ordinal()]){
switch (tag.getId()){
Expand Down Expand Up @@ -563,7 +587,7 @@ public Gallery(String x) throws IOException{
String pagstr=jr.nextString();
if(pagstr.length()==1){
for(int a=0;a<pageCount;a++)pages[a]=stringToExt(pagstr);
}else for(int a=0;a<pageCount;a++)pages[a]=chatToExt(pagstr.charAt(a));
}else for(int a=0;a<pageCount;a++)pages[a]= charToExt(pagstr.charAt(a));
int len=TagType.values().length;
tags=new Tag[len][];
for(int a=0;a<len;a++){
Expand Down
21 changes: 21 additions & 0 deletions app/src/main/java/com/dar/nclientv2/async/database/Queries.java
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,27 @@ public static TagStatus getStatus(SQLiteDatabase db,Tag tag){
}


public static Tag[][] getTags(SQLiteDatabase db, String tagString){
Tag[][]tags=new Tag[TagType.values().length][];
String query="SELECT * FROM "+TABLE_NAME+" WHERE "+IDTAG+" IN ("+tagString+")";
Cursor cursor=db.rawQuery(query,null);
Tag[]all=new Tag[cursor.getCount()];
int i=0;
if(cursor.moveToFirst()){
do{
all[i++]=cursorToTag(cursor);
}while(cursor.moveToNext());
}
List<Tag>[]tt=new ArrayList[TagType.values().length];
for(int a=0;a<tt.length;a++)tt[a]=new ArrayList<>();
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";
Expand Down

0 comments on commit 007ddaa

Please sign in to comment.