栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java 通过Twitter API v2 提取收藏列表视频数据

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java 通过Twitter API v2 提取收藏列表视频数据

我有个好朋友,喜欢听音乐,在twitter上收藏了很多音乐博主,想要每天定时拉取音乐MV视频,问我能不能做,我当然很热心的帮助他.

 一. 创建twitter app 拿到Bearer Token

地址:https://developer.twitter.com/en/portal/projects-and-apps

具体怎么创建网上很多文章有说明,但大多数是过时的,创建应用无非是验证邮箱,然后填一些个人信息,创建应用原因等等,自己慢慢摸索,最需要注意:

twitter帐号关联的邮箱地址不要用国内的,不然收不到验证码

我改成gmail邮箱,才成功收到验证码

二.查看api 找到获取列表推文接口

https://developer.twitter.com/en/docs/twitter-api/v1/accounts-and-users/create-manage-lists/api-reference/get-lists-statuseshttps://developer.twitter.com/en/docs/twitter-api/v1/accounts-and-users/create-manage-lists/api-reference/get-lists-statusesapi接口:https://api.twitter.com/1.1/lists/statuses.json

参数:

list_id:列表ID(自己去twitter列表F12去看自己的列表ID)

since_id:最小推文id

count:推文数量

三.通过接口获取数据解析并下载视频
  • 获取列表的推文内容
    public static List getListTweets(String listId, String startListId, Integer count) {
        try {
            if (StringUtils.isEmpty(listId)) return null;
            trustEveryone();
            System.setProperty("https.protocols", "TLSv1,TLSv1.1,TLSv1.2,SSLv3");
            String baseUrl = listApi + "?list_id=" + listId;
            if (StringUtils.isNotEmpty(startListId)) {
                baseUrl += "&since_id=" + startListId;
            }
            if (count != null && count > 0) {
                baseUrl += "&count" + count;
            }
            URL url = new URL(baseUrl);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("GET");
            conn.setRequestProperty("Authorization", "Bearer " + token);
            conn.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
            conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36");
            conn.setRequestProperty("Accept", "*
    private static Tweet formaterTweet(JSonObject json) {
        try {
            if (json == null || !json.containsKey("extended_entities")) return null;
            Tweet tweet = new Tweet();
            tweet.setTwId(json.getString("id"));
            tweet.setTwText(UnicodeUtil.toString(json.getString("text")));
            tweet.setTwTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(json.getString("created_at"))));
            tweet.setMediaId(json.getJSonObject("extended_entities").getJSonArray("media").getJSonObject(0).getString("id"));
            tweet.setMediaPic(json.getJSonObject("extended_entities").getJSonArray("media").getJSonObject(0).getString("media_url"));
            if (json.getJSonObject("extended_entities").getJSonArray("media").size() > 1 || !json.getJSonObject("extended_entities").getJSonArray("media").getJSonObject(0).containsKey("video_info"))
                return null;
            //获取分辨率最高的视频地址
            List mediaz = json.getJSonObject("extended_entities").getJSonArray("media").getJSonObject(0).getJSonObject("video_info").getJSonArray("variants").toJavaList(JSONObject.class);
            List medias = mediaz.stream().filter(t -> t.getString("content_type").equals("video/mp4")).sorted((t1, t2) -> {
                return t2.getInteger("bitrate") - t1.getInteger("bitrate");
            }).collect(Collectors.toList());
            tweet.setMediaUrl(medias.get(0).getString("url"));
            tweet.setUserId(json.getJSonObject("user").getString("id"));
            tweet.setUserName(json.getJSonObject("user").getString("name"));
            return tweet;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

Tweet 实体:

@Data
@NoArgsConstructor
@AllArgsConstructor
public class Tweet {
    private String twId; //推文ID
    private String twText;  //推文内容
    private String twTime;  //发推时间
    private String mediaId;  //视频ID
    private String mediaPic; //视频缩略图
    private String mediaUrl; //视频地址
    private String userId;   //发推用户ID
    private String userName; //发推用户名

    @Override
    public String toString() {
        return JSONObject.toJSonString(this);
    }
}


  • 下载内容
    
    private static void downloadMedia(Tweet tweet) {
        try {
            String tweetFile = localSource + "/" + tweet.getTwId() + "/" + tweet.getTwId();
            //保存缩略图
            if (!FileUtil.exist(tweetFile + ".jpg")) {
                saveToFile(tweet.getMediaPic(), tweetFile + ".jpg");
            }
            //保存视频
            if (!FileUtil.exist(tweetFile + ".mp4")) {
                saveToFile(tweet.getMediaUrl(), tweetFile + ".mp4");
            }
            //保存推文内容
            if (!FileUtil.exist(tweetFile + ".txt")) {
                FileUtil.writeString(tweet.getTwText(), tweetFile + ".txt", Charset.forName("utf-8"));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

效果:

代码完整内容查看:https://download.csdn.net/download/D_lady/44794742

github:https://github.com/doodt/TwitterUtil/blob/main/TwitterUtil.java

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/572080.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号