tiktok.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. console.log('bundle2!')
  2. printable = (platform) => {
  3. return platform === "WEB";
  4. }
  5. parseSetCookie = (headers) => {
  6. if (!headers) {
  7. return ""
  8. }
  9. const setCookie = headers['Set-Cookie']
  10. if (!setCookie) {
  11. return ""
  12. }
  13. console.log(`setCookie: ${setCookie}`)
  14. let result = {}
  15. const needCookieNames = ['ttwid', 'tt_csrf_token', 'tt_chain_token'];
  16. for (const i in needCookieNames) {
  17. const cookieName = needCookieNames[i];
  18. const regexp = new RegExp(`${cookieName}=([^;,]+)`)
  19. const match = setCookie.match(regexp)
  20. if (match && match.length === 2) {
  21. result[cookieName] = match[1]
  22. }
  23. }
  24. return result;
  25. }
  26. request = async (method, url, data = null, headers = {}, platform) => {
  27. console.log(`request url:${url}`)
  28. console.log(`request data:${data}`)
  29. console.log(`request method:${method}`)
  30. console.log(`request headers:${JSON.stringify((headers))}`)
  31. if (platform === "WEB") {
  32. const res = await fetch(url, {
  33. 'mode': 'cors',
  34. 'method': method,
  35. 'headers': headers,
  36. 'body': data
  37. })
  38. const resData = await res.text()
  39. return Promise.resolve({
  40. 'data': resData,
  41. 'headers': res.headers
  42. });
  43. }
  44. return new Promise((resolve, reject) => {
  45. AF.request(url, method, data, headers, (data, headers, err) => {
  46. if (err) {
  47. reject(err);
  48. } else {
  49. console.log(`response headers: ${headers}`);
  50. resolve({
  51. 'data': data,
  52. 'headers': JSON.parse(headers)
  53. });
  54. }
  55. });
  56. })
  57. }
  58. detail = async (url, platform) => {
  59. try {
  60. const htmlResp = await request('GET', url, null, {
  61. 'Host': 'www.tiktok.com',
  62. 'Connection': 'keep-alive',
  63. 'User-Agent': 'Mozilla/5.0',
  64. 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
  65. 'Accept-Language': 'en-us,en;q=0.5',
  66. 'Sec-Fetch-Mode': 'navigate',
  67. 'Accept-Encoding': 'gzip, deflate, br'
  68. }, platform);
  69. let {data: html, headers: htmlHeaders} = htmlResp;
  70. let match = html.match(/({"__DEFAULT_SCOPE__".*?)<\/script>/);
  71. if (!match || !match.length) {
  72. console.log('can not found JSON: scope');
  73. throw new Error('JSON not found: scope');
  74. }
  75. const scope = JSON.parse(match[1]);
  76. if (printable(platform)) {
  77. console.log(match[1]);
  78. console.log(scope);
  79. }
  80. const setCookie = parseSetCookie(htmlHeaders);
  81. const videoDetail = scope["__DEFAULT_SCOPE__"]["webapp.video-detail"]["itemInfo"]["itemStruct"];
  82. const bitrateInfos = videoDetail["video"]["bitrateInfo"];
  83. const formats = [];
  84. formats.push({
  85. "url": videoDetail["video"]["playAddr"],
  86. "headers": {
  87. "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.19 Safari/537.36",
  88. "Cookie": setCookie
  89. }
  90. })
  91. for (let bitrateInfo of bitrateInfos) {
  92. for (let url of bitrateInfo["PlayAddr"]["UrlList"]) {
  93. formats.push({
  94. "url": url,
  95. "headers": {
  96. "Cookie": setCookie
  97. }
  98. })
  99. }
  100. }
  101. const videoDetails = {
  102. "title": videoDetail["desc"],
  103. "thumbnail": videoDetail["video"]["cover"],
  104. "videoId": videoDetail["id"]
  105. }
  106. const ret = {
  107. "code": 200,
  108. "msg": "",
  109. "data": {
  110. "videoDetails": videoDetails,
  111. "streamingData": {
  112. "formats": formats
  113. }
  114. },
  115. "id": "DetailViewModel_detail_url"
  116. }
  117. console.log(`detail result: ${JSON.stringify(ret)}`);
  118. return ret;
  119. } catch (e) {
  120. const ret = {
  121. "code": -1,
  122. "msg": e.toString()
  123. }
  124. console.log(`detail result error: ${JSON.stringify(ret)}`);
  125. console.log(e);
  126. return ret;
  127. }
  128. }