31 lines
		
	
	
		
			1.0 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
			
		
		
	
	
			31 lines
		
	
	
		
			1.0 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
| import Mastodon from "mastodon-api";
 | |
| import fs from "fs";
 | |
| 
 | |
| export class MastodonHandler extends Mastodon{
 | |
|     postStatus(status){
 | |
|         this.post('statuses', {status: status}).then((resp) => {
 | |
|             console.log("status posted => ");
 | |
|             console.log(resp.data);
 | |
|         });
 | |
|     }
 | |
|     postMedia(status, mediaPath, retry, onError){
 | |
|         if (retry <= 0){
 | |
|             onError();
 | |
|             return;
 | |
|         }
 | |
|         this.post('media', { file: fs.createReadStream(mediaPath) }).then(resp => {
 | |
|             if (!resp.data.id){
 | |
|                 this.postMedia(status, mediaPath, --retry);
 | |
|                 console.log("issue while uploading, retry")
 | |
|                 return;
 | |
|             }
 | |
|             let id = resp.data.id;
 | |
|             console.log("media uploaded => ");
 | |
|             console.log(resp.data);
 | |
|             this.post('statuses', { status: status, media_ids: [id] }).then((resp) => {
 | |
|                 console.log("media posted => ");
 | |
|                 console.log(resp.data);
 | |
|             });
 | |
|         });
 | |
|     }
 | |
| } |