Commit cb6a0b84 by [zhangzhiwei]

添加文章类型

parent efe57d38
......@@ -35,7 +35,14 @@ public class TouTiaoArticle implements Serializable{
private String readNum;
private String shareNum;
private List<String> labelList;
private String articleType;
public String getArticleType() {
return articleType;
}
public void setArticleType(String articleType) {
this.articleType = articleType;
}
public String getCommentCount() {
return commentCount;
}
......@@ -113,7 +120,8 @@ public class TouTiaoArticle implements Serializable{
public TouTiaoArticle(){}
public TouTiaoArticle(String url,String title,String user_id,
String source,Date time,String content,String commentCount,
String playCount,String readNum, String shareNum,String type)
String playCount,String readNum, String shareNum,String type
,String articleType)
{
this.url = url ;
this.title = title;
......@@ -126,6 +134,7 @@ public class TouTiaoArticle implements Serializable{
this.playCount = playCount;
this.shareNum = shareNum;
this.commentCount = commentCount;
this.articleType = articleType;
}
public String toString()
......@@ -143,6 +152,7 @@ public class TouTiaoArticle implements Serializable{
+ ", readNum = " + readNum
+ ", shareNum = " + shareNum
+ ", labelList = " + labelList
+ ", articleType = " + articleType
+ "]";
}
......
......@@ -39,10 +39,12 @@ import com.zhiwei.toutiao.util.Tools;
* @date 2016年9月2日 上午11:17:44
*/
public class TouTiaoArticleParse {
private TouTiaoArticleParse() {}
private TouTiaoArticleParse() {
}
private static Logger logger = LogManager.getLogger(TouTiaoArticleParse.class);
private static HttpBoot httpBoot = new HttpBoot();
/***
* 获取头条数据
*
......@@ -54,23 +56,25 @@ public class TouTiaoArticleParse {
* @throws Exception
*/
@Deprecated
public static Map<String, Object> getTouTiaoList(String media_id, String max_behot_time,Date endData, Proxy proxy ) throws Exception{
public static Map<String, Object> getTouTiaoList(String media_id, String max_behot_time, Date endData, Proxy proxy)
throws Exception {
Signature signature = new Signature();
String url = "https://www.toutiao.com/pgc/ma/?page_type=1&media_id="+media_id+"&count=20&as="+signature.getAs()+"&cp="+signature.getCp();
if(max_behot_time!=null){
url = url + "&max_behot_time="+max_behot_time;
String url = "https://www.toutiao.com/pgc/ma/?page_type=1&media_id=" + media_id + "&count=20&as="
+ signature.getAs() + "&cp=" + signature.getCp();
if (max_behot_time != null) {
url = url + "&max_behot_time=" + max_behot_time;
}
Map<String,String> headerMap = Tools.getTouTiaoHeader();
Map<String, String> headerMap = Tools.getTouTiaoHeader();
headerMap.put("Referer", url);
String htmlBody = null;
try {
htmlBody = httpBoot.syncCall(RequestUtils.wrapGet(url, headerMap), proxy).body().string();
if(htmlBody != null && htmlBody.contains("behot_time")){
if (htmlBody != null && htmlBody.contains("behot_time")) {
Map<String, Object> ttList = parseHtmlByAccount(htmlBody, endData);
if(ttList!=null && ttList.size()>0){
if (ttList != null && ttList.size() > 0) {
return ttList;
}
}else{
} else {
logger.info("数据为null");
}
} catch (Exception e) {
......@@ -79,27 +83,29 @@ public class TouTiaoArticleParse {
}
return Collections.emptyMap();
}
@Deprecated
public static Map<String, Object> getTouTiaoList(String media_id, Long max_behot_time,Date endData, ProxyHolder proxy ) throws Exception{
public static Map<String, Object> getTouTiaoList(String media_id, Long max_behot_time, Date endData,
ProxyHolder proxy) throws Exception {
Signature signature = new Signature();
String as=signature.getAs();
String cp=signature.getCp();
String url = "https://www.toutiao.com/pgc/ma/?page_type=1&media_id="+media_id+"&count=20&as="+as+"&cp="+cp;
if(max_behot_time!=null){
url = url + "&max_behot_time="+max_behot_time;
String as = signature.getAs();
String cp = signature.getCp();
String url = "https://www.toutiao.com/pgc/ma/?page_type=1&media_id=" + media_id + "&count=20&as=" + as + "&cp="
+ cp;
if (max_behot_time != null) {
url = url + "&max_behot_time=" + max_behot_time;
}
Map<String,String> headerMap = Tools.getTouTiaoHeader();
Map<String, String> headerMap = Tools.getTouTiaoHeader();
headerMap.put("Referer", url);
String htmlBody = null;
try {
htmlBody = httpBoot.syncCall(RequestUtils.wrapGet(url, headerMap), proxy).body().string();
if(htmlBody != null && htmlBody.contains("behot_time")){
if (htmlBody != null && htmlBody.contains("behot_time")) {
Map<String, Object> ttList = parseHtmlByAccount(htmlBody, endData);
if(ttList!=null && ttList.size()>0){
if (ttList != null && ttList.size() > 0) {
return ttList;
}
}else{
} else {
logger.info("数据为null");
}
} catch (Exception e) {
......@@ -108,9 +114,10 @@ public class TouTiaoArticleParse {
}
return Collections.emptyMap();
}
/**
* 获取今日头条历史文章接口新
*
* @param user_id
* @param max_behot_time
* @param endData
......@@ -118,26 +125,29 @@ public class TouTiaoArticleParse {
* @return
* @throws Exception
*/
public static Map<String, Object> getTouTiaoHistory(String user_id, String max_behot_time,Date endData, Proxy proxy ) throws Exception{
for(int i=0;i<3;i++){
Signature signature = new Signature(user_id, max_behot_time);
String as=signature.getAs();
String cp=signature.getCp();
String _signature = signature.getSignature();
String url = "https://www.toutiao.com/c/user/article/?page_type=1&user_id="+user_id+"&max_behot_time="+max_behot_time+"&count=20&as="+as+"&cp="+cp+"&_signature="+_signature;
System.out.println(url);
Map<String,String> headerMap = new HashMap<String,String>();
headerMap.put("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.80 Safari/537.36");
headerMap.put("referer","https://www.toutiao.com/c/user/"+user_id+"/");
String htmlBody = null;
public static Map<String, Object> getTouTiaoHistory(String user_id, String max_behot_time, Date endData,
Proxy proxy) throws Exception {
for (int i = 0; i < 3; i++) {
Signature signature = new Signature(user_id, max_behot_time);
String as = signature.getAs();
String cp = signature.getCp();
String _signature = signature.getSignature();
String url = "https://www.toutiao.com/c/user/article/?page_type=1&user_id=" + user_id + "&max_behot_time="
+ max_behot_time + "&count=20&as=" + as + "&cp=" + cp + "&_signature=" + _signature;
System.out.println(url);
Map<String, String> headerMap = new HashMap<String, String>();
headerMap.put("user-agent",
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.80 Safari/537.36");
headerMap.put("referer", "https://www.toutiao.com/c/user/" + user_id + "/");
String htmlBody = null;
try {
htmlBody = httpBoot.syncCall(RequestUtils.wrapGet(url, headerMap), proxy).body().string();
if(htmlBody != null && htmlBody.contains("behot_time")){
if (htmlBody != null && htmlBody.contains("behot_time")) {
Map<String, Object> ttList = parseHtmlByAccount(user_id, htmlBody, endData);
if(ttList!=null && ttList.size()>0){
if (ttList != null && ttList.size() > 0) {
return ttList;
}
}else{
} else {
logger.info("数据为null");
continue;
}
......@@ -148,29 +158,32 @@ public class TouTiaoArticleParse {
}
return Collections.emptyMap();
}
public static Map<String, Object> getTouTiaoHistory(String user_id, String max_behot_time,Date endData, ProxyHolder proxy ) throws Exception{
for(int i=0;i<3;i++){
public static Map<String, Object> getTouTiaoHistory(String user_id, String max_behot_time, Date endData,
ProxyHolder proxy) throws Exception {
for (int i = 0; i < 3; i++) {
Signature signature = new Signature(user_id, max_behot_time);
String as=signature.getAs();
String cp=signature.getCp();
String as = signature.getAs();
String cp = signature.getCp();
String _signature = signature.getSignature();
String url = "https://www.toutiao.com/c/user/article/?page_type=1&user_id="+user_id+"&max_behot_time="+max_behot_time+"&count=20&as="+as+"&cp="+cp+"&_signature="+_signature;
String url = "https://www.toutiao.com/c/user/article/?page_type=1&user_id=" + user_id + "&max_behot_time="
+ max_behot_time + "&count=20&as=" + as + "&cp=" + cp + "&_signature=" + _signature;
logger.info("当前采集的历史文章链接:::{}", url);
Map<String,String> headerMap = new HashMap<String,String>();
headerMap.put("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.80 Safari/537.36");
headerMap.put("referer","https://www.toutiao.com/c/user/"+user_id+"/");
Map<String, String> headerMap = new HashMap<String, String>();
headerMap.put("user-agent",
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.80 Safari/537.36");
headerMap.put("referer", "https://www.toutiao.com/c/user/" + user_id + "/");
String htmlBody = null;
try {
htmlBody = httpBoot.syncCall(RequestUtils.wrapGet(url, headerMap), proxy).body().string();
if(htmlBody != null && htmlBody.contains("behot_time")){
if (htmlBody != null && htmlBody.contains("behot_time")) {
Map<String, Object> ttList = parseHtmlByAccount(user_id, htmlBody, endData);
if(ttList!=null && ttList.size()>0){
if (ttList != null && ttList.size() > 0) {
return ttList;
}else{
} else {
break;
}
}else{
} else {
logger.info("数据为null,获取到的文本为:::{}", htmlBody);
continue;
}
......@@ -181,8 +194,7 @@ public class TouTiaoArticleParse {
}
return Collections.emptyMap();
}
/***
* 根据帐号解析历史文章地址
*
......@@ -210,25 +222,28 @@ public class TouTiaoArticleParse {
String shareNum = null;
String source = null;
String user_id = null;
String articleType = null;
List<String> labelList = null;
for (int i = 0; i < jsonArray.size(); i++) {
try {
JSONObject data = jsonArray.getJSONObject(i);
String href = "https://www.toutiao.com/";
if(data.containsKey("group_id")){
href = href+"a"+data.getLongValue("group_id");
if (data.containsKey("group_id")) {
href = href + "a" + data.getLongValue("group_id");
title = data.getString("title");
content = data.getString("abstract");
time = data.getLongValue("behot_time")*1000+"";
time = data.getLongValue("behot_time") * 1000 + "";
date = TimeParse.stringFormartDate(time);
readNum = data.getString("go_detail_count");
commentNum = data.getString("comments_count");
playNum = data.getString("detail_play_effective_count");
shareNum = data.getString("share_count");
source = data.getString("source");
user_id = data.getLong("creator_uid")+"";
TouTiaoArticle tt = new TouTiaoArticle(href, title, user_id, source, date, content, commentNum, playNum, readNum, shareNum,"今日头条");
if(data.containsKey("label")){
user_id = data.getLong("creator_uid") + "";
articleType = data.getString("chinese_tag");
TouTiaoArticle tt = new TouTiaoArticle(href, title, user_id, source, date, content, commentNum,
playNum, readNum, shareNum, "今日头条", articleType);
if (data.containsKey("label")) {
labelList = data.getJSONArray("label").toJavaList(String.class);
tt.setLabelList(labelList);
}
......@@ -243,11 +258,11 @@ public class TouTiaoArticleParse {
logger.error("数据解析出现问题,{}", e.getMessage());
return null;
}
if(endDate!=null){
if(max_behot_time!=null && !"0".equals(max_behot_time)){
Date nextDate = new Date(Long.valueOf(max_behot_time+"000"));
if(endDate.after(nextDate)){
if (endDate != null) {
if (max_behot_time != null && !"0".equals(max_behot_time)) {
Date nextDate = new Date(Long.valueOf(max_behot_time + "000"));
if (endDate.after(nextDate)) {
max_behot_time = null;
}
}
......@@ -256,9 +271,7 @@ public class TouTiaoArticleParse {
map.put("data", dataList);
return map;
}
private static Map<String, Object> parseHtmlByAccount(String user_id, String htmlBody, Date endDate) {
Map<String, Object> map = new HashMap<String, Object>();
Long max_behot_time = null;
......@@ -276,28 +289,30 @@ public class TouTiaoArticleParse {
String playNum = null;
String shareNum = null;
String source = null;
String articleType = null;
List<String> labelList = null;
for (int i = 0; i < jsonArray.size(); i++) {
try {
JSONObject data = jsonArray.getJSONObject(i);
String href = "https://www.toutiao.com/";
if(data.containsKey("group_id")){
href = href+"a"+data.getLongValue("group_id");
if (data.containsKey("group_id")) {
href = href + "a" + data.getLongValue("group_id");
title = data.getString("title");
content = data.getString("abstract");
time = data.getLongValue("behot_time")*1000+"";
time = data.getLongValue("behot_time") * 1000 + "";
date = TimeParse.stringFormartDate(time);
readNum = data.getString("go_detail_count");
commentNum = data.getString("comments_count");
playNum = data.getString("detail_play_effective_count");
shareNum = data.getString("share_count");
source = data.getString("source");
TouTiaoArticle tt = new TouTiaoArticle(href, title, user_id, source, date, content, commentNum, playNum, readNum, shareNum,"今日头条");
if(data.containsKey("label")){
articleType = data.getString("chinese_tag");
TouTiaoArticle tt = new TouTiaoArticle(href, title, user_id, source, date, content, commentNum,
playNum, readNum, shareNum, "今日头条", articleType);
if (data.containsKey("label")) {
labelList = data.getJSONArray("label").toJavaList(String.class);
tt.setLabelList(labelList);
}
System.out.println(tt.toString());
dataList.add(tt);
}
} catch (Exception e) {
......@@ -309,10 +324,10 @@ public class TouTiaoArticleParse {
logger.error("数据解析出现问题,{}", e.getMessage());
return null;
}
if(endDate!=null){
if(max_behot_time!=null && !"0".equals(max_behot_time)){
Date nextDate = new Date(Long.valueOf(max_behot_time+"000"));
if(endDate.after(nextDate)){
if (endDate != null) {
if (max_behot_time != null && !"0".equals(max_behot_time)) {
Date nextDate = new Date(Long.valueOf(max_behot_time + "000"));
if (endDate.after(nextDate)) {
max_behot_time = null;
}
}
......@@ -321,36 +336,39 @@ public class TouTiaoArticleParse {
map.put("data", dataList);
return map;
}
/**
* @Title: getMicroTouTiaoCrawler
* @author hero
* @Title: getMicroTouTiaoCrawler
* @author hero
* @Description: 根据用户user_id查询用户微头条数据
* @param @param user_id
* @param @param endDate
* @param @param proxy
* @param @param
* user_id
* @param @param
* endDate
* @param @param
* proxy
* @param @return
* @param @throws IOException 设定文件
* @param @throws
* IOException 设定文件
* @return List<Map<String,Object>> 返回类型
*/
public static Map<String, Object> getMicroTouTiaoCrawler(String user_id, Date endDate, Proxy proxy, String max_behot_time) throws IOException {
String url = "https://www.toutiao.com/c/ugc/content/list/" + user_id+"/";
if(max_behot_time!=null){
public static Map<String, Object> getMicroTouTiaoCrawler(String user_id, Date endDate, Proxy proxy,
String max_behot_time) throws IOException {
String url = "https://www.toutiao.com/c/ugc/content/list/" + user_id + "/";
if (max_behot_time != null) {
url = url + "?max_time=" + max_behot_time;
}
Map<String, String> headerMap = Tools.getTouTiaoHeader();
headerMap.put("Referer", "https://www.toutiao.com/c/user/" + user_id + "/");
System.out.println(url);
try {
String htmlBody = httpBoot.syncCall(RequestUtils.wrapGet(url,headerMap), proxy).body().string();
String htmlBody = httpBoot.syncCall(RequestUtils.wrapGet(url, headerMap), proxy).body().string();
if (htmlBody != null) {
Map<String, Object> dataMap = parseHtmlByMicroAccount(htmlBody, endDate);
if(dataMap!=null && dataMap.size()>0){
if (dataMap != null && dataMap.size() > 0) {
return dataMap;
}
}else{
} else {
logger.info("数据为null");
}
} catch (Exception e) {
......@@ -359,25 +377,24 @@ public class TouTiaoArticleParse {
}
return null;
}
public static Map<String, Object> getMicroTouTiaoCrawler(String user_id, Date endDate, ProxyHolder proxy, Long max_behot_time) throws IOException {
String url = "https://www.toutiao.com/c/ugc/content/list/" + user_id+"/";
if(max_behot_time!=null){
public static Map<String, Object> getMicroTouTiaoCrawler(String user_id, Date endDate, ProxyHolder proxy,
Long max_behot_time) throws IOException {
String url = "https://www.toutiao.com/c/ugc/content/list/" + user_id + "/";
if (max_behot_time != null) {
url = url + "?max_time=" + max_behot_time;
}
Map<String, String> headerMap = Tools.getTouTiaoHeader();
headerMap.put("Referer", "https://www.toutiao.com/c/user/" + user_id + "/");
System.out.println(url);
try {
String htmlBody = httpBoot.syncCall(RequestUtils.wrapGet(url,headerMap), proxy).body().string();
String htmlBody = httpBoot.syncCall(RequestUtils.wrapGet(url, headerMap), proxy).body().string();
if (htmlBody != null) {
Map<String, Object> dataMap = parseHtmlByMicroAccount(htmlBody, endDate);
if(dataMap!=null && dataMap.size()>0){
if (dataMap != null && dataMap.size() > 0) {
return dataMap;
}
}else{
} else {
logger.info("数据为null");
}
} catch (Exception e) {
......@@ -386,14 +403,17 @@ public class TouTiaoArticleParse {
}
return null;
}
/**
* @Title: parseHtmlByMicroAccount
* @author hero
* @Description: 解析微头条数据
* @param @param htmlBody
* @param @param endDate
* @param @return 设定文件
* @Title: parseHtmlByMicroAccount
* @author hero
* @Description: 解析微头条数据
* @param @param
* htmlBody
* @param @param
* endDate
* @param @return
* 设定文件
* @return Map<String,Object> 返回类型
*/
private static Map<String, Object> parseHtmlByMicroAccount(String htmlBody, Date endDate) {
......@@ -413,51 +433,50 @@ public class TouTiaoArticleParse {
String commentNum = null;
String playNum = null;
String user_id = null;
String articleType = null;
int count = 16;
for (int i = 0; i < jsonArray.size(); i++) {
try {
JSONObject data = jsonArray.getJSONObject(i);
max_behot_time = data.getLongValue("create_time");
date = new Date(max_behot_time*1000);
date = new Date(max_behot_time * 1000);
href = "https://www.toutiao.com/a" + data.getString("thread_id");
source = data.getJSONObject("ugc_user").getString("name");
content = data.getString("content");
readNum = data.getInteger("read_count")+"";
commentNum = data.getInteger("comment_count")+"";
readNum = data.getInteger("read_count") + "";
commentNum = data.getInteger("comment_count") + "";
user_id = data.getJSONObject("ugc_user").getString("user_id");
if(content!=null && !"".equals(content)){
if(content.length()<16){
if (content != null && !"".equals(content)) {
if (content.length() < 16) {
count = content.length();
}
title = content.substring(0, count);
}
TouTiaoArticle tt = new TouTiaoArticle(href, title, user_id, source,date, content, commentNum, playNum, readNum, "0","微头条");
TouTiaoArticle tt = new TouTiaoArticle(href, title, user_id, source, date, content, commentNum,
playNum, readNum, "0", "微头条", articleType);
dataList.add(tt);
} catch (Exception e) {
continue;
}
}
/**验证是否有下一页数据**/
if(more){
if(max_behot_time!=null && max_behot_time!=0){
if(endDate.after(date)){
/** 验证是否有下一页数据 **/
if (more) {
if (max_behot_time != null && max_behot_time != 0) {
if (endDate.after(date)) {
max_behot_time = null;
}
}
}else{
} else {
max_behot_time = null;
}
} catch (Exception e) {
e.printStackTrace();
}
map.put("max_behot_time", max_behot_time);
map.put("data", dataList);
return map;
}
}
......@@ -98,7 +98,7 @@ public class TouTiaoChannelParse {
}
url = getUrl(url);
date = TimeParse.stringFormartDate(time);
TouTiaoArticle tt = new TouTiaoArticle(url, title, null,source, date, content, comment_count, "-1", "-1", "-1","今日头条");
TouTiaoArticle tt = new TouTiaoArticle(url, title, null,source, date, content, comment_count, "-1", "-1", "-1","今日头条",null);
ttList.add(tt);
} catch (JSONException e) {
continue;
......
......@@ -152,7 +152,7 @@ public class TouTiaoParse {
String shareNum = data.getString("share_count");
if (endData.before(date)) {
TouTiaoArticle tt = new TouTiaoArticle(href, title, null,source, date, content, commentNum, playNum, readNum, shareNum,"今日头条");
TouTiaoArticle tt = new TouTiaoArticle(href, title, null,source, date, content, commentNum, playNum, readNum, shareNum,"今日头条",null);
dataList.add(tt);
}else
{
......
......@@ -90,7 +90,7 @@ public class TouTiaoSearchParse {
String user_id = jso.getString("user_id");
Date date = TimeParse.stringFormartDate(time);
TouTiaoArticle tt = new TouTiaoArticle(url, title, user_id,source, date, content, comment_count, "-1", "-1", "-1","今日头条");
TouTiaoArticle tt = new TouTiaoArticle(url, title, user_id,source, date, content, comment_count, "-1", "-1", "-1","今日头条",null);
ttList.add(tt);
} catch (JSONException e) {
logger.debug("解析数据出现问题", e.fillInStackTrace());
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment