+

Last commit for send.php: ee126815ade188d07d5ca9f24caf890b83e8e44e

Add debug mode

Benjamin Renard [2015-01-07 10:06:21]
Add debug mode
  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. // Set locale to 'fr_FR.UTF-8' (required to retreive correct days short name)
  9. // CAUTION : If you want to modify it, you have to change day2int() function
  10. setlocale(LC_ALL, 'fr_FR.UTF-8');
  11.  
  12. // Manage script parameters
  13. $options=getopt('hDc:t:d:');
  14.  
  15. if (isset($options["h"])) {
  16. echo "Usage: ".$argv[0]." [-h] -c=[code_cine] -t=[0612345678] -d=[sms.domain.tld]\n";
  17. echo " -h Show this message\n";
  18. echo " -d Debug mode\n";
  19. echo " -c=[code_cine] Specify cine code\n";
  20. echo " -t=[number] Specify phone number\n";
  21. echo " -d=[sms.domain.tld] Specify SMS mail domaine\n";
  22. exit();
  23. }
  24.  
  25. $_debug=false;
  26. if (isset($options["D"])) {
  27. $_debug=true;
  28. }
  29.  
  30. function debug($data,$dump=false) {
  31. global $_debug;
  32. if (!$_debug) return true;
  33. if ($dump) {
  34. var_dump($data);
  35. }
  36. else if (!is_array($data) && !is_object($data)) {
  37. echo "$data\n";
  38. }
  39. return true;
  40. }
  41.  
  42. debug("Options : ".print_r($options,1));
  43.  
  44. if (isset($options["c"]) && $options["c"]!==false) {
  45. $code_cine=$options["c"];
  46. }
  47. else die("You must provide cine code with -c=XXX parameter.\n");
  48.  
  49. $send_to=false;
  50. $mail_domain=false;
  51. if (isset($options["t"]) && $options["t"]!==false) {
  52. $send_to=$options["t"];
  53. if (isset($options["d"]) && $options["d"]!==false) {
  54. $mail_domain=$options["d"];
  55. }
  56. else die("You must provide SMS mail domain with -d=domain.tld parameter.\n");
  57. }
  58.  
  59. try {
  60. $helper = new AlloHelper;
  61. debug("Request showtimes for cine $code_cine");
  62. $result = $helper->showtimesByTheaters($code_cine);
  63. debug("Result :");
  64. debug($result,true);
  65. }
  66. catch( ErrorException $error ) {
  67. die("Error retreiving informations (Err. n°". $error->getCode(). ") : ". $error->getMessage());
  68. }
  69.  
  70. try {
  71. debug("Parse result content...");
  72. $name_cine=null;
  73. $movies=array();
  74. foreach($result->theaterShowtimes as $t) {
  75. $name_cine=$t->place->theater->name;
  76. debug("Cine name : $name_cine");
  77. foreach($t->movieShowtimes as $mst) {
  78. $movie=utf8_encode($mst->onShow->movie->title);
  79. $suf=array();
  80. if ($mst->screenFormat['$']=='3D') {
  81. $suf[]='3D';
  82. }
  83. if ($mst->version->code!=6001) {
  84. $suf[]='VO';
  85. }
  86. if(count($suf)>0) {
  87. $movie.=' ('.implode('/',$suf).')';
  88. }
  89.  
  90. if (!isset($movies[$movie])) {
  91. debug("New movie : $movie");
  92. $movies[$movie]=array();
  93. }
  94.  
  95. foreach($mst->scr as $day) {
  96. $date=DateTime::createFromFormat('Y-m-d',$day['d']);
  97. $jour=str_replace('.','',strftime('%a',$date->getTimestamp()));
  98. if(!isset($movies[$movie][$jour])) $movies[$movie][$jour]=array();
  99. foreach($day['t'] as $t) {
  100. debug("New schedule time for movie $movie : day=$jour / time=".$t['$']);
  101. $movies[$movie][$jour][]=$t['$'];
  102. }
  103. }
  104. }
  105. }
  106. }
  107. catch( ErrorException $error ) {
  108. die("Error parsing informations (Err. n°".$error->getCode().") : ".$error->getMessage());
  109. }
  110.  
  111. debug("Parsing result :".print_r($movies,1));
  112.  
  113. // Format movies informations
  114. $fmovies=group_days($movies);
  115. debug("Formated movies informations : ".print_r($fmovies,1));
  116.  
  117. // Calculate start/end of cine week
  118. $start_date=new Datetime(date('Y-m-d').' 00:00:00');
  119. $start=$start_date->format('d/m');
  120. $start_time=$start_date->getTimestamp();
  121. $start_wd=(int)date('N',$start_time);
  122. $end_time=$start_time+(24*3600*((9-$start_wd)%7));
  123. $end=date('d/m',$end_time);
  124.  
  125. // Format Message
  126. $msg="Cinema $name_cine\nProgramme du $start au $end\n";
  127.  
  128. foreach($fmovies as $m => $ps){
  129. $msg.="$m : ";
  130. $hss=array();
  131. foreach($ps as $p => $hs) {
  132. $hss[]="$p ".implode(' & ',$hs);
  133. }
  134. $msg.=implode(' / ',$hss)."\n";
  135. }
  136.  
  137. // Remove accents
  138. $msg=$msg=withoutAccents($msg);
  139.  
  140. if ($send_to && $mail_domain) {
  141. // Format mail
  142. $mail="$send_to@$mail_domain";
  143.  
  144. // Display message
  145. echo "Send to $mail :\n\n$msg";
  146.  
  147. // Send message
  148. mail($mail,'',$msg);
  149. }
  150. else {
  151. echo "$msg";
  152. }