Skip to content

Commit

Permalink
部分api直接反序列化到实体类
Browse files Browse the repository at this point in the history
  • Loading branch information
luern0313 committed Jun 14, 2021
1 parent bd76d92 commit a6e073a
Show file tree
Hide file tree
Showing 7 changed files with 153 additions and 111 deletions.
53 changes: 29 additions & 24 deletions app/src/main/java/cn/luern0313/wristbilibili/api/BangumiApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,21 @@



import android.content.Context;

import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;

import cn.luern0313.lson.LsonUtil;
import cn.luern0313.lson.element.LsonArray;
import cn.luern0313.lson.TypeReference;
import cn.luern0313.lson.element.LsonObject;
import cn.luern0313.wristbilibili.R;
import cn.luern0313.wristbilibili.models.BangumiModel;
import cn.luern0313.wristbilibili.models.BangumiRecommendModel;
import cn.luern0313.wristbilibili.models.BaseModel;
import cn.luern0313.wristbilibili.models.ListBangumiModel;
import cn.luern0313.wristbilibili.util.MyApplication;
import cn.luern0313.wristbilibili.util.NetWorkUtil;
import cn.luern0313.wristbilibili.util.SharedPreferencesUtil;
import okhttp3.Response;
Expand All @@ -21,6 +27,7 @@

public class BangumiApi
{
private final Context ctx;
private final String mid;
private final String csrf;
private final String access_key;
Expand All @@ -32,6 +39,7 @@ public class BangumiApi

public BangumiApi(String season_id)
{
this.ctx = MyApplication.getContext();
this.mid = SharedPreferencesUtil.getString(SharedPreferencesUtil.mid, "");
this.csrf = SharedPreferencesUtil.getString(SharedPreferencesUtil.csrf , "");
this.access_key = SharedPreferencesUtil.getString(SharedPreferencesUtil.accessKey, "");
Expand All @@ -50,38 +58,35 @@ public BangumiApi(String season_id)

public BangumiModel getBangumiInfo() throws IOException
{
String url = "https://api.bilibili.com/pgc/view/app/season";
String temp_per = "access_key=" + access_key + "&appkey=" + ConfInfoApi.getConf("appkey") + "&build=" + ConfInfoApi.getConf("build") + "&platform=android&season_id=" + season_id + "&ts=" + (int) (System.currentTimeMillis() / 1000);
String sign = ConfInfoApi.calc_sign(temp_per, ConfInfoApi.getConf("app_secret"));
Response response = NetWorkUtil.get("https://api.bilibili.com/pgc/view/app/season?" + temp_per + "&sign=" + sign, appHeaders);
LsonObject result = LsonUtil.parseAsObject(response.body().string());
if(result.getInt("code") == 0)
bangumiModel = LsonUtil.fromJson(result.getJsonObject("result"), BangumiModel.class);
Response response = NetWorkUtil.get(url + "?" + temp_per + "&sign=" + sign, appHeaders);
BaseModel<BangumiModel> baseModel = LsonUtil.fromJson(LsonUtil.parse(response.body().string()), new TypeReference<BaseModel<BangumiModel>>(){});
if(baseModel.isSuccess())
bangumiModel = baseModel.getData();
return bangumiModel;
}

public ArrayList<ListBangumiModel> getBangumiRecommend() throws IOException
{
String url = "https://api.bilibili.com/pgc/web/recommend/related/recommend?season_id=" + season_id;
LsonObject result = LsonUtil.parseAsObject(NetWorkUtil.get(url, webHeaders).body().string());
ArrayList<ListBangumiModel> bangumiRecommendModelArrayList = new ArrayList<>();
if(result.getInt("code", -1) == 0)
{
LsonArray seasonArray = result.getJsonObject("result").getJsonArray("season");
for(int i = 0; i < seasonArray.size(); i++)
bangumiRecommendModelArrayList.add(LsonUtil.fromJson(seasonArray.getJsonObject(i), ListBangumiModel.class));
}
return bangumiRecommendModelArrayList;
String url = "https://api.bilibili.com/pgc/web/recommend/related/recommend";
String arg = "season_id=" + season_id;
BaseModel<BangumiRecommendModel> baseModel = LsonUtil.fromJson(LsonUtil.parse(NetWorkUtil.get(url + "?" + arg, webHeaders).body().string()), new TypeReference<BaseModel<BangumiRecommendModel>>(){});
if(baseModel.isSuccess())
return baseModel.getData().getListBangumiModelArrayList();
return null;
}

public String followBangumi(boolean isFollow) throws IOException
{
String url = isFollow ? "https://api.bilibili.com/pgc/web/follow/add" : "https://api.bilibili.com/pgc/web/follow/del";
String per = "season_id=" + season_id + "&csrf=" + csrf;
LsonObject result = LsonUtil.parseAsObject(NetWorkUtil.post(url, per, webHeaders).body().string());
if(result.getInt("code") == 0)
BaseModel<LsonObject> baseModel = LsonUtil.fromJson(LsonUtil.parse(NetWorkUtil.post(url, per, webHeaders).body().string()), new TypeReference<BaseModel<LsonObject>>(){});
if(baseModel.isSuccess())
{
bangumiModel.setUserIsFollow(isFollow);
return result.getJsonObject("result").getString("toast");
return baseModel.getData().getString("toast");
}
return (isFollow ? "" : "取消") + "追番错误";
}
Expand All @@ -90,19 +95,19 @@ public String shareBangumi(String text) throws IOException
{
String url = "https://api.vc.bilibili.com/dynamic_repost/v1/dynamic_repost/share";
String per = "csrf_token=" + csrf + "&platform=pc&type=8&uid=&share_uid=" + mid + "&content=" + URLEncoder.encode(text, "UTF-8") + "&repost_code=20000&rid=" + bangumiModel.getUserProgressAid();
LsonObject result = LsonUtil.parseAsObject(NetWorkUtil.post(url, per, webHeaders).body().string());
if(result.getInt("code") == 0)
BaseModel<?> baseModel = LsonUtil.fromJson(LsonUtil.parse(NetWorkUtil.post(url, per, webHeaders).body().string()), new TypeReference<BaseModel<?>>(){});
if(baseModel.isSuccess())
return "";
return "未知错误";
return ctx.getString(R.string.main_error_unknown);
}

public String sendReply(String text) throws IOException
{
String url = "https://api.bilibili.com/x/v2/reply/add";
String per = "oid=" + bangumiModel.getUserProgressAid() + "&type=1&message=" + text + "&plat=1&jsonp=jsonp&csrf=" + csrf;
LsonObject result = LsonUtil.parseAsObject(NetWorkUtil.post(url, per, webHeaders).body().string());
if(result.getInt("code") == 0)
BaseModel<?> baseModel = LsonUtil.fromJson(LsonUtil.parse(NetWorkUtil.post(url, per, webHeaders).body().string()), new TypeReference<BaseModel<?>>(){});
if(baseModel.isSuccess())
return "";
return "发送评论失败";
return ctx.getString(R.string.main_error_unknown);
}
}
25 changes: 10 additions & 15 deletions app/src/main/java/cn/luern0313/wristbilibili/api/FavorBoxApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
import java.util.ArrayList;

import cn.luern0313.lson.LsonUtil;
import cn.luern0313.lson.element.LsonArray;
import cn.luern0313.lson.element.LsonObject;
import cn.luern0313.lson.TypeReference;
import cn.luern0313.wristbilibili.models.BaseModel;
import cn.luern0313.wristbilibili.models.FavorBoxModel;
import cn.luern0313.wristbilibili.util.NetWorkUtil;
import cn.luern0313.wristbilibili.util.SharedPreferencesUtil;
Expand All @@ -22,7 +22,7 @@ public class FavorBoxApi
private final boolean isShowOthersBox;
private final ArrayList<String> webHeaders;

private final ArrayList<FavorBoxModel> favorBoxArrayList = new ArrayList<>();
private FavorBoxModel favorBoxModel;

public FavorBoxApi(String mid, boolean isShowOthersBox)
{
Expand All @@ -35,24 +35,19 @@ public FavorBoxApi(String mid, boolean isShowOthersBox)
}};
}

public ArrayList<FavorBoxModel> getFavorBox() throws IOException
public FavorBoxModel getFavorBox() throws IOException
{
String url = "http://space.bilibili.com/ajax/fav/getBoxList";
String arg = "mid=" + mid;
String a = NetWorkUtil.get(url + "?" + arg, webHeaders).body().string();
Log.w("bilibili", a);
LsonObject result = LsonUtil.parseAsObject(a);
if(result.getAsBoolean("status", false))
{
LsonArray favorBoxJSONArray = result.getAsJsonObject("data").getAsJsonArray("list");
for(int i = 0; i < favorBoxJSONArray.size(); i++)
favorBoxArrayList.add(LsonUtil.fromJson(favorBoxJSONArray.getAsJsonObject(i), FavorBoxModel.class));
}
BaseModel<FavorBoxModel> baseModel = LsonUtil.fromJson(LsonUtil.parse(a), new TypeReference<BaseModel<FavorBoxModel>>(){});
if(baseModel.isSuccess())
favorBoxModel = baseModel.getData();
if(isShowOthersBox)
{
favorBoxArrayList.add(new FavorBoxModel(1));
favorBoxArrayList.add(new FavorBoxModel(2));
favorBoxModel.getBoxModelArrayList().add(new FavorBoxModel.BoxModel(1));
favorBoxModel.getBoxModelArrayList().add(new FavorBoxModel.BoxModel(2));
}
return favorBoxArrayList;
return favorBoxModel;
}
}
24 changes: 23 additions & 1 deletion app/src/main/java/cn/luern0313/wristbilibili/api/HistoryApi.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
package cn.luern0313.wristbilibili.api;

import android.content.Context;

import java.io.IOException;
import java.util.ArrayList;

import cn.luern0313.lson.LsonUtil;
import cn.luern0313.lson.TypeReference;
import cn.luern0313.lson.element.LsonArray;
import cn.luern0313.lson.element.LsonObject;
import cn.luern0313.wristbilibili.R;
import cn.luern0313.wristbilibili.models.BaseModel;
import cn.luern0313.wristbilibili.models.ListVideoModel;
import cn.luern0313.wristbilibili.util.MyApplication;
import cn.luern0313.wristbilibili.util.NetWorkUtil;
import cn.luern0313.wristbilibili.util.SharedPreferencesUtil;

Expand All @@ -15,10 +21,16 @@
*/
public class HistoryApi
{
private final Context ctx;
private final String csrf;

private final ArrayList<String> webHeaders;
public HistoryApi()
{
webHeaders = new ArrayList<String>(){{
this.ctx = MyApplication.getContext();
this.csrf = SharedPreferencesUtil.getString(SharedPreferencesUtil.csrf, "");

this.webHeaders = new ArrayList<String>(){{
add("Cookie"); add(SharedPreferencesUtil.getString(SharedPreferencesUtil.cookies, ""));
add("Referer"); add("https://www.bilibili.com/");
add("User-Agent"); add(ConfInfoApi.USER_AGENT_WEB);
Expand All @@ -39,4 +51,14 @@ public ArrayList<ListVideoModel> getHistory(int pn) throws IOException
}
return videoModelArrayList;
}

public String delHistory(String aid) throws IOException
{
String url = "https://api.bilibili.com/x/v2/history/delete";
String per = "kid=archive_" + aid + "&jsonp=jsonp&csrf=" + csrf;
BaseModel<?> baseModel = LsonUtil.fromJson(LsonUtil.parse(NetWorkUtil.post(url, per, webHeaders).body().string()), new TypeReference<BaseModel<?>>(){});
if(baseModel.isSuccess())
return "";
return ctx.getString(R.string.main_error_unknown);
}
}
66 changes: 43 additions & 23 deletions app/src/main/java/cn/luern0313/wristbilibili/api/RankingApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,17 @@

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;

import cn.luern0313.lson.LsonUtil;
import cn.luern0313.lson.TypeReference;
import cn.luern0313.lson.element.LsonArray;
import cn.luern0313.lson.element.LsonObject;
import cn.luern0313.wristbilibili.models.BaseModel;
import cn.luern0313.wristbilibili.models.RankingModel;
import cn.luern0313.wristbilibili.util.FileUtil;
import cn.luern0313.wristbilibili.util.MyApplication;
import cn.luern0313.wristbilibili.util.NetWorkUtil;
import cn.luern0313.wristbilibili.util.SharedPreferencesUtil;

Expand All @@ -20,32 +25,38 @@ public class RankingApi
{
private final String mid;
private final String csrf;

private RankingModel rankingModel;
private static LinkedHashMap<String, String> rankingRegionMap;

private final ArrayList<String> webHeaders;
private final ArrayList<String> appHeaders;

public RankingApi()
{
initConfig();
this.csrf = SharedPreferencesUtil.getString(SharedPreferencesUtil.csrf, "");
this.mid = SharedPreferencesUtil.getString(SharedPreferencesUtil.mid, "");

webHeaders = new ArrayList<String>(){{
add("Cookie"); add(SharedPreferencesUtil.getString(SharedPreferencesUtil.cookies, ""));
add("Referer"); add("https://www.bilibili.com/");
add("User-Agent"); add(ConfInfoApi.USER_AGENT_WEB);
}};
appHeaders = new ArrayList<String>(){{
add("Cookie"); add(SharedPreferencesUtil.getString(SharedPreferencesUtil.cookies, ""));
add("User-Agent"); add(ConfInfoApi.USER_AGENT_OWN);
}};
}

public ArrayList<RankingModel> getRankingVideo(int pn) throws IOException
public RankingModel getRankingVideo(String rid) throws IOException
{
ArrayList<RankingModel> rankingVideoArrayList = new ArrayList<>();
String url = "http://app.bilibili.com/x/v2/rank/region";
String temp_per = "appkey=" + ConfInfoApi.getConf("appkey") + "&build=" + ConfInfoApi.getConf("build") + "&mobi_app=android&platform=android&pn=" + pn + "&ps=20&rid=0&ts=" + (int) (System.currentTimeMillis() / 1000);
String sign = ConfInfoApi.calc_sign(temp_per, ConfInfoApi.getConf("app_secret"));
LsonObject result = LsonUtil.parseAsObject(NetWorkUtil.get(url + "?" + temp_per + "&sign=" + sign, appHeaders).body().string());
if(result.getAsInt("code", -1) == 0)
{
LsonArray list = result.getAsJsonArray("data");
for (int i = 0; i < list.size(); i++)
rankingVideoArrayList.add(LsonUtil.fromJson(list.getAsJsonObject(i), RankingModel.class));
}
return rankingVideoArrayList;
String url = "https://api.bilibili.com/x/web-interface/ranking/v2";
String per = "rid=" + rid + "&type=all";
BaseModel<RankingModel> baseModel = LsonUtil.fromJson(LsonUtil.parse(NetWorkUtil.get(url + "?" + per, webHeaders).body().string()), new TypeReference<BaseModel<RankingModel>>(){});
if(baseModel.isSuccess())
rankingModel = baseModel.getData();
return rankingModel;
}

public LinkedHashMap<Integer, String> getPickUpVideo()
Expand All @@ -54,12 +65,12 @@ public LinkedHashMap<Integer, String> getPickUpVideo()
{
LinkedHashMap<Integer, String> pickUpMap = new LinkedHashMap<>();
String url = "http://luern0313.cn:8080/bp/getHistoryPickUpVideo";
LsonObject result = LsonUtil.parseAsObject(NetWorkUtil.get(url).body().string());
LsonArray list = result.getAsJsonArray("Data");
LsonObject result = LsonUtil.parseAsObject(NetWorkUtil.get(url, appHeaders).body().string());
LsonArray list = result.getJsonArray("Data");
for(int i = 0; i < list.size(); i++)
{
final LsonObject jsonObject = list.getAsJsonObject(i);
pickUpMap.put(Integer.valueOf(jsonObject.getAsString("Date")), jsonObject.getAsString("Aid"));
final LsonObject jsonObject = list.getJsonObject(i);
pickUpMap.put(Integer.valueOf(jsonObject.getString("Date")), jsonObject.getString("Aid"));
}
return pickUpMap;
}
Expand All @@ -70,17 +81,26 @@ public LinkedHashMap<Integer, String> getPickUpVideo()
return null;
}

public void clickPickUpVideo()
public String getRegionName(String rid)
{
return rankingRegionMap.get(rid);
}

public HashMap<String, String> getRankingRegionMap()
{
return rankingRegionMap;
}

private void initConfig()
{
try
{
String url = "http://luern0313.cn:8080/bp/clickPickUpVideo";
NetWorkUtil.get(url);
if(rankingRegionMap == null)
rankingRegionMap = LsonUtil.fromJson(LsonUtil.parse(FileUtil.fileReader(
MyApplication.getContext().getAssets().open("ranking_region.json"))), new TypeReference<LinkedHashMap<String, String>>(){});
}
catch (Exception e)
catch (IOException ignored)
{
e.printStackTrace();
}
}

}
Loading

0 comments on commit a6e073a

Please sign in to comment.