25require_once DOL_DOCUMENT_ROOT.
'/core/class/socialnetworkmanager.class.php';
40 private $clientSecret;
65 private $authUrl =
'https://www.reddit.com/api/v1/access_token';
84 $this->clientId = $authParams[
'client_id'] ??
'';
85 $this->clientSecret = $authParams[
'client_secret'] ??
'';
86 $this->username = $authParams[
'username'] ??
'';
87 $this->password = $authParams[
'password'] ??
'';
88 $this->userAgent = ($authParams[
'name_app'] ??
'').
'/0.1 by '.($authParams[
'username'] ??
'');
100 'grant_type' =>
'password',
101 'username' => $this->username,
102 'password' => $this->password,
103 'scope' =>
'read identity'
107 'Authorization: Basic ' . base64_encode($this->clientId .
':' . $this->clientSecret),
108 'Content-Type: application/x-www-form-urlencoded',
109 'User-Agent: ' . $this->userAgent
112 $result =
getURLContent($this->
getAuthUrl(),
'POST', http_build_query($authData), 1, $headers, [
'http',
'https'], 0);
114 if (!empty($result[
'content'])) {
115 $data = json_decode($result[
'content'],
true);
116 if (isset($data[
'access_token'])) {
117 $this->accessToken = $data[
'access_token'];
120 $this->error = $data[
'error'] ??
'Unknown error during authentication';
124 $this->error =
'Authentication failed. No content received.';
139 public function fetch($urlAPI, $maxNb = 5, $cacheDelay = 60, $cacheDir =
'', $authParams = [])
141 if (empty($this->accessToken) && !$this->
authenticate()) {
145 $cacheFile = $cacheDir .
'/' .
dol_hash($urlAPI,
'3');
146 $foundInCache =
false;
150 if ($cacheDelay > 0 && $cacheDir &&
dol_is_file($cacheFile)) {
152 if ($fileDate >= (
dol_now() - $cacheDelay)) {
153 $foundInCache =
true;
154 $data = file_get_contents($cacheFile);
158 if (!$foundInCache) {
160 'Authorization: Bearer ' . $this->accessToken,
161 'User-Agent: ' . $this->userAgent,
164 $result =
getURLContent($urlAPI,
'GET',
'', 1, $headers, [
'http',
'https'], 0);
166 if (!empty($result[
'content'])) {
167 $data = $result[
'content'];
171 file_put_contents($cacheFile, $data);
174 $this->error =
'Error retrieving URL ' . $urlAPI;
178 if (!is_null($data)) {
179 $data = json_decode($data,
true);
181 if (is_array($data)) {
185 foreach ($data[
'data'][
'children'] as $postData) {
186 if ($count >= $maxNb) {
189 $this->posts[$count] = $this->
normalizeData($postData[
'data']);
195 $this->error =
'Invalid data format or empty response';
199 $this->error =
'Failed to retrieve or decode data';
212 if (!is_array($postData)) {
217 'id' => $postData[
'id'] ??
'',
218 'content' => $postData[
'title'] ??
'',
219 'created_at' => $this->
formatDate($postData[
'created'] ??
''),
220 'url' =>
'https://www.reddit.com' . ($postData[
'permalink'] ??
''),
221 'media_url' => $postData[
'thumbnail'] ??
'',
232 $timestamp = is_numeric($dateString) ? (int) $dateString : strtotime($dateString);
233 return $timestamp > 0 ?
dol_print_date($timestamp,
"dayhour",
'tzuserrel') :
'Invalid Date';
252 return $this->authUrl;
Class for handler Reddit.
__construct(array $authParams)
Constructor to initialize RedditHandler.
normalizeData($postData)
Normalize the data fetched from the Reddit API.
fetch($urlAPI, $maxNb=5, $cacheDelay=60, $cacheDir='', $authParams=[])
Fetch Reddit API to retrieve posts.
getPosts()
Get the list of retrieved posts.
authenticate()
Authenticate with Reddit to get an access token.
formatDate($dateString)
Format date for normalize date.
getAuthUrl()
Get url for authenticate with Reddit.
dol_filemtime($pathoffile)
Return time of a file.
dol_is_file($pathoffile)
Return if path is a file.
dol_now($mode='auto')
Return date for now.
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs=null, $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
dol_mkdir($dir, $dataroot='', $newmask='')
Creation of a directory (this can create recursive subdir)
getURLContent($url, $postorget='GET', $param='', $followlocation=1, $addheaders=array(), $allowedschemes=array('http', 'https'), $localurl=0, $ssl_verifypeer=-1)
Function to get a content from an URL (use proxy if proxy defined).
dol_hash($chain, $type='0', $nosalt=0)
Returns a hash (non reversible encryption) of a string.