Skip to content
Newer
Older
100644 149 lines (136 sloc) 3.71 KB
fb9f0a0 @alexkingorg initial add
authored Feb 14, 2011
1 <?php
2
3 // iTunes Stats
4 //
5 // Copyright (c) 2005-2010 Alex King. All rights reserved.
6 // http://alexking.org
7 //
8 // Released under the GPL license
9 // http://www.opensource.org/licenses/gpl-license.php
10 //
11 // **********************************************************************
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
15 // **********************************************************************
16
17 // requires PHP 5+ (XML libraries) and MySQL 4+ (case insensitive SELECTs)
18
19 require_once('config.inc.php');
20 require_once('backend/objects.inc.php');
21 require_once('backend/functions.inc.php');
22
23 $database_connection = mysql_connect(
24 $database_host
25 , $database_user
26 , $database_pass
27 );
28 if ($database_connection) {
29 if (!mysql_select_db($database_name, $database_connection)) {
30 die('<p>Error selecting database: '.$database_name);
31 }
32 }
33 else {
34 die('<p>Error connecting to database: '.$database_host);
35 }
36
37 if (!empty($_GET['action'])) {
38 switch ($_GET['action']) {
39 case 'load':
40 $result = mysql_query("TRUNCATE `".$database_table_prefix."artist`");
41 $result = mysql_query("TRUNCATE `".$database_table_prefix."album`");
42 $result = mysql_query("TRUNCATE `".$database_table_prefix."song`");
43
44 if (!file_exists('Library.xml')) {
45 die('<p>Export your iTunes music library as <strong>Library.xml</strong> and place it in this directory.</p>');
46 }
47
48 $data = simplexml_load_file('Library.xml');
49
50 if (!$data) {
51 die('<p>Error loading iTunes data.');
52 }
53
54 $properties = array(
55 'Name'
56 , 'Artist'
57 , 'Album'
58 , 'Rating'
59 , 'Play Count'
60 );
61
62 $count = 0;
63
64 foreach ($data->dict->dict->dict as $tune) {
65 $song = new song;
66 $next = false;
67 foreach ($tune as $property) {
68 if ($next != false) {
69 // this is such an ugly hack, but otherwise the data was still in a simpleXMLObject
70 // and I didn't know how to get it out - somebody help me out here
71 ob_start();
72 print($property);
73 $song->$next = ob_get_contents();
74 ob_end_clean();
75 }
76 if (in_array($property, $properties)) {
77 $next = strtolower(str_replace(' ', '_', $property));
78 }
79 else {
80 $next = false;
81 }
82 }
83 if ($song->insert()) {
84 $count++;
85 }
86 }
87 header("Location: index.php");
88 die();
89 break;
90 }
91 }
92 else if (isset($_GET['report']) && file_exists('reports/'.basename($_GET['report']).'.inc.php')) {
93 include('reports/'.basename($_GET['report']).'.inc.php');
94 }
95 else {
96 $title = 'Home';
97 $reports = get_reports();
98 }
99 ?>
100 <html>
101 <head>
102 <title>iTunes Stats - <?php print($title); ?></title>
103 <link rel="stylesheet" type="text/css" href="css/default.css" />
104 </head>
105 <body>
106 <h1>iTunes Stats <?php if (!empty($your_name)) { print(' for '.$config_your_name); } ?></h1>
107 <div id="menu">
108 <ul>
109 <li><a href="index.php">Home</a></li>
110 </ul>
111 </div>
112 <div id="content">
113
114 <?php
115 if (isset($grid)) {
116 print('
117 <h2>'.$title.'</h2>
118 ');
119 $grid->display();
120 }
121 else {
122 ?>
123 <h2>(Re)Load Data</h2>
124 <ol>
125 <li><a href="index.php?action=load">Import iTunes data to MySQL</a></li>
126 </ol>
127 <h2>Reports</h2>
128 <dl class="reports">
129 <?php
130 foreach ($reports as $report) {
131 if (!is_array($report)) {
132 $report->print_dl_item();
133 }
134 }
135 ?>
136 </dl>
137 <?php
138 foreach ($reports as $title => $reports) {
139 if (is_array($report)) {
140 print_reports_category($title, $reports);
141 }
142 }
143 }
144 ?>
145
146 </div>
147 <p id="footer">Copyright &copy; 2005-2010 <a href="http://alexking.org">Alex King</a>. All rights reserved. Version 0.4 beta.</p>
148 </body>
149 </html>
Something went wrong with that request. Please try again.