Ejemplo n.º 1
0
int
ol_lrc_fetch_add_candidate (const OlMusicInfo *info,
                            OlLrcCandidate *candidate_list,
                            size_t count,
                            size_t size,
                            struct _OlLrcCandidate *new_candidate)
{
  ol_log_func ();
  if (info == NULL || candidate_list == NULL || new_candidate == NULL ||
      count < 0 || size <= 0)
    return 0;
  new_candidate->rank = ol_lrc_fetch_calc_rank (info, new_candidate);
  if (new_candidate->rank < RANK_ACCEPT_FACTOR * RANK_SCALE)
    return count;
  int pos = count;
  while (pos > 0 && new_candidate->rank > candidate_list[pos - 1].rank)
  {
    if (pos < size)
    {
      candidate_list[pos] = candidate_list[pos - 1];
    }
    pos--;
  }
  if (pos < size)
  {
    candidate_list[pos] = *new_candidate;
  }
  if (count < size)
    count++;
  return count;
}
Ejemplo n.º 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;
}