Added recurrence management; fixed multidate interpretation; bumped version to 0.5.6
This commit is contained in:
parent
490c32a157
commit
579a723a10
1 changed files with 43 additions and 11 deletions
54
ganciof2f
54
ganciof2f
|
@ -18,7 +18,7 @@
|
|||
|
||||
$SNAME='GancioF2F';
|
||||
$ENAME=strtolower($SNAME);
|
||||
$SVERS='0.5.5';
|
||||
$SVERS='0.5.6';
|
||||
|
||||
require __DIR__.'/lib/getconf.php';
|
||||
require __DIR__.'/lib/ckmkeys.php';
|
||||
|
@ -362,7 +362,7 @@ if ($feed['httpcode']!='200') dieYoung("Error: «{$url} returned http code «{$r
|
|||
$feed=$feed['content'];
|
||||
if (is_null($feed)) dieYoung("Error: got no valid JSON from «{$url}».\n",1);
|
||||
//print_r($feed);
|
||||
$buff=['id', 'title', 'slug', 'description', 'multidate', 'start_datetime', 'end_datetime', 'media', 'online_locations', 'updatedAt', 'tags', 'place'];
|
||||
$buff=['id', 'title', 'slug', 'description', 'multidate', 'start_datetime', 'end_datetime', 'media', 'online_locations', 'updatedAt', 'parentId', 'tags', 'place'];
|
||||
foreach ($feed as $item)
|
||||
if (!ckmkeys($buff,$item)) dieYoung("Error: feed from «{$url}» had unexpected format.\n",1);
|
||||
$itemsCount=count($feed);
|
||||
|
@ -370,6 +370,7 @@ if ($itemsCount==0) exitYoung("Info: feed from «{$url}» was empty, bye.\n");
|
|||
vecho($opts['verbose'],"Info: got feed with {$itemsCount} announcement(s) from «{$url}».\n");
|
||||
//file_put_contents(__DIR__.'/storage/dump-'.time(),print_r($feed,true));
|
||||
|
||||
$ordinals=[-1=>_('ultimo'), 1=>_('primo'), 2=>_('secondo'), 3=>_('terzo'), 4=>_('quarto'), 5=>_('quinto'), 6=>_('sesto'), 7=>_('settimo'), 8=>_('ottavo'), 9=>_('nono'), 10=>_('decimo')];
|
||||
//date_default_timezone_set($conf['timezone']);
|
||||
//$dfmt=datefmt_create('it',IntlDateFormatter::FULL,IntlDateFormatter::SHORT,$conf['timezone'],IntlDateFormatter::GREGORIAN,"eeee d MMMM '"._('alle')."' HH:mm");
|
||||
$tsfp="{$conf['state_file_absolute_path']}.tmp";
|
||||
|
@ -400,11 +401,10 @@ foreach ($feed as $index=>$item) {
|
|||
} else {
|
||||
$itemsToPost++;
|
||||
$postHead="{$item['title']}\n\n";
|
||||
// ogni settimana: 1w; ogni due settimane: 2w; ogni giorno 6 di ogni mese: 1m, type ordinal; ogni primo mercoledì del mese: 1m senza type
|
||||
if ($item['multidate']) {
|
||||
$dfmt=datefmt_create($conf['posts_language'],0,0,$conf['timezone'],null,"eeee d MMMM");
|
||||
$postHead.=_('Tutti i giorni da').' '.datefmt_format($dfmt,$item['start_datetime']).' '._('a').' '.datefmt_format($dfmt,$item['end_datetime']).', ';
|
||||
$dfmt=datefmt_create($conf['posts_language'],0,0,$conf['timezone'],null,"HH:mm");
|
||||
$postHead.=_('dalle').' '.datefmt_format($dfmt,$item['start_datetime']).' '._('alle').' '.datefmt_format($dfmt,$item['end_datetime']);
|
||||
$dfmt=datefmt_create($conf['posts_language'],0,0,$conf['timezone'],null,"eeee d MMMM '"._('alle')."' HH:mm");
|
||||
$postHead.=_('Da').' '.datefmt_format($dfmt,$item['start_datetime']).' '._('a').' '.datefmt_format($dfmt,$item['end_datetime']);
|
||||
} else {
|
||||
$dfmt=datefmt_create($conf['posts_language'],0,0,$conf['timezone'],null,"eeee d MMMM '"._('dalle')."' HH:mm");
|
||||
$postHead.=mb_ucfirst(datefmt_format($dfmt,$item['start_datetime']));
|
||||
|
@ -412,8 +412,40 @@ foreach ($feed as $index=>$item) {
|
|||
$dfmt=datefmt_create($conf['posts_language'],0,0,$conf['timezone'],null,"HH:mm");
|
||||
$postHead.=' '._('alle').' '.datefmt_format($dfmt,$item['end_datetime']);
|
||||
}
|
||||
if (!is_null($item['parentId'])) {
|
||||
$url="https://{$conf['feed_hostname']}/api/event/detail/{$item['parentId']}";
|
||||
vecho($opts['verbose'],"Info: looks like announcement «{$postUrl}» is recurrent, trying to fetch recurrence info from «{$url}».\n");
|
||||
$res=curl($url,'gancio/api/event/detail',['Accept: application/json']);
|
||||
if ($res['content']===false) {
|
||||
fwrite(STDERR,"Warning: could not connect to «{$url}»: «{$res['error']}»; won’t add recurrence.\n");
|
||||
} elseif (is_null($res['content']=@json_decode($res['content'],true))) {
|
||||
fwrite(STDERR,"Warning: «{$url}» did not return valid JSON; won’t add recurrence.\n");
|
||||
} elseif ($res['httpcode']!='200') {
|
||||
(isset($res['content']['error'])) ? $buff=" (error: «{$res['content']['error']}»)" : $buff='';
|
||||
fwrite(STDERR,"Warning: «{$url}» returned http code «{$res['httpcode']}»{$buff}; won’t add recurrence.\n");
|
||||
} elseif (!isset($res['content']['recurrent']['frequency'])) {
|
||||
fwrite(STDERR,"Warning: no frequency info in JSON from «{$url}»; won’t add recurrence.\n");
|
||||
} else {
|
||||
vecho($opts['verbose'],"Info: got good recurrence info from «{$url}».\n");
|
||||
$res=$res['content'];
|
||||
if ($res['recurrent']['frequency']=='1w') {
|
||||
$dfmt=datefmt_create($conf['posts_language'],0,0,$conf['timezone'],null,"eeee");
|
||||
$postHead.=' ('.sprintf(_('e ogni %s'),datefmt_format($dfmt,$res['start_datetime'])).')';
|
||||
} elseif ($res['recurrent']['frequency']=='2w') {
|
||||
$dfmt=datefmt_create($conf['posts_language'],0,0,$conf['timezone'],null,"eeee");
|
||||
$postHead.=' ('.sprintf(_('e un %s ogni due'),datefmt_format($dfmt,$res['start_datetime'])).')';
|
||||
} elseif ($res['recurrent']['frequency']=='1m' && isset($res['recurrent']['type']) && $res['recurrent']['type']=='ordinal') {
|
||||
$dfmt=datefmt_create($conf['posts_language'],0,0,$conf['timezone'],null,"d");
|
||||
$postHead.=' ('.sprintf(_('e il giorno %d di ogni mese'),datefmt_format($dfmt,$res['start_datetime'])).')';
|
||||
} elseif ($res['recurrent']['frequency']=='1m' && isset($res['recurrent']['type']) && array_key_exists($res['recurrent']['type'],$ordinals)) {
|
||||
$dfmt=datefmt_create($conf['posts_language'],0,0,$conf['timezone'],null,"eeee");
|
||||
$postHead.=' ('.sprintf(_('e ogni %s %s del mese'),$ordinals[$res['recurrent']['type']],datefmt_format($dfmt,$res['start_datetime'])).')';
|
||||
} else {
|
||||
fwrite(STDERR,"Warning: could not decode recurrence info in JSON from «{$url}»; won’t add recurrence.\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// if (isset($item['parentId']))// this probably means it's a recurring event, but i see no way to check *when* it is recurring
|
||||
$postHead.=', '._('presso')." {$item['place']['name']}, {$item['place']['address']}";
|
||||
if (is_array($item['online_locations']) && count($item['online_locations'])>0) $postHead.='; '._('e anche online su ').implode(' - ',$item['online_locations']);
|
||||
$postBody='';
|
||||
|
@ -445,7 +477,7 @@ foreach ($feed as $index=>$item) {
|
|||
$post="{$postHead}{$postBody}[…]{$postLink}{$postTags}";
|
||||
$postLen=postLength($post,$tldsregex['tlds']);
|
||||
}
|
||||
// echo "@@@ {$postUrl}: {$postLen} @@@\n{$post}\n---\n";
|
||||
// echo "@@@ {$postUrl} ({$postLen}) @@@\n{$post}\n---\n";
|
||||
if ($postLen>$conf['max_post_length']) {
|
||||
fwrite(STDERR,"Warning: could not shrink post for {$state} announcement «{$postUrl}» into {$conf['max_post_length']} characters; won’t try to post.\n");
|
||||
} elseif (!$opts['do-post'] || ($opts['test'] && $index+1<$itemsCount)) {
|
||||
|
@ -480,7 +512,7 @@ foreach ($feed as $index=>$item) {
|
|||
} else {
|
||||
$postData=['file'=>curl_file_create($tfp,$type,'file'), 'description'=>$item['media'][0]['name']];
|
||||
$url="https://{$conf['fedi_hostname']}/api/v2/media";
|
||||
$res=curl($url,'/api/v2/media',["Authorization: Bearer {$conf['fedi_token']}", 'Accept: application/json'],$postData);
|
||||
$res=curl($url,'fedi/api/v2/media',["Authorization: Bearer {$conf['fedi_token']}", 'Accept: application/json'],$postData);
|
||||
unset($postData);
|
||||
if ($res['content']===false) {
|
||||
fwrite(STDERR,"Warning: could not connect to «{$url}»: «{$res['error']}»; won’t try to post.\n");
|
||||
|
@ -499,7 +531,7 @@ foreach ($feed as $index=>$item) {
|
|||
while ($res['httpcode']!='200' && $i<5) {
|
||||
sleep(2);
|
||||
$url="https://{$conf['fedi_hostname']}/api/v1/media/{$res['id']}";
|
||||
$res=curl($url,'/api/v1/media',["Authorization: Bearer {$conf['fedi_token']}", 'Accept: application/json']);
|
||||
$res=curl($url,'fedi/api/v1/media',["Authorization: Bearer {$conf['fedi_token']}", 'Accept: application/json']);
|
||||
if ($res['content']!==false && $res['httpcode']=='200' && !is_null($res['content']=@json_decode($res,true)) && isset($res['content']['id'])) $id=$res['content']['id'];
|
||||
$i++;
|
||||
}
|
||||
|
@ -527,7 +559,7 @@ foreach ($feed as $index=>$item) {
|
|||
$postData['language']=$conf['posts_language'];
|
||||
$url="https://{$conf['fedi_hostname']}/api/v1/statuses";
|
||||
$headers=["Authorization: Bearer {$conf['fedi_token']}", 'Accept: application/json', 'Idempotency-Key: '.md5(implode('-',$postData).time())];
|
||||
$res=curl($url,'/api/v1/statuses',$headers,$postData);
|
||||
$res=curl($url,'fedi/api/v1/statuses',$headers,$postData);
|
||||
if ($res['content']===false) {
|
||||
fwrite(STDERR,"Warning: could not connect to «{$url}»: «{$res['error']}»; could not post status for {$state} announcement «{$postUrl}».\n");
|
||||
} elseif (is_null($res['content']=@json_decode($res['content'],true))) {
|
||||
|
|
Loading…
Reference in a new issue