示例#1
0
char * 
ol_lrc_candidate_deserialize (OlLrcCandidate *candidate,
                              char *data)
{
  ol_assert_ret (candidate != NULL, 0);
  ol_assert_ret (data != NULL, 0);
  char *title, *artist, *url, *rank, *ret;
  title = artist = url = rank = ret = NULL;
  title = data;
  artist = ol_split_a_line (title);
  if (artist != NULL)
    url = ol_split_a_line (artist);
  if (url != NULL)
    rank = ol_split_a_line (url);
  if (rank != NULL)
    ret = ol_split_a_line (rank);
  if (ret != NULL)
  {
    ol_lrc_candidate_set_title (candidate, title);
    ol_lrc_candidate_set_artist (candidate, artist);
    ol_lrc_candidate_set_url (candidate, url);
    int rank_int = 0;
    sscanf (rank, "%d", &rank_int);
    ol_lrc_candidate_set_rank (candidate, rank_int);
  }
  return ret;
}
示例#2
0
static OlLrcCandidate *
ol_lrc_fetch_xiami_search(const OlMusicInfo *info, int *size, const char* charset)
{
  ol_assert_ret (info != NULL, NULL);
  ol_debugf ("  title: %s\n"
             "  artist: %s\n"
             "  album: %s\n",
             info->title,
             info->artist,
             info->album);
  OlLrcCandidate *ret = NULL;
  static OlLrcCandidate candidate_list[TRY_MATCH_MAX];
  OlLrcCandidate candidate;
  int result;
  int count = 0;
  if (info->title == NULL && info->artist == NULL)
    return NULL;

  GString *url = g_string_new (PREFIX_SEARCH_URL);
  if (ol_music_info_get_title (info) != NULL)
    g_string_append_uri_escaped (url,
                                 ol_music_info_get_title (info),
                                 NULL,
                                 FALSE);
  if (ol_music_info_get_artist (info) != NULL)
  {
    g_string_append_c (url, '+');
    g_string_append_uri_escaped (url,
                                 ol_music_info_get_artist (info),
                                 NULL,
                                 FALSE);
  }

  char *search_url = g_string_free (url, FALSE);
  ol_debugf ("Search url: %s\n", search_url);

  struct memo content;
  content.mem_base = NULL;
  content.mem_len = 0;

  if((result = fetch_into_memory(search_url,
                                 NULL, /* refer */
                                 NULL, /* user-agent */
                                 NULL, /* postdata */
                                 0,    /* postdata len */
                                 &content)) >= 0)
  {
    char *current = (char*)content.mem_base;
    char *p;
    while ((p = strstr (current, TITLE_PATTERN)) != NULL)
    {
      current = p + strlen (TITLE_PATTERN);
      if (!isdigit(*current))
        continue;
      p = strchr (current, '\"');
      if (p == NULL)
        break;
      *p = '\0';
      ol_debugf ("url id: %s\n", current);
      char *id = g_strdup (current);
      current = p + 1;
      char *title = _get_title_attr (current, &current);
      if (title == NULL)
        break;
      ol_lrc_candidate_set_title (&candidate, title);
      ol_debugf ("title: %s\n", title);
      g_free (title);
      p = strstr (current, ARTIST_PATTERN);
      if (p != NULL)
      {
        char *artist = _get_title_attr (p, &current);
        if (artist != NULL)
        {
          ol_lrc_candidate_set_artist (&candidate, artist);
          ol_debugf ("artist: %s\n", artist);
          g_free (artist);
        }
      }

      if (ol_lrc_fetch_calc_rank (info, &candidate) >= LRC_RANK_THRESHOLD)
      {
        char *url = _get_lyric_url (id);
        ol_debugf ("lyric url: %s\n", url);
        if (url != NULL)
        {
          ol_lrc_candidate_set_url (&candidate, url);
          count = ol_lrc_fetch_add_candidate (info,
                                              candidate_list,
                                              count,
                                              TRY_MATCH_MAX,
                                              &candidate);
          g_free (url);
        }
      }
      g_free (id);
    }
    free(content.mem_base);
    if (count > 0)
      ret = candidate_list;
  }
  *size = count;
  g_free (search_url);
  return ret;
}