OPiQuotations  v.03.00.00 — April 5, 2019
une-OPiCitation-pour-Twitter.php
Go to the documentation of this file.
1 <?php /* -*- coding: utf-8 -*- */
2 
3 /** \file une-OPiCitation-pour-Twitter.php
4  * (June 28, 2018)
5  *
6  * \brief
7  * Little PHP application to send one quotation (from OPiCitations)
8  * to Twitter account https://twitter.com/OPiCitationJour
9  *
10  * Use the https://github.com/abraham/twitteroauth library.
11  *
12  * Piece of OPiQuotations.
13  * https://bitbucket.org/OPiMedia/opiquotations
14  *
15  * GPLv3 --- Copyright (C) 2014, 2016, 2017, 2018 Olivier Pirson
16  * http://www.opimedia.be/
17  *
18  * @package OPiCitations
19  */
20 
21 set_include_path(get_include_path().PATH_SEPARATOR.dirname(realpath(__FILE__)));
22 require_once 'OPiQuotations/log.inc';
23 
24 #DEBUG
25 if (true) {
26  // Development configuration
27  ini_set('display_errors', 'stdout');
28  ini_set('display_startup_errors', 1);
29  ini_set('html_errors', 1);
30 
31  error_reporting(-1);
32 
33  assert_options(ASSERT_ACTIVE, true);
34  assert_options(ASSERT_WARNING, true);
35  assert_options(ASSERT_BAIL, true);
36 }
37 else {
38 #DEBUG_END
39  // Production configuration
40  ini_set('display_errors', 'stderr');
41  ini_set('display_startup_errors', 0);
42  ini_set('html_errors', 0);
43 
44  error_reporting(-1);
45 
46  assert_options(ASSERT_ACTIVE, false);
47  assert_options(ASSERT_WARNING, false);
48  assert_options(ASSERT_BAIL, false);
49 
50  set_error_handler('\OPiQuotations\error_handler');
51 #DEBUG
52 }
53 #DEBUG_END
54 
55 mb_internal_encoding('UTF-8');
56 mb_regex_encoding('UTF-8');
57 mb_http_output('UTF-8');
58 mb_detect_order('UTF-8');
59 
60 require_once 'OPiQuotations/OPiQuotations.inc';
61 
62 
63 $LABEL = 'Twitter - OPiCitationJour';
64 
65 // Choose a quotation at random
67 
68 $quot = $opiquotations->quotation_by_random($LABEL);
69 #DEBUG
70 #$quot = $opiquotations->quotation_by_id(9);
71 #$quot = $opiquotations->quotation_by_id(110);
72 #$quot = $opiquotations->quotation_by_id(459);
73 #$quot = $opiquotations->quotation_by_id(961);
74 #$quot = $opiquotations->quotation_by_id(3062);
75 #DEBUG_END
76 
77 if ($quot === null) {
78  OPiQuotations\to_log('une-OPiCitation-pour-Twitter.php: no random quotation!');
79 
80  return false;
81 }
82 
83 
84 // Connect to Twitter
85 require_once('libs/twitteroauth/autoload.php');
87 
88 require_once('.private/Twitter_login.inc');
89 
90 $connection = new TwitterOAuth($consumer_key, $consumer_secret, $access_token, $access_token_secret);
91 
92 $result = $connection->get('help/configuration');
93 $short_url_length = (empty($result->errors)
94  ? $result->short_url_length
95  : 23);
96 #DEBUG
97 if (true) { // print connection result for debugging
98  echo '<pre>help/configuration: ';
99  var_dump($result);
100  echo '</pre>';
101 }
102 #DEBUG_END
103 
104 
106 
107 for ($try = 0; $try < 5; ++$try, --$max_length) { // try 5 times with decreasing length
108  // Build text and send to Twitter
109  $text = $quot->to_text_twitter('http://www.opimedia.be/OPiCitations/?id='.$quot->id(),
111 
112 #DEBUG
113  if (true) { // print short quotation for debugging
114  preg_match('/(http:\/\/.*?)(\n|$)/', $text, $long_url);
115  $long_url = $long_url[0];
116  echo mb_strlen($text).' - '.mb_strlen($long_url).' + '.$short_url_length.' = '.(mb_strlen($text) - mb_strlen($long_url) + $short_url_length)
117  .'<pre>'.htmlspecialchars($text).'</pre>';
118 
119  unset($long_url);
120 
121  return false; // exit to *don't* post the tweet when do tests
122  }
123 #DEBUG_END
124 
125 
126  // Send to Twitter
127  $result = $connection->post('statuses/update', array('status' => $text,
128  'trim_user' => true));
129  if (empty($result->errors)) { // tweet succeed
130  break;
131  }
132 }
133 
134 if (!empty($result->errors)) { // tweet failed
135  #DEBUG
136  if (true) { // print Twitter errors for debugging
137  echo '<pre>$result->errors:
138 ';
139  var_dump($result->errors);
140  echo '</pre>';
141  }
142  #DEBUG_END
143  $a = array(basename(__FILE__).' error(s):');
144  foreach ($result->errors as $error) {
145  $a[] = ' - Error '.$error->code.': '.$error->message;
146  }
147  $a[] = 'Quotation '.$quot->id().': '.$text;
148 
149  OPiQuotations\to_log(implode('
150 ', $a));
151 }
152 
153 
154 if (empty($result->id)) {
155  OPiQuotations\to_log('une-OPiCitation-pour-Twitter.php: no add selection! '.$quot->id());
156 }
157 else {
158  // Update DB with URL
159  $url = 'https://twitter.com/OPiCitationJour/status/'.$result->id;
160  #DEBUG
161  if (true) {
162  echo '<pre>id:
163 ';
164  var_dump($result->id);
165  echo '</pre><a href="', $url, '">', $url, '</a>';
166  }
167  #DEBUG_END
168 
170 
171  $ok = $opiquotations->quotation_add_selection($quot, $selection);
172 
173  if (!$ok) {
174  OPiQuotations\to_log('une-OPiCitation-pour-Twitter.php: add selection failed! '.$quot->id());
175  }
176 }
177 
178 ?>
to_log($message)
Append $message in LOG_FILE.
Definition: log.inc:104
Class selection.
if($quot===null) $text
$url
Definition: index.php:385
Class to get OPiQuotation and informations from the databse.
if(true) $max_length
if(true) $selection