Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# Feed
feed
.DS_Store
package-lock.json

# Logs
logs
Expand Down
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,26 @@ next post automatically.

```
fairpost.js help
fairpost.js get-feed [--config=xxx]
fairpost.js get-feed
fairpost.js get-folders [--folders=xxx,xxx]
fairpost.js prepare-posts [--platforms=xxx,xxx] [--folders=xxx,xxx]
fairpost.js get-posts [--status=xxx] [--platforms=xxx,xxx] [--folders=xxx,xxx]
fairpost.js schedule-next-post [--date=xxxx-xx-xx] [--platforms=xxx,xxx] [--folders=xxx,xxx]
fairpost.js publish-due-posts [--platforms=xxx,xxx] [--folders=xxx,xxx] [--dry-run]
```

### Common arguments

```
# Select which config file to use
fairpost.js [command] [arguments] --config=.env-test

# Set the cli output format to pure json
fairpost.js [command] [arguments] --report=json

```


## Add a new platform

To add support for a new platform, add a class to `src/platforms`
Expand Down
146 changes: 93 additions & 53 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
Fairpost cli handler
*/

import Feed from './src/classes/Feed';
import { PostStatus } from './src/classes/Post';
import Feed from './src/Feed';
import { PostStatus } from './src/Post';
import { PlatformSlug } from './src/platforms';

// arguments
Expand All @@ -13,6 +13,7 @@ const COMMAND = process.argv[2] ?? 'help'
// options
const DRY_RUN = !!getOption('dry-run') ?? false;
const CONFIG = (getOption('config') as string ) ?? '.env';
const REPORT = (getOption('report') as string ) ?? 'text';
const PLATFORMS = (getOption('platforms') as string)?.split(',') as PlatformSlug[] ?? undefined;
const FOLDERS = (getOption('folders') as string)?.split(',') ?? undefined;
const DATE = (getOption('date') as string) ?? undefined;
Expand All @@ -32,61 +33,100 @@ function getOption(key:string):boolean|string|null {
/* main */
async function main() {

let result: any = '';
let report = '';

const feed = new Feed(CONFIG);
console.log('Fairpost '+feed.path+' starting .. ',DRY_RUN?'dry-run':'');
console.log();
report += 'Fairpost '+feed.path+' starting .. ',DRY_RUN?'dry-run':'';
report += '\n';

let result: any = '';
switch(COMMAND) {
case 'get-feed':
result = feed;
break;
case 'get-platforms':
result = feed.getPlatforms(PLATFORMS);
break;
case 'get-folders':
result = feed.getFolders(FOLDERS);
break;
case 'get-posts':
result = feed.getPosts({
paths:FOLDERS,
platforms:PLATFORMS,
status: STATUS
});
break;
case 'prepare-posts':
result = await feed.preparePosts({
paths:FOLDERS,
platforms:PLATFORMS
});
break;
case 'schedule-next-posts':
result = feed.scheduleNextPosts(DATE ? new Date(DATE): undefined,{
paths:FOLDERS,
platforms:PLATFORMS
});
break;
case 'publish-due-posts':
result = await feed.publishDuePosts({
paths:FOLDERS,
platforms:PLATFORMS
}, DRY_RUN);
break;
default:
const cmd = process.argv[1];
console.log(`
${cmd} help
${cmd} get-feed [--config=xxx]
${cmd} get-platforms [--platforms=xxx,xxx]
${cmd} get-folders [--folders=xxx,xxx]
${cmd} prepare-posts [--platforms=xxx,xxx] [--folders=xxx,xxx]
${cmd} get-posts [--status=xxx] [--platforms=xxx,xxx] [--folders=xxx,xxx]
${cmd} schedule-next-post [--date=xxxx-xx-xx] [--platforms=xxx,xxx] [--folders=xxx,xxx]
${cmd} publish-due-posts [--platforms=xxx,xxx] [--folders=xxx,xxx] [--dry-run]
`);

try {
switch(COMMAND) {
case 'get-feed':
result = feed;
report = 'Feed: '+feed.path;
break;
case 'get-platforms':
const platforms = feed.getPlatforms(PLATFORMS);
platforms.forEach(platform => {
report += 'Platform: '+platform.slug+'\n';
});
result = platforms;
break;
case 'get-folders':
const folders = feed.getFolders(FOLDERS);
folders.forEach(folder => {
report += 'Folder: '+folder.path+'\n';
});
result = folders;
break;
case 'get-posts':
const allposts = feed.getPosts({
paths:FOLDERS,
platforms:PLATFORMS,
status: STATUS
});
allposts.forEach(post => {
report += post.report();
});
result = allposts;
break;
case 'prepare-posts':
const prepposts = await feed.preparePosts({
paths:FOLDERS,
platforms:PLATFORMS
});
prepposts.forEach(post => {
report += post.report();
});
result = prepposts;
break;
case 'schedule-next-posts':
const nextposts = feed.scheduleNextPosts(DATE ? new Date(DATE): undefined,{
paths:FOLDERS,
platforms:PLATFORMS
});
nextposts.forEach(post => {
report += post.report();
});
result = nextposts;
break;
case 'publish-due-posts':
const pubposts = await feed.publishDuePosts({
paths:FOLDERS,
platforms:PLATFORMS
}, DRY_RUN);
pubposts.forEach(post => {
report += post.report();
});
result = nextposts;
break;
default:
const cmd = process.argv[1];
result = [
`${cmd} help`,
`${cmd} get-feed [--config=xxx]`,
`${cmd} get-platforms [--platforms=xxx,xxx]`,
`${cmd} get-folders [--folders=xxx,xxx]`,
`${cmd} prepare-posts [--platforms=xxx,xxx] [--folders=xxx,xxx]`,
`${cmd} get-posts [--status=xxx] [--platforms=xxx,xxx] [--folders=xxx,xxx]`,
`${cmd} schedule-next-post [--date=xxxx-xx-xx] [--platforms=xxx,xxx] [--folders=xxx,xxx]`,
`${cmd} publish-due-posts [--platforms=xxx,xxx] [--folders=xxx,xxx] [--dry-run]`
];
result.forEach(line => report += '\n'+line);
}
} catch (e) {
console.error(e.getMessage());
}

switch(REPORT) {
case 'json':
console.log(JSON.stringify(result,null,'\t'));
break;
default:
console.log(report);
}
console.log(JSON.stringify(result,null,'\t'));


}
Expand Down
18 changes: 9 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
"dependencies": {
"dotenv": "^16.0.3",
"node-fetch": "^2.6.7",
"sharp": "^0.31.1"
"sharp": "0.31.1"
},
"devDependencies": {
"ts-node": "^10.9.1",
"typescript": "^5.0.4"
}
}
8 changes: 4 additions & 4 deletions src/classes/Feed.ts → src/Feed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import Platform from "./Platform";
import Folder from "./Folder";
import Post from "./Post";
import { PostStatus } from "./Post";
import * as platforms from '../platforms';
import { PlatformSlug } from '../platforms';
import * as platforms from './platforms';
import { PlatformSlug } from './platforms';

export default class Feed {

Expand All @@ -20,7 +20,7 @@ export default class Feed {

constructor(configPath?: string) {
if (configPath) {
const configPathResolved = path.resolve(__dirname+'/../../../'+configPath);
const configPathResolved = path.resolve(__dirname+'/../../'+configPath);
dotenv.config({ path:configPathResolved });
} else {
dotenv.config();
Expand All @@ -33,7 +33,7 @@ export default class Feed {
this.interval = Number(process.env.FAIRPOST_FEED_INTERVAL ?? 7);

const activePlatformSlugs = process.env.FAIRPOST_FEED_PLATFORMS.split(',');
const platformClasses = fs.readdirSync(path.resolve(__dirname+'/../platforms'));
const platformClasses = fs.readdirSync(path.resolve(__dirname+'/platforms'));
platformClasses.forEach(file=> {
const constructor = file.replace('.ts','').replace('.js','');
if (platforms[constructor] !== undefined) {
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion src/classes/Platform.ts → src/Platform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as path from 'path';
import Folder from "./Folder";
import Post from "./Post";
import { PostStatus } from "./Post";
import { PlatformSlug } from "../platforms";
import { PlatformSlug } from "./platforms";



Expand Down
10 changes: 10 additions & 0 deletions src/classes/Post.ts → src/Post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,16 @@ export default class Post {
this.save();
}

report(): string {
let report = '';
report += '\nPost: '+this.platform.slug+' : '+this.folder.path;
report += '\n - valid: '+this.valid;
report += '\n - status: '+this.status;
report += '\n - scheduled: '+this.scheduled;
report += '\n - posted: '+this.posted;
return report;
}

}

export enum PostStatus {
Expand Down
4 changes: 2 additions & 2 deletions src/platforms/AsFacebook.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@

import Ayrshare from "./Ayrshare";
import { PlatformSlug } from ".";
import Folder from "../classes/Folder";
import Post from "../classes/Post";
import Folder from "../Folder";
import Post from "../Post";
import * as fs from 'fs';
import * as sharp from 'sharp';

Expand Down
4 changes: 2 additions & 2 deletions src/platforms/AsInstagram.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@

import Ayrshare from "./Ayrshare";
import { PlatformSlug } from ".";
import Folder from "../classes/Folder";
import Post from "../classes/Post";
import Folder from "../Folder";
import Post from "../Post";
import * as sharp from 'sharp';

export default class AsInstagram extends Ayrshare {
Expand Down
4 changes: 2 additions & 2 deletions src/platforms/AsLinkedIn.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@

import Ayrshare from "./Ayrshare";
import { PlatformSlug } from ".";
import Folder from "../classes/Folder";
import Post from "../classes/Post";
import Folder from "../Folder";
import Post from "../Post";
import * as fs from 'fs';
import * as sharp from 'sharp';

Expand Down
4 changes: 2 additions & 2 deletions src/platforms/AsReddit.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@

import Ayrshare from "./Ayrshare";
import { PlatformSlug } from ".";
import Folder from "../classes/Folder";
import Post from "../classes/Post";
import Folder from "../Folder";
import Post from "../Post";

export default class AsReddit extends Ayrshare {
slug = PlatformSlug.ASREDDIT;
Expand Down
4 changes: 2 additions & 2 deletions src/platforms/AsTikTok.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@

import Ayrshare from "./Ayrshare";
import { PlatformSlug } from ".";
import Folder from "../classes/Folder";
import Post from "../classes/Post";
import Folder from "../Folder";
import Post from "../Post";

export default class AsTikTok extends Ayrshare {
slug = PlatformSlug.ASTIKTOK;
Expand Down
4 changes: 2 additions & 2 deletions src/platforms/AsTwitter.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@

import Ayrshare from "./Ayrshare";
import { PlatformSlug } from ".";
import Folder from "../classes/Folder";
import Post from "../classes/Post";
import Folder from "../Folder";
import Post from "../Post";
import * as fs from 'fs';
import * as sharp from 'sharp';

Expand Down
Loading