pornhub.js 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. console.log('bundle2!')
  2. printable = (platform) => {
  3. return platform === "WEB";
  4. }
  5. request = async (method, url, data = null, headers = {}, platform) => {
  6. if (platform === "WEB") {
  7. url = url.replace("https://cn.pornhub.com/", "http://127.0.0.1:81/");
  8. }
  9. console.log(`request url:${url}`)
  10. console.log(`request data:${data}`)
  11. console.log(`request method:${method}`)
  12. console.log(`request headers:${JSON.stringify((headers))}`)
  13. if (platform === "WEB") {
  14. const res = await fetch(url, {
  15. 'mode': 'cors',
  16. 'method': method,
  17. 'headers': headers,
  18. 'body': data
  19. })
  20. const resData = await res.text()
  21. return Promise.resolve({
  22. 'data': resData,
  23. 'headers': res.headers
  24. });
  25. }
  26. return new Promise((resolve, reject) => {
  27. AF.request(url, method, data, headers, (data, headers, err) => {
  28. if (err) {
  29. reject(err);
  30. } else {
  31. console.log(`response headers: ${headers}`);
  32. resolve({
  33. 'data': data,
  34. 'headers': JSON.parse(headers)
  35. });
  36. }
  37. });
  38. })
  39. }
  40. detail = async (url, platform) => {
  41. try {
  42. const htmlResp = await request('GET', url, null, {}, platform);
  43. let {data: html, headers: htmlHeaders} = htmlResp;
  44. let match = html.match(/var flashvars_[^ ]* = ({.*?});/);
  45. if (!match || !match.length) {
  46. console.log('can not found JSON: flashVars');
  47. throw new Error('JSON not found: flashVars');
  48. }
  49. const flashVars = JSON.parse(match[1]);
  50. if (printable(platform)) {
  51. console.log(flashVars);
  52. }
  53. const mediaDefinitions = flashVars['mediaDefinitions'];
  54. const formats = [];
  55. for (const item of mediaDefinitions) {
  56. const format = {
  57. 'url': item['videoUrl'],
  58. 'format': item["format"],
  59. "quality": item["quality"],
  60. }
  61. formats.push(format);
  62. }
  63. const videoDetails = {
  64. "title": flashVars["video_title"],
  65. "thumbnail": flashVars["image_url"],
  66. "videoId": flashVars["link_url"].match(/viewkey=(.*)/)[1],
  67. "duration": flashVars["video_duration"],
  68. }
  69. const ret = {
  70. "code": 200,
  71. "msg": "",
  72. "data": {
  73. "videoDetails": videoDetails,
  74. "streamingData": {
  75. "formats": formats
  76. }
  77. },
  78. "id": "DetailViewModel_detail_url"
  79. }
  80. console.log(`detail result: ${JSON.stringify(ret)}`);
  81. return ret;
  82. } catch (e) {
  83. const ret = {
  84. "code": -1,
  85. "msg": e.toString()
  86. }
  87. console.log(`detail result error: ${JSON.stringify(ret)}`);
  88. console.log(e);
  89. return ret;
  90. }
  91. }