26require_once DOL_DOCUMENT_ROOT.
'/core/class/socialnetworkmanager.class.php';
41 private $clientSecret;
66 private $authUrl =
'https://www.reddit.com/api/v1/access_token';
85 $this->clientId = $authParams[
'client_id'] ??
'';
86 $this->clientSecret = $authParams[
'client_secret'] ??
'';
87 $this->username = $authParams[
'username'] ??
'';
88 $this->password = $authParams[
'password'] ??
'';
89 $this->userAgent = ($authParams[
'name_app'] ??
'').
'/0.1 by '.($authParams[
'username'] ??
'');
101 'grant_type' =>
'password',
102 'username' => $this->username,
103 'password' => $this->password,
104 'scope' =>
'read identity'
108 'Authorization: Basic ' . base64_encode($this->clientId .
':' . $this->clientSecret),
109 'Content-Type: application/x-www-form-urlencoded',
110 'User-Agent: ' . $this->userAgent
113 $result =
getURLContent($this->
getAuthUrl(),
'POST', http_build_query($authData), 1, $headers, [
'http',
'https'], 0);
115 if (!empty($result[
'content'])) {
116 $data = json_decode($result[
'content'],
true);
117 if (isset($data[
'access_token'])) {
118 $this->accessToken = $data[
'access_token'];
121 $this->error = $data[
'error'] ??
'Unknown error during authentication';
125 $this->error =
'Authentication failed. No content received.';
140 public function fetch($urlAPI, $maxNb = 5, $cacheDelay = 60, $cacheDir =
'', $authParams = [])
142 if (empty($this->accessToken) && !$this->
authenticate()) {
146 $cacheFile = $cacheDir .
'/' .
dol_hash($urlAPI,
'3');
147 $foundInCache =
false;
151 if ($cacheDelay > 0 && $cacheDir &&
dol_is_file($cacheFile)) {
153 if ($fileDate >= (
dol_now() - $cacheDelay)) {
154 $foundInCache =
true;
155 $data = file_get_contents($cacheFile);
159 if (!$foundInCache) {
161 'Authorization: Bearer ' . $this->accessToken,
162 'User-Agent: ' . $this->userAgent,
165 $result =
getURLContent($urlAPI,
'GET',
'', 1, $headers, [
'http',
'https'], 0);
167 if (!empty($result[
'content'])) {
168 $data = $result[
'content'];
172 file_put_contents($cacheFile, $data);
175 $this->error =
'Error retrieving URL ' . $urlAPI;
179 if (!is_null($data)) {
180 $data = json_decode($data,
true);
182 if (is_array($data)) {
186 foreach ($data[
'data'][
'children'] as $postData) {
187 if ($count >= $maxNb) {
190 $this->posts[$count] = $this->
normalizeData($postData[
'data']);
196 $this->error =
'Invalid data format or empty response';
200 $this->error =
'Failed to retrieve or decode data';
213 if (!is_array($postData)) {
218 'id' => $postData[
'id'] ??
'',
219 'content' => $postData[
'title'] ??
'',
220 'created_at' => $this->
formatDate($postData[
'created'] ??
''),
221 'url' =>
'https://www.reddit.com' . ($postData[
'permalink'] ??
''),
222 'media_url' => $postData[
'thumbnail'] ??
'',
233 $timestamp = is_numeric($dateString) ? (int) $dateString : strtotime($dateString);
234 return $timestamp > 0 ?
dol_print_date($timestamp,
"dayhour",
'tzuserrel') :
'Invalid Date';
253 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.