rss_converter_twitter.com.xsl: add a line break after the "(Video)" label
[tweeper.git] / tweeper.php
1 <?php
2 /**
3  * @file
4  * Tweeper - a Twitter to RSS web scraper.
5  *
6  * Copyright (C) 2013-2016  Antonio Ospite <ao2@ao2.it>
7  *
8  * This program is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation, either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20  */
21
22 require_once 'autoload.php';
23
24 use Tweeper\Tweeper;
25
26 date_default_timezone_set('UTC');
27
28 /**
29  * Check if the script is being run from the command line.
30  */
31 function is_cli() {
32   return (php_sapi_name() === "cli");
33 }
34
35 /**
36  * Show the script usage.
37  */
38 function usage($argv) {
39   if (is_cli()) {
40     $usage = "{$argv[0]} [-e|-h|--help] <src_url>\n";
41   }
42   else {
43     $usage = htmlentities("{$_SERVER['SCRIPT_NAME']}?src_url=<src_url>&generate_enclosure=<0|1>");
44   }
45
46   return "usage: $usage";
47 }
48
49 /**
50  * Parse command line options.
51  */
52 function parse_options_cli($argv, $argc) {
53   $options = array(
54     'generate_enclosure' => FALSE,
55   );
56
57   if ($argc < 2) {
58     return $options;
59   }
60
61   $cli_options = getopt("eh", array("help"));
62   foreach ($cli_options as $opt => $val) {
63     switch ($opt) {
64       case 'e':
65         $options['generate_enclosure'] = TRUE;
66         break;
67
68       case 'h':
69       case 'help':
70         echo usage($argv);
71         exit(0);
72
73       default:
74         fwrite(STDERR, usage($argv));
75         exit(1);
76     }
77   }
78
79   $options['src_url'] = $argv[count($cli_options) + 1];
80
81   return $options;
82 }
83
84 /**
85  * Parse options passed from a query string.
86  */
87 function parse_options_query_string() {
88   $options = array(
89     'generate_enclosure' => FALSE,
90   );
91
92   if (isset($_GET['src_url'])) {
93     $options['src_url'] = $_GET['src_url'];
94   }
95
96   if (isset($_GET['generate_enclosure'])) {
97     $options['generate_enclosure'] = $_GET['generate_enclosure'] == 1;
98   }
99
100   return $options;
101 }
102
103 if (is_cli()) {
104   $options = parse_options_cli($argv, $argc);
105   $error_stream = fopen('php://stderr', 'w');
106 }
107 else {
108   $options = parse_options_query_string();
109   $error_stream = fopen('php://output', 'w');
110 }
111
112 if (!isset($options['src_url'])) {
113   fwrite($error_stream, usage(is_cli() ? $argv : NULL));
114   exit(1);
115 }
116
117 $tweeper = new Tweeper($options['generate_enclosure']);
118 $output = $tweeper->tweep($options['src_url']);
119 if (is_null($output)) {
120   exit(1);
121 }
122 echo $output;