java swing 简单实现,抖音短视频批量采集

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/jintaocccq/article/details/82820366

1.用Charles抓一抓接口,大概是这样的

https://api.amemv.com/aweme/v1/hot/search/list/?iid=44478516381&device_id=12229011297&os_api=18&app_name=aweme&channel=App%20Store&idfa=5FDF9129-D8F4-4594-96FE-6EC18AD136B9&device_platform=iphone&build_number=26006&vid=AB52ACAA-1E73-44D1-9BC2-BB1D74A6F682&openudid=282a2be914adb26833e880d38ee671e45da49829&device_type=iPhone7,2&app_version=2.6.0&version_code=2.6.0&os_version=12.0&screen_width=750&aid=1128&ac=WIFI&detail_list=0&mas=017c141ec42dace5a30cd06954fe1cbb40142e84eb9df316cf8287&as=a165392a937d1b9e756732&ts=1537580755

2.在浏览器里会拿到返回JSON数据,分析JSON对象

3.简单用了swing

上面json数据分层来写对应类,用对象来接收数据,下面来开始撸码

import java.util.List;


/**
 * json数据结构对象
 *  
 */
public class DouClass {

    private List<AwemeList> aweme_list;

    public List<AwemeList> getAweme_list() {
        return aweme_list;
    }

    public void setAweme_list(List<AwemeList> aweme_list) {
        this.aweme_list = aweme_list;
    }

    @Override
    public String toString() {
        return "DouClass [aweme_list=" + aweme_list + "]";
    }

     

     
    
}

/**
 * 大對象
 *  
 */
class AwemeList{
    private Video video;
    private ShareInfo share_info ;

    private String desc ;
    public Video getVideo() {
        return video;
    }

    public void setVideo(Video video) {
        this.video = video;
    }

     

    public ShareInfo getShare_info() {
        return share_info;
    }

    public void setShare_info(ShareInfo share_info) {
        this.share_info = share_info;
    }

    
    
    public String getDesc() {
        return desc;
    }

    public void setDesc(String desc) {
        this.desc = desc;
    }

    @Override
    public String toString() {
        return "AwemeList [video=" + video + ", share_info=" + share_info
                + ", desc=" + desc + "]";
    }

     
    
}


/**
 * 視頻
 *  
 */
class Video {
    private PlayAddr play_addr ;

    public PlayAddr getPlay_addr() {
        return play_addr;
    }

    public void setPlay_addr(PlayAddr play_addr) {
        this.play_addr = play_addr;
    }

    @Override
    public String toString() {
        return "Video [play_addr=" + play_addr + "]";
    }

     

}


/**
 * 視頻地址
 *  
 */
class PlayAddr{
    private List<String> url_list;

    public List<String> getUrl_list() {
        return url_list;
    }

    public void setUrl_list(List<String> url_list) {
        this.url_list = url_list;
    }

    @Override
    public String toString() {
        return "PlayAddr [url_list=" + url_list + "]";
    }
    
}

/**
 * 視頻內容
 *  
 */
class ShareInfo{
    private String share_title;

    public String getShare_title() {
        return share_title;
    }

    public void setShare_title(String share_title) {
        this.share_title = share_title;
    }

    @Override
    public String toString() {
        return "ShareInfo [share_title=" + share_title + "]";
    }
    
    
}


import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import com.google.gson.Gson;


//发送http请求,视频下载功能
public class DouYina {
	
	///发送http
	public static String sendGet(String url){
		 String entity = null;
		  try {
				 //创建HttpClient
			    CloseableHttpClient httpClient = HttpClients.createDefault();
			    
			    //创建请求方法
			    HttpGet httpGet = new HttpGet(url);
		
			    //设置Header模拟浏览器行为
			    httpGet.setHeader("User-Agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36");
		
			  
		        //发送请求,收取响应
		        CloseableHttpResponse httpResponse = httpClient.execute(httpGet);
		
		        if(httpResponse.getStatusLine().getStatusCode() == 200){
		            //解析响应
		            entity = EntityUtils.toString(httpResponse.getEntity());
		           // System.out.println(entity);
		        }
	
		        EntityUtils.consume(httpResponse.getEntity());
		        httpResponse.close();
		  	} catch (IOException e) {
		        e.printStackTrace();
		        return null;
		    }
		  return entity;
	 }

	
	
	public static byte[] readInputStream(InputStream inputStream) throws IOException {
		byte[] buffer = new byte[1024];
		int len = 0;
		ByteArrayOutputStream bos = new ByteArrayOutputStream();
		while ((len = inputStream.read(buffer)) != -1) {
			bos.write(buffer, 0, len);
		}
		bos.close();
		return bos.toByteArray();
	}
	
    //下载视频
	public static void downLoadFromUrl(String urlStr, String fileName, String savePath) throws IOException {
		URL url = new URL(urlStr);
		HttpURLConnection conn = (HttpURLConnection) url.openConnection();
		conn.setConnectTimeout(3000);
		conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
		InputStream inputStream = conn.getInputStream();
		byte[] getData = readInputStream(inputStream);
		java.io.File saveDir = new java.io.File(savePath);
		if (!saveDir.exists()) {
			saveDir.mkdir();
		}
		java.io.File file = new java.io.File(saveDir + java.io.File.separator + fileName);
		FileOutputStream fos = new FileOutputStream(file);
		fos.write(getData);
		if (fos != null) {
			fos.close();
		}
		if (inputStream != null) {
			inputStream.close();
		}
	}
}
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JRadioButton;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import com.google.gson.Gson;

 



public class Douyin {
 
	
	public static void main(String[] args) {
		   
		//主题
		JFrame f = new JFrame("抖音批量下载");
  
		//设置大小
		f.setSize(600, 400);
		f.setLocation(200, 200);
	 	f.setLayout(null);
		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		
		//按钮
		JButton b = new JButton("开始解析下载");
		b.setBounds(400, 30, 150, 30);
	
		
		//文字
		JLabel l = new JLabel("接口地址:");
		//l.setForeground(Color.black);
		l.setBounds(20, 20, 100, 30);
		
		//文本框
		JTextArea ta = new JTextArea("");
		ta.setBounds(80, 20, 300, 100);
		ta.setLineWrap(true);
		ta.setWrapStyleWord(true);
		
		/* //为JTextArea添加滚动条
		 
        JScrollPane jsp = new JScrollPane(ta);
        jsp.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
        jsp.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);*/
        
        
		
		//文字
		JLabel l1 = new JLabel("保存地址:");
		//l.setForeground(Color.black);
		l1.setBounds(20, 80, 100, 130);
		
		JTextField tf = new JTextField("F:\\douyin",30);
		tf.setBounds(80, 135, 400, 25);
		
		JRadioButton jr = new JRadioButton("作品");
		jr.setSelected(false);
		jr.setBounds(420, 60, 55, 30);
	
		JRadioButton jr1 = new JRadioButton("喜欢");
		jr1.setSelected(true);
		jr1.setBounds(480, 60, 80, 30);
		
		
		JTextArea ta1 = new JTextArea("初始化.... \n",20, 43);
		ta1.setBounds(50, 180, 480, 150);
		ta1.setLineWrap(true);
		ta1.setWrapStyleWord(true);
		
		/*//为JTextArea添加滚动条
        JScrollPane jsp1 = new JScrollPane(ta1);
        jsp1.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        jsp1.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
    	f.add(jsp1);*/
		
        
		//按钮分组
		ButtonGroup bg = new ButtonGroup();
		bg.add(jr);
		bg.add(jr1);
		
		f.add(ta);
		f.add(ta1);
		f.add(jr);
		f.add(jr1);
		f.add(l);
		f.add(l1);
		f.add(tf);
		f.add(b);
		
		f.setResizable(false);
		f.setVisible(true);
		
		
		
		//按钮解析监听
		b.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				
				 
				if("".equals(ta.getText())){
					 JOptionPane.showMessageDialog(f, "请输入接口地址");
					 return; 
				}
				if(jr.isSelected() == false && jr1.isSelected() == false){
					JOptionPane.showMessageDialog(f, "请选择你要下载的类型");
					return ;
				}
				if("".equals(tf.getText())){
					JOptionPane.showMessageDialog(f, "请输入你要保存的视频地址");
					 return; 
				}
				//下载操作
				
				DouYina dy = new DouYina();
				Gson g = new Gson();
				
				if(jr.isSelected() == true){
					//JOptionPane.showMessageDialog(f, "作品视频接口由于Token加密,暂时还没有办法解析出来");
					for(int i=0;i<20000;i++){
						System.out.println(i);
					}
					ta1.append("下载完成");
					
					
					return ;
				}else if(jr1.isSelected() == true){ //作品类
					 
					try {
						String json = dy.sendGet(ta.getText());
						DouClass dc = g.fromJson(json, DouClass.class);
						System.out.println(dc.getAweme_list());
						
						for(AwemeList al : dc.getAweme_list()){
							Video v = al.getVideo() ;
							PlayAddr pa = v.getPlay_addr();
							System.out.println(pa.getUrl_list().get(0) );
							
							String urlStr = pa.getUrl_list().get(0) ;
							
							long imageTitile = System.currentTimeMillis();
							
							String fileName = null ;
							
							if( !"".equals(al.getDesc() )){
								  fileName = al.getDesc() + "." + "mp4";
							}else{
								  fileName = imageTitile + "." + "mp4";
							}
							String savePath = tf.getText();
							dy.downLoadFromUrl(urlStr, fileName, savePath);
						}
					} catch (Exception e1) {
						JOptionPane.showMessageDialog(f, "接口地址有误");
						return ;
					}
					ta1.append("下载完成 \n");
				}
				
			}
			
		});
		
		
		
		
		
		 
		
				
		
	}
 
	 
}

运行效果,虽然很简陋但是使用啊

         

现在是一个jar,通过exe4j把jar包转成exe这个小工具就ok了

下面福利成功 ~~ 

猜你喜欢

转载自blog.csdn.net/jintaocccq/article/details/82820366