Initial commit
Benjamin Renard

Benjamin Renard commited on 2015-01-04 23:39:08
Showing 3 changed files, with 261 additions and 0 deletions.

... ...
@@ -0,0 +1,18 @@
1
+Script pour envoyer par SMS le programme ciné
2
+=============================================
3
+
4
+Ce script récupère sur Allociné le programme ciné du cinéma
5
+de votre choix et formatte un message qui sera envoyé par SMS.
6
+
7
+La librairie api-allocine-helper est utilisée pour intéroger
8
+l'API d'Allociné. L'envoi du SMS est fait via une passerelle
9
+mail2sms.
10
+
11
+Utilisation
12
+-----------
13
+
14
+  Usage: send.php [-h] -c=[code_cine] -t=[0612345678] -d=[sms.domain.tld]
15
+	-h			Show this message
16
+	-c=[code_cine]		Specify cine code
17
+	-t=[number]		Specify phone number
18
+	-d=[sms.domain.tld]	Specify SMS mail domaine
... ...
@@ -0,0 +1,127 @@
1
+<?php
2
+
3
+function day2int($day,$inv=false) {
4
+	$days=array(
5
+		'lun' => 1,
6
+		'mar' => 2,
7
+		'mer' => 3,
8
+		'jeu' => 4,
9
+		'ven' => 5,
10
+		'sam' => 6,
11
+		'dim' => 7,
12
+	);
13
+	if ($inv) {
14
+		foreach($days as $d => $i) if ($day==$i) return $d;
15
+		return false;
16
+	}
17
+	else {
18
+		return (isset($days[$day])?$days[$day]:false);
19
+	}
20
+}
21
+
22
+function group_days($movies) {
23
+	$ret=array();
24
+	foreach($movies as $m => $days) {
25
+		//echo $m."\n";
26
+		$times=array();
27
+		$ret[$m]=array();
28
+		foreach ($days as $d => $hs) {
29
+			foreach ($hs as $h) {
30
+				if (!isset($times[$h])) {
31
+					$times[$h]=array();
32
+				}
33
+				$times[$h][]=$d;
34
+			}
35
+		}
36
+		foreach($times as $h => $days) {
37
+			$f=null;
38
+			$l=null;
39
+			$ps=array();
40
+			//echo "$m - $h : ".implode('-',$days)."\n";
41
+			foreach(array(3,4,5,6,7,1,2,0) as $d) {
42
+				$day=day2int($d,1);
43
+				//echo "$h : $day ?\n";
44
+				if (in_array($day,$days)) {
45
+					if ($f==null) {
46
+						//echo "$h : debut $day\n";
47
+						$f=$day;	
48
+					}
49
+					//else echo "$day : on continue\n";
50
+					$prev=$day;
51
+				}
52
+				elseif ($f!=null) {
53
+					//echo "$h : fin $prev\n";
54
+					$l=$prev;
55
+					if ($f==$l) $ps[]=$f; else $ps[]="$f-$l";
56
+					$f=null;
57
+					$l=null;
58
+				}
59
+			}
60
+			//echo "$m - $h => ".implode('&',$ps)."\n";
61
+			foreach($ps as $p) {
62
+				if (!isset($ret[$m][$p])) {
63
+					$ret[$m][$p]=array();
64
+				}
65
+				$ret[$m][$p][]=$h;
66
+			}
67
+		}
68
+	}
69
+	return $ret;
70
+}
71
+
72
+function withoutAccents($string){
73
+	$replaceAccent = Array(
74
+		"à" => "a",
75
+		"á" => "a",
76
+		"â" => "a",
77
+		"ã" => "a",
78
+		"ä" => "a",
79
+		"ç" => "c",
80
+		"è" => "e",
81
+		"é" => "e",
82
+		"ê" => "e",
83
+		"ë" => "e",
84
+		"ì" => "i",
85
+		"í" => "i",
86
+		"î" => "i",
87
+		"ï" => "i",
88
+		"ñ" => "n",
89
+		"ò" => "o",
90
+		"ó" => "o",
91
+		"ô" => "o",
92
+		"õ" => "o",
93
+		"ö" => "o",
94
+		"ù" => "u",
95
+		"ú" => "u",
96
+		"û" => "u",
97
+		"ü" => "u",
98
+		"ý" => "y",
99
+		"ÿ" => "y",
100
+		"À" => "A",
101
+		"Á" => "A",
102
+		"Â" => "A",
103
+		"Ã" => "A",
104
+		"Ä" => "A",
105
+		"Ç" => "C",
106
+		"È" => "E",
107
+		"É" => "E",
108
+		"Ê" => "E",
109
+		"Ë" => "E",
110
+		"Ì" => "I",
111
+		"Í" => "I",
112
+		"Î" => "I",
113
+		"Ï" => "I",
114
+		"Ñ" => "N",
115
+		"Ò" => "O",
116
+		"Ó" => "O",
117
+		"Ô" => "O",
118
+		"Õ" => "O",
119
+		"Ö" => "O",
120
+		"Ù" => "U",
121
+		"Ú" => "U",
122
+		"Û" => "U",
123
+		"Ü" => "U",
124
+		"Ý" => "Y"
125
+	);
126
+	return strtr($string, $replaceAccent);
127
+}
... ...
@@ -0,0 +1,116 @@
1
+<?php
2
+
3
+error_reporting(E_ERROR);
4
+
5
+@require_once "lib/api-allocine-helper/api-allocine-helper.php";
6
+require_once "functions.php";
7
+
8
+// Retreive local from environnement
9
+setlocale(LC_ALL, '');
10
+
11
+// Manage script parameters
12
+$options=getopt('hc:t:d:');
13
+
14
+if (isset($options["h"])) {
15
+	echo "Usage: ".$argv[0]." [-h] -c=[code_cine] -t=[0612345678] -d=[sms.domain.tld]\n";
16
+	echo "	-h			Show this message\n";
17
+	echo "	-c=[code_cine]		Specify cine code\n";
18
+	echo "	-t=[number]		Specify phone number\n";
19
+	echo "	-d=[sms.domain.tld]	Specify SMS mail domaine\n";
20
+	exit();
21
+}
22
+
23
+if (isset($options["c"]) && $options["c"]!==false) {
24
+	$code_cine=$options["c"];
25
+}
26
+else die("You must provide cine code with -c=XXX parameter.\n");
27
+
28
+if (isset($options["t"]) && $options["t"]!==false) {
29
+	$send_to=$options["t"];
30
+}
31
+else die("You must provide phone number with -t=XXXX parameter.\n");
32
+
33
+if (isset($options["d"]) && $options["d"]!==false) {
34
+	$mail_domain=$options["d"];
35
+}
36
+else die("You must provide SMS mail domain with -d=domain.tld parameter.\n");
37
+
38
+try {
39
+	$helper = new AlloHelper;
40
+	$result = $helper->showtimesByTheaters($code_cine);
41
+}
42
+catch( ErrorException $error ) {
43
+	die("Error retreiving informations (Err. n°". $error->getCode(). ") : ". $error->getMessage());
44
+}
45
+
46
+try {
47
+	$name_cine=null;
48
+	$movies=array();
49
+	foreach($result->theaterShowtimes as $t) {
50
+		$name_cine=$t->place->theater->name;
51
+		foreach($t->movieShowtimes as $mst) {
52
+			$movie=utf8_encode($mst->onShow->movie->title);
53
+			$suf=array();
54
+			if ($mst->screenFormat['$']=='3D') {
55
+				$suf[]='3D';
56
+			}
57
+			if ($mst->version->code!=6001) {
58
+				$suf[]='VO';
59
+			}
60
+			if(count($suf)>0) {
61
+				$movie.=' ('.implode('/',$suf).')';
62
+			}
63
+	
64
+			if (!isset($movies[$movie])) {
65
+				$movies[$movie]=array();
66
+			}
67
+	
68
+			foreach($mst->scr as $day) {
69
+				$date=DateTime::createFromFormat('Y-m-d',$day['d']);
70
+				$jour=str_replace('.','',strftime('%a',$date->getTimestamp()));
71
+				if(!isset($movies[$movie][$jour])) $movies[$movie][$jour]=array();
72
+				foreach($day['t'] as $t) {
73
+					$movies[$movie][$jour][]=$t['$'];
74
+				}
75
+			}
76
+		}
77
+	}
78
+}
79
+catch( ErrorException $error ) {
80
+	die("Error parsing informations (Err. n°".$error->getCode().") : ".$error->getMessage());
81
+}
82
+
83
+// Format movies informations
84
+$fmovies=group_days($movies);
85
+
86
+// Calculate start/end of cine week
87
+$start_date=new Datetime(date('Y-m-d').' 00:00:00');
88
+$start=$start_date->format('d/m');
89
+$start_time=$start_date->getTimestamp();
90
+$start_wd=(int)date('N',$start_time);
91
+$end_time=$start_time+(24*3600*((9-$start_wd)%7));
92
+$end=date('d/m',$end_time);
93
+
94
+// Format Message
95
+$msg="Cinema $name_cine\nProgramme du $start au $end\n";
96
+
97
+foreach($fmovies as $m => $ps){
98
+	$msg.="$m : ";
99
+	$hss=array();
100
+	foreach($ps as $p => $hs) {
101
+		$hss[]="$p ".implode(' & ',$hs);
102
+	}
103
+	$msg.=implode(' / ',$hss)."\n";
104
+}
105
+
106
+// Remove accents
107
+$msg=$msg=withoutAccents($msg);
108
+
109
+// Format mail
110
+$mail="$send_to@$mail_domain";
111
+
112
+// Display message
113
+echo "Send to $mail :\n\n$msg";
114
+
115
+// Send message
116
+mail($mail,'',$msg);
0 117