private void btnDownload_Click(object sender, EventArgs e) { if (this.txtNumber.Text.isEmpty()) { MsgBox.Alert("请输入抖音ID!"); return; } isDownload = true; this.button1.Enabled = false; string id = this.txtNumber.Text.Trim(); string userInfo = getUserInfo(id); string basePath = this.selectDialog1.Text.Trim(); if (!string.IsNullOrEmpty(userInfo) && !string.IsNullOrEmpty(basePath)) { try { JObject obj = (JObject)JsonConvert.DeserializeObject(userInfo); JObject objUser = (JObject)(((JObject)((JArray)obj["user_list"])[0])["user_info"]); string uid = objUser["uid"].toString(); string awemeCount = objUser["aweme_count"].toString("100"); string nickname = objUser["nickname"].toString(id); //string uniqueid = objUser["unique_id"].toString(); List <DouYin> listUrls = getDownloadUrls(uid, awemeCount); if (listUrls.Count > 0) { string path = Path.Combine(basePath, nickname); FileHelper.CreateFolder(path); this.lbMsg.Text = "正在下载:{0}/{1}".format(0, listUrls.Count); Application.DoEvents(); for (int i = 0; i < listUrls.Count; i++) { if (isDownload) { DouYin dy = listUrls[i]; this.lbMsg.Text = "正在下载:{0}/{1}".format(i, listUrls.Count); download(dy.DownloadUrl, Path.Combine(path, dy.Desc.replaceEmpty((i + 1) + "") + ".mp4")); } else { return; } } this.lbMsg.Text = "下载完毕:{0}/{1}".format(listUrls.Count, listUrls.Count); Application.DoEvents(); } else { this.lbMsg.Text = "当前账号不存在或者没有发布视频"; Application.DoEvents(); } } catch (Exception ex) { MsgBox.Alert("下载出错:" + ex.Message); } } this.button1.Enabled = true; }
private List <DouYin> getDownloadUrls(string uid, string awemeCount) { List <DouYin> downloadUrls = new List <DouYin>(); List <string> listUrls = getVideos(uid, awemeCount); foreach (var url in listUrls) { Result result = client.Request(url); if (result.IsSuccess && !string.IsNullOrEmpty(result.Msg)) { Document doc = NSoupClient.Parse(result.Msg); Element tag = doc.GetElementsByTag("script").Last; Match match = Regex.Match(tag.Data, ".*var data = \\[(.*)\\];.*"); if (match.Success) { string value = match.Groups[1].Value; JObject downloadObj = (JObject)JsonConvert.DeserializeObject(value); DateTime createTime = new DateTime(1970, 1, 1).AddSeconds(downloadObj["create_time"].toString().toInt()); string desc = downloadObj["desc"].toString(); string downloadurl = ((JArray)downloadObj["video"]["play_addr"]["url_list"])[0].toString().Replace("playwm", "play"); string vid = downloadObj["video"]["play_addr"]["uri"].toString(); string nickname = downloadObj["author"]["nickname"].toString(); DouYin douyin = new DouYin() { VideoId = vid, CreateTime = createTime, DownloadUrl = downloadurl, Desc = desc, Nickname = nickname }; if (this.checkBox1.Checked) { DateTime date = this.dateTimePicker1.Value; if (createTime.CompareTo(date) >= 0) { downloadUrls.Add(douyin); } } else { downloadUrls.Add(douyin); } } } } return(downloadUrls); }