Exemple #1
0
int BarFlyOpen(BarFly_t* fly, PianoSong_t const* song,
		BarSettings_t const* settings)
{
	char const* const XML_STRING = "/xml/";

	int exit_status = 0;
	int status;
	BarFly_t output_fly;
	char* buffer = NULL;
	char* song_content_url = NULL;
	char* xml_pos;
	char* question_pos;
	int xml_len = strlen(XML_STRING);

	assert(fly != NULL);
	assert(song != NULL);
	assert(settings != NULL);

	/*
	 * Initialize the BarFly_t members.
	 */
	memset(&output_fly, 0, sizeof(BarFly_t));
	output_fly.audio_file = NULL;
	output_fly.completed = false;
	output_fly.status = NOT_RECORDING;

	/*
	 * Copy the artist, album, title, and audio format.
	 */
	strncpy(output_fly.artist, song->artist, BAR_FLY_NAME_LENGTH);
	output_fly.artist[BAR_FLY_NAME_LENGTH - 1] = '\0';
	strncpy(output_fly.album, song->album, BAR_FLY_NAME_LENGTH);
	output_fly.album[BAR_FLY_NAME_LENGTH - 1] = '\0';
	strncpy(output_fly.title, song->title, BAR_FLY_NAME_LENGTH);
	output_fly.title[BAR_FLY_NAME_LENGTH - 1] = '\0';
	output_fly.audio_format = song->audioFormat;

	/*
	 * Get the song content URL and extract the cover art URL.  The song
	 * content URL is the same as the song explorer URL except that "/xml" is
	 * replaced with "/content/".
	 */
	question_pos = strchr(song->songExplorerUrl, '?');
	if (question_pos == NULL) {
		question_pos = song->songExplorerUrl + strlen(song->songExplorerUrl);
	}

	xml_pos = strstr(song->songExplorerUrl, XML_STRING);
	if (xml_pos == NULL) {
		BarUiMsg(settings, MSG_DEBUG, "The song explorer URL did not contain "
				"the expected \"/xml/\" substring.  The cover art will not be "
				"added to the tag.\n");
		exit_status = -1;
	}

	if (xml_pos != NULL) {
		status = BarFlyasprintf(&song_content_url, "%.*s/content/%.*s",
				(int)(xml_pos - song->songExplorerUrl), song->songExplorerUrl,
				(int)(question_pos - xml_pos) - xml_len, xml_pos + xml_len);
		if (status == -1) {
			BarUiMsg(settings, MSG_DEBUG, "Error copying the song content URL.  "
					"The cover art will not be added to the tag.  (%d:%s)\n",
					errno, strerror(errno));
			exit_status = -1;
		}

		if (song_content_url != NULL) {
			status = _BarFlyFetchURL(song_content_url, (uint8_t**)&buffer, NULL,
						settings);
			if (status != 0) {
				BarUiMsg(settings, MSG_DEBUG, "Couldn't get the song content "
						"page.  The cover art will not be added to the tag.\n");
				exit_status = -1;
			}

			if (buffer != NULL) {
//				status = _BarFlyParseYear(album_buf, &output_fly.year,
//						settings);
//				if (status != 0) {
//					BarUiMsg(settings, MSG_DEBUG, "The album release year will "
//							"not be added to the tag.\n");
//					exit_status = -1;
//				}

				output_fly.cover_art_url = _BarFlyParseCoverArtURL(buffer,
						settings);
				if (output_fly.cover_art_url == NULL) {
					BarUiMsg(settings, MSG_DEBUG, "The cover art will not be "
							"addded to the tag.\n");
					exit_status = -1;
				}

				free(buffer);
				buffer = NULL;
			}
		}
	}

	/*
	 * Get the album explorer page and extract the track and disc numbers.
	 */
	status = _BarFlyFetchURL(song->albumExplorerUrl, (uint8_t**)&buffer,
			NULL, settings);
	if (status != 0) {
		BarUiMsg(settings, MSG_DEBUG, "Couldn't get the album explorer page.  "
				"The track and disc numbers will not be added to the tag.\n");
		exit_status = -1;
	}

	if (buffer != NULL) {
		status = _BarFlyParseTrackDisc(song->title, buffer,
				&output_fly.track, &output_fly.disc, settings);
		if (status != 0) {
			BarUiMsg(settings, MSG_DEBUG, "The track and disc numbers will not "
					"be added to the tag.\n");
			exit_status = -1;
		}
	}

	/*
	 * Get the path to the file.
	 */
	output_fly.audio_file_path = _BarFlyFileGetPath(song->artist, song->album,
			song->title, output_fly.year, output_fly.track, output_fly.disc,
			song->audioFormat, settings);
	if (output_fly.audio_file_path == NULL) {
		goto error;
	}
	
	/*
	 * Open a stream to the file.
	 */
	status = _BarFlyFileOpen(&output_fly.audio_file,
			output_fly.audio_file_path, settings);
	if (status == 0) {
		output_fly.status = RECORDING;
	} else if (status == -2) {
		output_fly.status = NOT_RECORDING_EXIST;
		output_fly.completed = true;
	} else {
		output_fly.completed = true;
		goto error;
	}

	/*
	 * All members of the BarFly_t structure were created successfully.  Copy
	 * them from the temporary structure to the one passed in.
	 */
	memcpy(fly, &output_fly, sizeof(BarFly_t));
	memset(&output_fly, 0, sizeof(BarFly_t));

	goto end;

error:
	exit_status = -1;

end:
	if (buffer != NULL) {
		free(buffer);
	}

	if (song_content_url != NULL) {
		free(song_content_url);
	}

	if (output_fly.audio_file != NULL) {
		fclose(output_fly.audio_file);
	}

	if (output_fly.audio_file_path != NULL) {
		free(output_fly.audio_file_path);
	}

	if (output_fly.cover_art_url != NULL) {
		free(output_fly.cover_art_url);
	}

	return exit_status;
}
Exemple #2
0
int BarFlyOpen(BarFly_t* fly, PianoSong_t const* song,
		BarSettings_t const* settings)
{
	int exit_status = 0;
	int status;
	BarFly_t output_fly;
	char* album_buf = NULL;

	assert(fly != NULL);
	assert(song != NULL);
	assert(settings != NULL);

	/*
	 * Initialize the BarFly_t members.
	 */
	memset(&output_fly, 0, sizeof(BarFly_t));
	output_fly.audio_file = NULL;
	output_fly.completed = false;
	output_fly.status = NOT_RECORDING;

	/*
	 * Copy the artist, album, title, and audio format.
	 */
	strncpy(output_fly.artist, song->artist, BAR_FLY_NAME_LENGTH);
	output_fly.artist[BAR_FLY_NAME_LENGTH - 1] = '\0';
	strncpy(output_fly.album, song->album, BAR_FLY_NAME_LENGTH);
	output_fly.album[BAR_FLY_NAME_LENGTH - 1] = '\0';
	strncpy(output_fly.title, song->title, BAR_FLY_NAME_LENGTH);
	output_fly.title[BAR_FLY_NAME_LENGTH - 1] = '\0';
	output_fly.audio_format = song->audioFormat;

	/*
	 * Get the album detail page and extract the year and cover art URL.
	 */
	status = _BarFlyFetchURL(song->albumDetailURL, (uint8_t**)&album_buf,
			NULL, settings);
	if (status != 0) {
		BarUiMsg(settings, MSG_DEBUG, "Couldn't get the album detail page.  "
				"The year and cover art will not be added to the tag.\n");
		exit_status = -1;
	}

	if (album_buf != NULL) {
		status = _BarFlyParseYear(album_buf, &output_fly.year, settings);
		if (status != 0) {
			BarUiMsg(settings, MSG_DEBUG, "The album release year will not be "
					"added to the tag.\n");
			exit_status = -1;
		}

		output_fly.cover_art_url = _BarFlyParseCoverArtURL(album_buf, settings);
		if (output_fly.cover_art_url == NULL) {
			BarUiMsg(settings, MSG_DEBUG, "The cover art will not be addded to the "
					"tag.\n");
			exit_status = -1;
		}

		free(album_buf);
		album_buf = NULL;
	}

	/*
	 * Get the album explorer page and extract the track and disc numbers.
	 */
	status = _BarFlyFetchURL(song->albumExplorerUrl, (uint8_t**)&album_buf,
			NULL, settings);
	if (status != 0) {
		BarUiMsg(settings, MSG_DEBUG, "Couldn't get the album explorer page.  "
				"The track and disc numbers will not be added to the tag.\n");
		exit_status = -1;
	}

	if (album_buf != NULL) {
		status = _BarFlyParseTrackDisc(song->title, album_buf,
				&output_fly.track, &output_fly.disc, settings);
		if (status != 0) {
			BarUiMsg(settings, MSG_DEBUG, "The track and disc numbers will not "
					"be added to the tag.\n");
			exit_status = -1;
		}
	}

	/*
	 * Get the path to the file.
	 */
	output_fly.audio_file_path = _BarFlyFileGetPath(song->artist, song->album,
			song->title, output_fly.year, output_fly.track, output_fly.disc,
			song->audioFormat, settings);
	if (output_fly.audio_file_path == NULL) {
		goto error;
	}
	
	/*
	 * Open a stream to the file.
	 */
	status = _BarFlyFileOpen(&output_fly.audio_file,
			output_fly.audio_file_path, settings);
	if (status == 0) {
		output_fly.status = RECORDING;
	} else if (status == -2) {
		output_fly.status = NOT_RECORDING_EXIST;
		output_fly.completed = true;
	} else {
		output_fly.completed = true;
		goto error;
	}

	/*
	 * All members of the BarFly_t structure were created successfully.  Copy
	 * them from the temporary structure to the one passed in.
	 */
	memcpy(fly, &output_fly, sizeof(BarFly_t));
	memset(&output_fly, 0, sizeof(BarFly_t));

	goto end;

error:
	exit_status = -1;

end:
	if (album_buf != NULL) {
		free(album_buf);
	}

	if (output_fly.audio_file != NULL) {
		fclose(output_fly.audio_file);
	}

	if (output_fly.audio_file_path != NULL) {
		free(output_fly.audio_file_path);
	}

	if (output_fly.cover_art_url != NULL) {
		free(output_fly.cover_art_url);
	}

	return exit_status;
}