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 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913
|
/*
Copyright (C) 2007 Bastien Nocera <hadess@hadess.net>
The Gnome Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
The Gnome Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with the Gnome Library; see the file COPYING.LIB. If not,
write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301 USA.
Author: Bastien Nocera <hadess@hadess.net>
*/
#include "config.h"
#include <string.h>
#include <glib.h>
#ifndef TOTEM_PL_PARSER_MINI
#include <libsoup/soup.h>
#include "xmlparser.h"
#include "totem-pl-parser.h"
#include "totem-disc.h"
#endif /* !TOTEM_PL_PARSER_MINI */
#include "totem-pl-parser-mini.h"
#include "totem-pl-parser-podcast.h"
#include "totem-pl-parser-videosite.h"
#include "totem-pl-parser-private.h"
#define RSS_NEEDLE "<rss "
#define RSS_NEEDLE2 "<rss\n"
#define ATOM_NEEDLE "<feed "
#define OPML_NEEDLE "<opml "
const char *
totem_pl_parser_is_rss (const char *data, gsize len)
{
if (len == 0)
return FALSE;
if (len > MIME_READ_CHUNK_SIZE)
len = MIME_READ_CHUNK_SIZE;
if (g_strstr_len (data, len, RSS_NEEDLE) != NULL)
return RSS_MIME_TYPE;
if (g_strstr_len (data, len, RSS_NEEDLE2) != NULL)
return RSS_MIME_TYPE;
return NULL;
}
const char *
totem_pl_parser_is_atom (const char *data, gsize len)
{
if (len == 0)
return FALSE;
if (len > MIME_READ_CHUNK_SIZE)
len = MIME_READ_CHUNK_SIZE;
if (g_strstr_len (data, len, ATOM_NEEDLE) != NULL)
return ATOM_MIME_TYPE;
return NULL;
}
const char *
totem_pl_parser_is_opml (const char *data, gsize len)
{
if (len == 0)
return FALSE;
if (len > MIME_READ_CHUNK_SIZE)
len = MIME_READ_CHUNK_SIZE;
if (g_strstr_len (data, len, OPML_NEEDLE) != NULL)
return OPML_MIME_TYPE;
return NULL;
}
const char *
totem_pl_parser_is_xml_feed (const char *data, gsize len)
{
if (totem_pl_parser_is_rss (data, len) != NULL)
return RSS_MIME_TYPE;
if (totem_pl_parser_is_atom (data, len) != NULL)
return ATOM_MIME_TYPE;
if (totem_pl_parser_is_opml (data, len) != NULL)
return OPML_MIME_TYPE;
return NULL;
}
#ifndef TOTEM_PL_PARSER_MINI
static gboolean
is_image (const char *url)
{
char *content_type;
gboolean retval = FALSE;
content_type = g_content_type_guess (url, NULL, 0, NULL);
if (content_type == NULL)
return FALSE;
if (g_content_type_is_a (content_type, "image/*"))
retval = TRUE;
g_free (content_type);
return retval;
}
static TotemPlParserResult
parse_rss_item (TotemPlParser *parser, xml_node_t *parent)
{
const char *title, *uri, *description, *author, *img;
const char *pub_date, *duration, *filesize, *content_type, *id;
xml_node_t *node;
title = uri = description = author = content_type = NULL;
img = pub_date = duration = filesize = id = NULL;
for (node = parent->child; node != NULL; node = node->next) {
if (node->name == NULL)
continue;
if (g_ascii_strcasecmp (node->name, "title") == 0) {
title = node->data;
} else if (g_ascii_strcasecmp (node->name, "url") == 0) {
uri = node->data;
} else if (g_ascii_strcasecmp (node->name, "pubDate") == 0) {
pub_date = node->data;
} else if (g_ascii_strcasecmp (node->name, "guid") == 0) {
id = node->data;
} else if (g_ascii_strcasecmp (node->name, "description") == 0
|| g_ascii_strcasecmp (node->name, "itunes:summary") == 0) {
description = node->data;
} else if (g_ascii_strcasecmp (node->name, "author") == 0
|| g_ascii_strcasecmp (node->name, "itunes:author") == 0) {
author = node->data;
} else if (g_ascii_strcasecmp (node->name, "itunes:duration") == 0) {
duration = node->data;
} else if (g_ascii_strcasecmp (node->name, "length") == 0) {
filesize = node->data;
} else if (g_ascii_strcasecmp (node->name, "media:content") == 0) {
const char *tmp;
tmp = xml_parser_get_property (node, "type");
if (tmp != NULL &&
g_str_has_prefix (tmp, "audio/") == FALSE) {
if (g_str_has_prefix (tmp, "image/"))
img = xml_parser_get_property (node, "url");
continue;
}
content_type = tmp;
tmp = xml_parser_get_property (node, "url");
if (tmp != NULL)
uri = tmp;
else
continue;
tmp = xml_parser_get_property (node, "fileSize");
if (tmp != NULL)
filesize = tmp;
tmp = xml_parser_get_property (node, "duration");
if (tmp != NULL)
duration = tmp;
} else if (g_ascii_strcasecmp (node->name, "enclosure") == 0) {
const char *tmp;
tmp = xml_parser_get_property (node, "url");
if (tmp != NULL && is_image (tmp) == FALSE)
uri = tmp;
else
continue;
tmp = xml_parser_get_property (node, "length");
if (tmp != NULL)
filesize = tmp;
} else if (g_ascii_strcasecmp (node->name, "link") == 0 &&
totem_pl_parser_is_videosite (node->data, FALSE) != FALSE) {
uri = node->data;
}
}
if (id != NULL &&
uri == NULL &&
totem_pl_parser_is_videosite (id, FALSE) != FALSE)
uri = id;
if (uri != NULL) {
totem_pl_parser_add_uri (parser,
TOTEM_PL_PARSER_FIELD_URI, uri,
TOTEM_PL_PARSER_FIELD_ID, id,
TOTEM_PL_PARSER_FIELD_TITLE, title,
TOTEM_PL_PARSER_FIELD_PUB_DATE, pub_date,
TOTEM_PL_PARSER_FIELD_DESCRIPTION, description,
TOTEM_PL_PARSER_FIELD_AUTHOR, author,
TOTEM_PL_PARSER_FIELD_DURATION, duration,
TOTEM_PL_PARSER_FIELD_FILESIZE, filesize,
TOTEM_PL_PARSER_FIELD_CONTENT_TYPE, content_type,
TOTEM_PL_PARSER_FIELD_IMAGE_URI, img,
NULL);
}
return TOTEM_PL_PARSER_RESULT_SUCCESS;
}
static TotemPlParserResult
parse_rss_items (TotemPlParser *parser, const char *uri, xml_node_t *parent)
{
const char *title, *language, *description, *author;
const char *contact, *img, *pub_date, *copyright;
xml_node_t *node;
title = language = description = author = NULL;
contact = img = pub_date = copyright = NULL;
/* We need to parse for the feed metadata first, then for the items */
for (node = parent->child; node != NULL; node = node->next) {
if (node->name == NULL)
continue;
if (g_ascii_strcasecmp (node->name, "title") == 0) {
title = node->data;
} else if (g_ascii_strcasecmp (node->name, "language") == 0) {
language = node->data;
} else if (g_ascii_strcasecmp (node->name, "description") == 0
|| g_ascii_strcasecmp (node->name, "itunes:subtitle") == 0) {
description = node->data;
} else if (g_ascii_strcasecmp (node->name, "author") == 0
|| g_ascii_strcasecmp (node->name, "itunes:author") == 0
|| (g_ascii_strcasecmp (node->name, "generator") == 0 && author == NULL)) {
author = node->data;
} else if (g_ascii_strcasecmp (node->name, "webMaster") == 0) {
contact = node->data;
} else if (g_ascii_strcasecmp (node->name, "image") == 0) {
img = node->data;
} else if (g_ascii_strcasecmp (node->name, "itunes:image") == 0) {
const char *href;
href = xml_parser_get_property (node, "href");
if (href != NULL)
img = href;
} else if (g_ascii_strcasecmp (node->name, "lastBuildDate") == 0
|| g_ascii_strcasecmp (node->name, "pubDate") == 0) {
pub_date = node->data;
} else if (g_ascii_strcasecmp (node->name, "copyright") == 0) {
copyright = node->data;
}
}
/* Send the info we already have about the feed */
totem_pl_parser_add_uri (parser,
TOTEM_PL_PARSER_FIELD_IS_PLAYLIST, TRUE,
TOTEM_PL_PARSER_FIELD_URI, uri,
TOTEM_PL_PARSER_FIELD_TITLE, title,
TOTEM_PL_PARSER_FIELD_LANGUAGE, language,
TOTEM_PL_PARSER_FIELD_DESCRIPTION, description,
TOTEM_PL_PARSER_FIELD_AUTHOR, author,
TOTEM_PL_PARSER_FIELD_PUB_DATE, pub_date,
TOTEM_PL_PARSER_FIELD_COPYRIGHT, copyright,
TOTEM_PL_PARSER_FIELD_IMAGE_URI, img,
TOTEM_PL_PARSER_FIELD_CONTACT, contact,
NULL);
for (node = parent->child; node != NULL; node = node->next) {
if (node->name == NULL)
continue;
if (g_ascii_strcasecmp (node->name, "item") == 0)
parse_rss_item (parser, node);
}
totem_pl_parser_playlist_end (parser, uri);
return TOTEM_PL_PARSER_RESULT_SUCCESS;
}
TotemPlParserResult
totem_pl_parser_add_rss (TotemPlParser *parser,
GFile *file,
GFile *base_file,
TotemPlParseData *parse_data,
gpointer data)
{
#ifndef HAVE_GMIME
WARN_NO_GMIME;
#else
xml_node_t* doc, *channel;
char *contents;
gsize size;
if (g_file_load_contents (file, NULL, &contents, &size, NULL, NULL) == FALSE)
return TOTEM_PL_PARSER_RESULT_ERROR;
doc = totem_pl_parser_parse_xml_relaxed (contents, size);
if (doc == NULL) {
g_free (contents);
return TOTEM_PL_PARSER_RESULT_ERROR;
}
/* If the document has no name */
if (doc->name == NULL
|| (g_ascii_strcasecmp (doc->name , "rss") != 0
&& g_ascii_strcasecmp (doc->name , "rss\n") != 0)) {
g_free (contents);
xml_parser_free_tree (doc);
return TOTEM_PL_PARSER_RESULT_ERROR;
}
for (channel = doc->child; channel != NULL; channel = channel->next) {
if (g_ascii_strcasecmp (channel->name, "channel") == 0) {
char *uri;
uri = g_file_get_uri (file);
parse_rss_items (parser, uri, channel);
g_free (uri);
/* One channel per file */
break;
}
}
g_free (contents);
xml_parser_free_tree (doc);
return TOTEM_PL_PARSER_RESULT_SUCCESS;
#endif /* !HAVE_GMIME */
}
/* http://www.apple.com/itunes/store/podcaststechspecs.html */
TotemPlParserResult
totem_pl_parser_add_itpc (TotemPlParser *parser,
GFile *file,
GFile *base_file,
TotemPlParseData *parse_data,
gpointer data)
{
#ifndef HAVE_GMIME
WARN_NO_GMIME;
#else
TotemPlParserResult ret;
char *uri, *new_uri, *uri_scheme;
GFile *new_file;
uri = g_file_get_uri (file);
uri_scheme = g_file_get_uri_scheme (file);
new_uri = g_strdup_printf ("http%s", uri + strlen (uri_scheme));
g_free (uri);
g_free (uri_scheme);
new_file = g_file_new_for_uri (new_uri);
g_free (new_uri);
ret = totem_pl_parser_add_rss (parser, new_file, base_file, parse_data, data);
g_object_unref (new_file);
return ret;
#endif /* !HAVE_GMIME */
}
TotemPlParserResult
totem_pl_parser_add_zune (TotemPlParser *parser,
GFile *file,
GFile *base_file,
TotemPlParseData *parse_data,
gpointer data)
{
#ifndef HAVE_GMIME
WARN_NO_GMIME;
#else
TotemPlParserResult ret;
char *uri, *new_uri;
GFile *new_file;
uri = g_file_get_uri (file);
if (g_str_has_prefix (uri, "zune://subscribe/?") == FALSE) {
g_free (uri);
return TOTEM_PL_PARSER_RESULT_UNHANDLED;
}
new_uri = strchr (uri + strlen ("zune://subscribe/?"), '=');
if (new_uri == NULL) {
g_free (uri);
return TOTEM_PL_PARSER_RESULT_UNHANDLED;
}
/* Skip over the '=' */
new_uri++;
new_file = g_file_new_for_uri (new_uri);
g_free (uri);
ret = totem_pl_parser_add_rss (parser, new_file, base_file, parse_data, data);
g_object_unref (new_file);
return ret;
#endif /* !HAVE_GMIME */
}
/* Atom docs:
* http://www.atomenabled.org/developers/syndication/atom-format-spec.php#rfc.section.4.1
* http://tools.ietf.org/html/rfc4287
* http://tools.ietf.org/html/rfc4946 */
static TotemPlParserResult
parse_atom_entry (TotemPlParser *parser, xml_node_t *parent)
{
const char *title, *author, *uri, *filesize;
const char *copyright, *pub_date, *description;
xml_node_t *node;
title = author = uri = filesize = NULL;
copyright = pub_date = description = NULL;
for (node = parent->child; node != NULL; node = node->next) {
if (node->name == NULL)
continue;
if (g_ascii_strcasecmp (node->name, "title") == 0) {
title = node->data;
} else if (g_ascii_strcasecmp (node->name, "author") == 0) {
//FIXME
} else if (g_ascii_strcasecmp (node->name, "link") == 0) {
const char *rel;
//FIXME how do we choose the default enclosure type?
rel = xml_parser_get_property (node, "rel");
if (g_ascii_strcasecmp (rel, "enclosure") == 0) {
const char *href;
//FIXME what's the difference between url and href there?
href = xml_parser_get_property (node, "href");
if (href == NULL)
continue;
uri = href;
filesize = xml_parser_get_property (node, "length");
} else if (g_ascii_strcasecmp (node->name, "license") == 0) {
const char *href;
href = xml_parser_get_property (node, "href");
if (href == NULL)
continue;
/* This isn't really a copyright, but what the hey */
copyright = href;
}
} else if (g_ascii_strcasecmp (node->name, "updated") == 0
|| (g_ascii_strcasecmp (node->name, "modified") == 0 && pub_date == NULL)) {
pub_date = node->data;
} else if (g_ascii_strcasecmp (node->name, "summary") == 0
|| (g_ascii_strcasecmp (node->name, "content") == 0 && description == NULL)) {
const char *type;
type = xml_parser_get_property (node, "content");
if (type != NULL && g_ascii_strcasecmp (type, "text/plain") == 0)
description = node->data;
}
//FIXME handle category
}
if (uri != NULL) {
totem_pl_parser_add_uri (parser,
TOTEM_PL_PARSER_FIELD_TITLE, title,
TOTEM_PL_PARSER_FIELD_AUTHOR, author,
TOTEM_PL_PARSER_FIELD_URI, uri,
TOTEM_PL_PARSER_FIELD_FILESIZE, filesize,
TOTEM_PL_PARSER_FIELD_COPYRIGHT, copyright,
TOTEM_PL_PARSER_FIELD_PUB_DATE, pub_date,
TOTEM_PL_PARSER_FIELD_DESCRIPTION, description,
NULL);
}
return TOTEM_PL_PARSER_RESULT_SUCCESS;
}
static TotemPlParserResult
parse_atom_entries (TotemPlParser *parser, const char *uri, xml_node_t *parent)
{
const char *title, *pub_date, *description;
const char *author, *img;
xml_node_t *node;
gboolean started = FALSE;
title = pub_date = description = NULL;
author = img = NULL;
for (node = parent->child; node != NULL; node = node->next) {
if (node->name == NULL)
continue;
if (g_ascii_strcasecmp (node->name, "title") == 0) {
title = node->data;
} else if (g_ascii_strcasecmp (node->name, "tagline") == 0) {
description = node->data;
} else if (g_ascii_strcasecmp (node->name, "modified") == 0
|| g_ascii_strcasecmp (node->name, "updated") == 0) {
pub_date = node->data;
} else if (g_ascii_strcasecmp (node->name, "author") == 0
|| (g_ascii_strcasecmp (node->name, "generator") == 0 && author == NULL)) {
author = node->data;
} else if ((g_ascii_strcasecmp (node->name, "icon") == 0 && img == NULL)
|| g_ascii_strcasecmp (node->name, "logo") == 0) {
img = node->data;
}
if (g_ascii_strcasecmp (node->name, "entry") == 0) {
if (started == FALSE) {
/* Send the info we already have about the feed */
totem_pl_parser_add_uri (parser,
TOTEM_PL_PARSER_FIELD_IS_PLAYLIST, TRUE,
TOTEM_PL_PARSER_FIELD_URI, uri,
TOTEM_PL_PARSER_FIELD_TITLE, title,
TOTEM_PL_PARSER_FIELD_DESCRIPTION, description,
TOTEM_PL_PARSER_FIELD_AUTHOR, author,
TOTEM_PL_PARSER_FIELD_PUB_DATE, pub_date,
TOTEM_PL_PARSER_FIELD_IMAGE_URI, img,
NULL);
started = TRUE;
}
parse_atom_entry (parser, node);
}
}
totem_pl_parser_playlist_end (parser, uri);
return TOTEM_PL_PARSER_RESULT_SUCCESS;
}
TotemPlParserResult
totem_pl_parser_add_atom (TotemPlParser *parser,
GFile *file,
GFile *base_file,
TotemPlParseData *parse_data,
gpointer data)
{
#ifndef HAVE_GMIME
WARN_NO_GMIME;
#else
xml_node_t* doc;
char *contents, *uri;
gsize size;
if (g_file_load_contents (file, NULL, &contents, &size, NULL, NULL) == FALSE)
return TOTEM_PL_PARSER_RESULT_ERROR;
doc = totem_pl_parser_parse_xml_relaxed (contents, size);
if (doc == NULL) {
g_free (contents);
return TOTEM_PL_PARSER_RESULT_ERROR;
}
/* If the document has no name */
if (doc->name == NULL
|| g_ascii_strcasecmp (doc->name , "feed") != 0) {
g_free (contents);
xml_parser_free_tree (doc);
return TOTEM_PL_PARSER_RESULT_ERROR;
}
uri = g_file_get_uri (file);
parse_atom_entries (parser, uri, doc);
g_free (uri);
g_free (contents);
xml_parser_free_tree (doc);
return TOTEM_PL_PARSER_RESULT_SUCCESS;
#endif /* !HAVE_GMIME */
}
TotemPlParserResult
totem_pl_parser_add_xml_feed (TotemPlParser *parser,
GFile *file,
GFile *base_file,
TotemPlParseData *parse_data,
gpointer data)
{
#ifndef HAVE_GMIME
WARN_NO_GMIME;
#else
guint len;
if (data == NULL)
return TOTEM_PL_PARSER_RESULT_UNHANDLED;
len = strlen (data);
if (totem_pl_parser_is_rss (data, len) != FALSE)
return totem_pl_parser_add_rss (parser, file, base_file, parse_data, data);
if (totem_pl_parser_is_atom (data, len) != FALSE)
return totem_pl_parser_add_atom (parser, file, base_file, parse_data, data);
if (totem_pl_parser_is_opml (data, len) != FALSE)
return totem_pl_parser_add_opml (parser, file, base_file, parse_data, data);
return TOTEM_PL_PARSER_RESULT_UNHANDLED;
#endif /* !HAVE_GMIME */
}
static GByteArray *
totem_pl_parser_load_http_itunes (const char *uri,
gboolean debug)
{
SoupMessage *msg;
SoupSession *session;
GByteArray *data;
if (debug)
g_print ("Loading ITMS playlist '%s'\n", uri);
session = soup_session_sync_new_with_options (
SOUP_SESSION_ADD_FEATURE_BY_TYPE, SOUP_TYPE_CONTENT_DECODER,
SOUP_SESSION_USER_AGENT, "iTunes/10.0.0",
SOUP_SESSION_ACCEPT_LANGUAGE_AUTO, TRUE,
NULL);
msg = soup_message_new (SOUP_METHOD_GET, uri);
soup_session_send_message (session, msg);
if (SOUP_STATUS_IS_SUCCESSFUL (msg->status_code)) {
data = g_byte_array_new ();
g_byte_array_append (data,
(guchar *) msg->response_body->data,
msg->response_body->length);
} else {
return NULL;
}
g_object_unref (msg);
g_object_unref (session);
return data;
}
static const char *
totem_pl_parser_parse_plist (xml_node_t *item)
{
for (item = item->child; item != NULL; item = item->next) {
/* What we're looking for looks like:
* <key>action</key>
* <dict>
* <key>kind</key><string>Goto</string>
* <key>url</key><string>URL</string>
*/
if (g_ascii_strcasecmp (item->name, "key") == 0
&& g_ascii_strcasecmp (item->data, "action") == 0) {
xml_node_t *node = item->next;
if (node && g_ascii_strcasecmp (node->name, "[CDATA]") == 0)
node = node->next;
if (node && g_ascii_strcasecmp (node->name, "dict") == 0) {
for (node = node->child; node != NULL; node = node->next) {
if (g_ascii_strcasecmp (node->name, "key") == 0 &&
g_ascii_strcasecmp (node->data, "url") == 0 &&
node->next != NULL) {
node = node->next;
if (g_ascii_strcasecmp (node->name, "string") == 0)
return node->data;
}
}
}
} else {
const char *ret;
ret = totem_pl_parser_parse_plist (item);
if (ret != NULL)
return ret;
}
}
return NULL;
}
static char *
totem_pl_parser_parse_html (char *data, gsize len, gboolean debug)
{
char *s, *end;
s = g_strstr_len (data, len, "feed-url=\"");
if (s == NULL)
return NULL;
s += strlen ("feed-url=\"");
if (*s == '\0')
return NULL;
end = g_strstr_len (s, len - (s - data), "\"");
if (end == NULL)
return NULL;
return g_strndup (s, end - s);
}
static GFile *
totem_pl_parser_get_feed_uri (char *data, gsize len, gboolean debug)
{
xml_node_t* doc;
const char *uri;
GFile *ret = NULL;
GByteArray *content;
uri = NULL;
/* Probably HTML, look for feed-url */
if (g_strstr_len (data, len, "feed-url") != NULL) {
char *feed_uri;
feed_uri = totem_pl_parser_parse_html (data, len, debug);
if (debug)
g_print ("Found feed-url in HTML: '%s'\n", feed_uri);
if (feed_uri == NULL)
return NULL;
ret = g_file_new_for_uri (feed_uri);
g_free (feed_uri);
return ret;
}
doc = totem_pl_parser_parse_xml_relaxed (data, len);
if (doc == NULL)
return NULL;
/* If the document has no name */
if (doc->name == NULL || g_ascii_strcasecmp (doc->name, "plist") != 0)
goto out;
/* Redirect plist? Find a goto action */
uri = totem_pl_parser_parse_plist (doc);
if (debug)
g_print ("Found redirect URL: %s\n", uri);
if (uri == NULL)
goto out;
content = totem_pl_parser_load_http_itunes (uri, debug);
if (!content)
goto out;
ret = totem_pl_parser_get_feed_uri ((char *) content->data, content->len, debug);
g_byte_array_free (content, TRUE);
out:
xml_parser_free_tree (doc);
return ret;
}
TotemPlParserResult
totem_pl_parser_add_itms (TotemPlParser *parser,
GFile *file,
GFile *base_file,
TotemPlParseData *parse_data,
gpointer data)
{
#ifndef HAVE_GMIME
WARN_NO_GMIME;
#else
GByteArray *content;
char *itms_uri;
GFile *feed_file;
TotemPlParserResult ret;
if (g_file_has_uri_scheme (file, "itms") != FALSE ||
g_file_has_uri_scheme (file, "itmss") != FALSE) {
itms_uri= g_file_get_uri (file);
memcpy (itms_uri, "http", 4);
} else if (g_file_has_uri_scheme (file, "http") != FALSE) {
itms_uri = g_file_get_uri (file);
} else {
return TOTEM_PL_PARSER_RESULT_ERROR;
}
/* Load the file using iTunes user-agent */
content = totem_pl_parser_load_http_itunes (itms_uri, totem_pl_parser_is_debugging_enabled (parser));
/* And look in the file for the feedURL */
feed_file = totem_pl_parser_get_feed_uri ((char *) content->data, content->len,
totem_pl_parser_is_debugging_enabled (parser));
g_byte_array_free (content, TRUE);
if (feed_file == NULL)
return TOTEM_PL_PARSER_RESULT_ERROR;
DEBUG(feed_file, g_print ("Found feed URI: %s\n", uri));
ret = totem_pl_parser_add_rss (parser, feed_file, NULL, parse_data, NULL);
g_object_unref (feed_file);
return ret;
#endif /* !HAVE_GMIME */
}
gboolean
totem_pl_parser_is_itms_feed (GFile *file)
{
char *uri;
g_return_val_if_fail (file != NULL, FALSE);
uri = g_file_get_uri (file);
if (g_file_has_uri_scheme (file, "itms") != FALSE ||
g_file_has_uri_scheme (file, "itmss") != FALSE ||
(g_file_has_uri_scheme (file, "http") != FALSE &&
strstr (uri, ".apple.com/") != FALSE)) {
if (strstr (uri, "/podcast/") != NULL ||
strstr (uri, "viewPodcast") != NULL) {
g_free (uri);
return TRUE;
}
}
g_free (uri);
return FALSE;
}
static TotemPlParserResult
parse_opml_outline (TotemPlParser *parser, xml_node_t *parent)
{
xml_node_t* node;
for (node = parent->child; node != NULL; node = node->next) {
const char *title, *uri;
if (node->name == NULL || g_ascii_strcasecmp (node->name, "outline") != 0)
continue;
uri = xml_parser_get_property (node, "xmlUrl");
title = xml_parser_get_property (node, "text");
if (uri == NULL)
continue;
totem_pl_parser_add_uri (parser,
TOTEM_PL_PARSER_FIELD_TITLE, title,
TOTEM_PL_PARSER_FIELD_URI, uri,
NULL);
}
return TOTEM_PL_PARSER_RESULT_SUCCESS;
}
static TotemPlParserResult
parse_opml_head_body (TotemPlParser *parser, const char *uri, xml_node_t *parent)
{
xml_node_t* node;
gboolean started;
started = FALSE;
for (node = parent->child; node != NULL; node = node->next) {
if (node->name == NULL)
continue;
if (g_ascii_strcasecmp (node->name, "body") == 0) {
if (started == FALSE) {
/* Send the info we already have about the feed */
totem_pl_parser_add_uri (parser,
TOTEM_PL_PARSER_FIELD_IS_PLAYLIST, TRUE,
TOTEM_PL_PARSER_FIELD_URI, uri,
NULL);
started = TRUE;
}
parse_opml_outline (parser, node);
}
}
return TOTEM_PL_PARSER_RESULT_SUCCESS;
}
TotemPlParserResult
totem_pl_parser_add_opml (TotemPlParser *parser,
GFile *file,
GFile *base_file,
TotemPlParseData *parse_data,
gpointer data)
{
#ifndef HAVE_GMIME
WARN_NO_GMIME;
#else
xml_node_t* doc;
char *contents, *uri;
gsize size;
if (g_file_load_contents (file, NULL, &contents, &size, NULL, NULL) == FALSE)
return TOTEM_PL_PARSER_RESULT_ERROR;
doc = totem_pl_parser_parse_xml_relaxed (contents, size);
if (doc == NULL) {
g_free (contents);
return TOTEM_PL_PARSER_RESULT_ERROR;
}
/* If the document has no name */
if (doc->name == NULL
|| g_ascii_strcasecmp (doc->name , "opml") != 0) {
g_free (contents);
xml_parser_free_tree (doc);
return TOTEM_PL_PARSER_RESULT_ERROR;
}
uri = g_file_get_uri (file);
parse_opml_head_body (parser, uri, doc);
g_free (uri);
g_free (contents);
xml_parser_free_tree (doc);
return TOTEM_PL_PARSER_RESULT_SUCCESS;
#endif /* !HAVE_GMIME */
}
#endif /* !TOTEM_PL_PARSER_MINI */
|