6f66691110a0e7b438ad54225ecbc64baa5e5b0d
[enigtomb.git] / sync-radio-bouqets.php
1 <?php
2 /**
3 * Syncs all dreambox radio bouqets into a folder on mediatomb.
4 * Renames/changes existing entries instead of deleting
5 * everything first.
6 *
7 * @author   Christian Weiske <cweiske@php.net>
8 * @license  LGPL http://www.gnu.org/copyleft/lesser.html
9 */
10 require_once 'DreamboxBouqets.php';
11 require_once 'Services/MediaTomb.php';
12 require_once 'config.php';
13 require_once dirname(__FILE__) . '/syncfunctions.php';
14
15 $bReally = true;
16
17 echo "Synchronizing dreambox radio bouqets and mediatomb radion station list\n";
18 echo " fetching bouqets\n";
19 $arBouqets = DreamboxBouqets::fetchRadioBouqets(
20     $GLOBALS['enigtombConfig']['dreambox']['host']
21 );
22
23 $mt = new Services_MediaTomb(
24     $GLOBALS['enigtombConfig']['mediatomb']['user'],
25     $GLOBALS['enigtombConfig']['mediatomb']['pass'],
26     $GLOBALS['enigtombConfig']['mediatomb']['host'],
27     $GLOBALS['enigtombConfig']['mediatomb']['port']
28 );
29
30 $strRadiodir = $GLOBALS['enigtombConfig']['mediatomb']['radiodir'];
31
32 $radiodir = $mt->createContainerByPath($strRadiodir);
33
34 /**
35 * create bouqet/container sync list
36 */
37 $arBouqetNames        = array_keys($arBouqets);
38 $arRadiodirContainers = $radiodir->getContainers();
39 $arContainerNames     = array();
40 foreach ($arRadiodirContainers as $nId => $container) {
41     $arBouqetNames[] = $container->title;
42     $arContainerNames[$container->title] = $nId;
43 }
44
45 if (isset($GLOBALS['enigtombConfig']['mediatomb']['rootBouqet'])
46     && $GLOBALS['enigtombConfig']['mediatomb']['rootBouqet']
47 ) {
48     $arBouqetNames[] = $GLOBALS['enigtombConfig']['mediatomb']['rootBouqet'];
49     $arContainerNames[$GLOBALS['enigtombConfig']['mediatomb']['rootBouqet']] = $radiodir->id;
50     $arRadiodirContainers[$radiodir->id] = $radiodir;
51 }
52
53 $arBouqetNames = array_unique($arBouqetNames);
54
55 /**
56 * sync bouqets and containers
57 * Does not know about renamed bouqets
58 */
59 echo " syncing bouqets and containers\n";
60 $arValidContainers = array();
61 foreach ($arBouqetNames as $strBouqetName) {
62     if (isset($arContainerNames[$strBouqetName]) && isset($arBouqets[$strBouqetName])) {
63         //everything is ok
64         echo "  " . $strBouqetName . " already exists, ok\n";
65         $nContainerId = $arContainerNames[$strBouqetName];
66         $arValidContainers[$nContainerId] = $arRadiodirContainers[$nContainerId];
67     } else if (!isset($arContainerNames[$strBouqetName])) {
68         //container does not exist
69         if ($strBouqetName != $GLOBALS['enigtombConfig']['mediatomb']['rootBouqet']) {
70             echo "  creating " . $strBouqetName . "\n";
71             $container = $mt->createContainerByPath($strRadiodir . '/' . $strBouqetName);
72             $arValidContainers[$container->id] = $container;
73         }
74     } else if (!isset($arBouqets[$strBouqetName])) {
75         if ($strBouqetName != $GLOBALS['enigtombConfig']['mediatomb']['rootBouqet']) {
76             //bouqet does not exist anymore -> delete container
77             echo "  deleting container " . $strBouqetName . "\n";
78             if ($bReally) {
79                 $mt->deleteItem($arContainerNames[$strBouqetName]);
80             }
81         }
82     }
83 }
84
85 /**
86 * Sync radio stations
87 */
88 echo " syncing stations\n";
89 foreach ($arValidContainers as $nContainerId => $container) {
90     /**
91     * Build stations sync list
92     */
93     if (isset($GLOBALS['enigtombConfig']['mediatomb']['rootBouqet'])
94         && $GLOBALS['enigtombConfig']['mediatomb']['rootBouqet']
95         && $container == $radiodir
96     ) {
97         $strContainerTitle = $GLOBALS['enigtombConfig']['mediatomb']['rootBouqet'];
98     } else {
99         $strContainerTitle = $container->title;
100     }
101     echo "  syncing stations in " . $strContainerTitle . "\n";
102
103     //existing stations in mediatomb
104     $arStationItems      = $container->getItems(0, 1000);
105     $arStationUris       = array();
106     $arStationItemsByUri = array();
107     foreach ($arStationItems as $item) {
108         $arStationUris[]                 = $item->url;
109         $arStationItemsByUri[$item->url] = $item;
110     }
111     unset($arStationItems);
112
113     //stations in bouqet
114     foreach ($arBouqets[$strContainerTitle] as $arStation) {
115         $arStationUris[]                             = $arStation['stream'];
116         $arBouqetStationsByUri[$arStation['stream']] = $arStation;
117     }
118     unset($arBouqets[$strContainerTitle]);
119
120     $arStationUris = array_unique($arStationUris);
121
122     //sync stations
123     foreach ($arStationUris as $strUri) {
124         echo '   syncing station ' . $strUri . "\n";
125         if (isset($arBouqetStationsByUri[$strUri]) && isset($arStationItemsByUri[$strUri])) {
126             //station exists on both dreambox and mediatomb
127             //update title
128             echo "    updating title and description\n";
129             $arTitles = enigtombGetStationTitle($arBouqetStationsByUri[$strUri]);
130             echo "    " . $arTitles['title'] . "\n";
131             if ($arTitles['description'] && $arTitles['description'] != $arTitles['title']) {
132                 echo "    " . $arTitles['description'] . "\n";
133             }
134             $arStationItemsByUri[$strUri]->title       = $arTitles['title'];
135             $arStationItemsByUri[$strUri]->description = $arTitles['description'];
136             if ($bReally) {
137                 $arStationItemsByUri[$strUri]->save();
138             }
139         } else if (!isset($arBouqetStationsByUri[$strUri])) {
140             //station does not exist on dreambox
141             //delete it in mediatomb
142             echo "    deleting\n";
143             if ($bReally) {
144                 $arStationItemsByUri[$strUri]->delete();
145             }
146         } else if (!isset($arStationItemsByUri[$strUri])) {
147             //station exists on dreambox only
148             //create it in mediatomb
149             echo "    creating\n";
150             $arTitles = enigtombGetStationTitle($arBouqetStationsByUri[$strUri]);
151             echo "    " . $arTitles['title'] . "\n";
152             if ($bReally) {
153                 $container->createExternalLink(
154                     $arTitles['title'], $strUri, $arTitles['description'],
155                     $GLOBALS['enigtombConfig']['mediatomb']['mimetype'],
156                     'http-get', 'object.item', false
157                 );
158             }
159         }
160     }
161 }
162
163
164 echo "done\n";
165 ?>