summaryrefslogtreecommitdiff
path: root/src/extractors/haokan.c
blob: b3e943ddcc6e34a9e2adb4da86aef0c7ec07c8ec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include "haokan.h"
#include "../logger.h"
#include "../process_url.h"
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void haokan_extract(Options *options) {
  char *resp, *title, *videoURL;
  if (!get(options->URL, &resp)) {
    const char *patterns_str[2] = {"<div class='ssr-video-title\'>(.*?)</div>",
                                   "\"playurl\":\"(http.+?)\""};
    const str_array_t patterns = {(char **)patterns_str, 2};
    str_array_t results = create_str_array(0);
    int r = regex_match(resp, patterns, &results);
    // Should match exactly two results in HTML, otherwise error out.
    if (!r && results.n == 2) {
      // for (unsigned short i = 0; i < results.n; i++) {
      //   DEBUG_PRINT("%s\n", results.str[i]);
      // }
      title = results.str[0];
      substitute_str(results.str[1], "\\\\/", "/", &videoURL);
      DEBUG_PRINT("title: %s\n", title);
      DEBUG_PRINT("videoURL: %s\n", videoURL);

      char *ct = NULL;
      get_info(videoURL, NULL, &ct, NULL);
      if (ct == NULL) {
        goto end;
      }
      const char *ext = mimeType2ext(ct);
      char *filename = malloc(strlen(title) + strlen(ct) + 2);
      sprintf(filename, "%s.%s", title, ext);

      set_referer("https://haokan.baidu.com");
      add_url(videoURL, NULL, filename, NULL, NULL);

    end:
      free_str_array(&results);
      FREE_AND_NULLIFY(filename);
      FREE_AND_NULLIFY(videoURL);
      return;
    }
    free_str_array(&results);
  }
  LOG("haokan", "Download failed.\n");
}