Skip to content

Commit

Permalink
Release v1.0.0
Browse files Browse the repository at this point in the history
- added support to confix.txt file, and added default file
- added "press enter to continue..."
- added release script
- updated readme
- added generate release zip script
  • Loading branch information
Racle committed Oct 1, 2018
1 parent 81c1831 commit a1af059
Show file tree
Hide file tree
Showing 8 changed files with 537 additions and 245 deletions.
2 changes: 1 addition & 1 deletion .env.default
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
subreddit=r/talesfromtechsupport
max_pages=10
kindle_from_email=user@mail.com
kindle_to_email=user@kindle.com
kindle_to_email=user@kindle.com
7 changes: 6 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
.idea
.git
.env
config.txt
node_modules
output
cover/cover.jpg
cover/cover.jpg
reddit2ebook-linux
reddit2ebook-win.exe
reddit2ebook-macos
releases
49 changes: 41 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,62 @@ Turn any subreddit (selftext only) to an ebook.
App currently loads post starting from newest post available.

It also ignores sticky messages and non-selfpost messages.
## Run via executable
### Usage
- Get lastest release from [releases](https://github.com/Racle/Reddit2Ebook/releases).
- unzip .zip folder
- modify config.txt
- run executable
- Linux
- use terminal to run
- run `chmod +x reddit2ebook-linux`
- run `./reddit2ebook-linux`
- Windows
- run `reddit2ebook-win.exe`
- Mac
- Not tested
- created ebook is found at `./output/<subredditname>.epub`


## requirements
## Run and build
### Requirements
- nodejs
- Tested with `node v10.11.0`

## Usage
### Running locally

- Install required packages `npm i`
- copy `.env.default` to `.env` in root folder
- Install required packages `npm i --production` (you don't need archiver to be installed)
- copy `.env.default` to `.env` **OR** `config.default.txt` to `config.txt` in root folder
- modify `.env` file to your liking
- run with `npm run start` OR `node index.js`
- run with `npm run start` **OR** `node index.js`
- created ebook is found at `./output/<subredditname>.epub`

NOTE: Windows users might need to run `npm i` in cmd with administrator privaledges for node-gyp install to success

### Building executable

- Install required packages `npm i`
- If not installed, install pkg globally `npm i -g pkg`
- run `npm run build-[linux|win|macos]` to build Linux, Windows or macOS (x64) executables.
- compiles executable to root folder

#### Required files

## .env
- cover/Reddit2Ebook.jpg
- .env **OR** config.txt
- index.js **OR** executable file

## .env / config.txt
```
subreddit=r/talesfromtechsupport # Subreddit with r/
max_pages=10 # Maxium pages to loop trough
kindle_to_email=user@kindle.com # Your personal kindle email
kindle_from_email=user@mail.com # Your whitelisted email
kindle_to_email=user@kindle.com # Your personal kindle email (NOT YET AVAILABLE)
kindle_from_email=user@mail.com # Your whitelisted email (NOT YET AVAILABLE)
```

NOTE: config.txt is prioritized over .env

## TODO

- Send to kindle support (https://www.amazon.com/gp/sendtokindle/email)
- Add support for comments
4 changes: 4 additions & 0 deletions config.default.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
subreddit=r/talesfromtechsupport
max_pages=10
kindle_from_email=user@mail.com
kindle_to_email=user@kindle.com
51 changes: 51 additions & 0 deletions generate-release.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
const fs = require('fs');
var pjson = require('./package.json');
const archiver = require('archiver');

// create releases folder if not exist
if (!fs.existsSync('releases')) {
fs.mkdirSync('releases');
}


//async to generate 3 zip files at same time
async function generateRelease(release) {
// create a file to stream archive data to.
const output = fs.createWriteStream(__dirname + '/releases/reddit2ebook-' + pjson.version + "-" + release + '.zip');
const archive = archiver('zip', {
zlib: { level: 9 } // Sets the compression level.
});

output.on('close', function() {
console.log("Release: " + release + ". Total size: " + formatBytes(archive.pointer()));
});

archive.on('error', function(err) {
throw err;
});


archive.pipe(output);

//add executable
const executable = release === 'win' ? 'win.exe' : release;
archive.append(fs.createReadStream('reddit2ebook-' + executable), { name: 'reddit2ebook-' + executable });

//add default config file
archive.append(fs.createReadStream('config.default.txt'), { name: 'config.txt' });

//add default cover file
archive.append(fs.createReadStream('./cover/Reddit2Ebook.jpg'), { name: 'cover/Reddit2Ebook.jpg' });


// finalize the archive (ie we are done appending files but streams have to finish yet)
archive.finalize();
}

// Quick formatting function from https://stackoverflow.com/a/18650828
function formatBytes(a,b){if(0==a)return"0 Bytes";var c=1024,d=b||2,e=["Bytes","KB","MB","GB","TB","PB","EB","ZB","YB"],f=Math.floor(Math.log(a)/Math.log(c));return parseFloat((a/Math.pow(c,f)).toFixed(d))+" "+e[f]}


generateRelease('win');
generateRelease('linux');
generateRelease('macos');
40 changes: 32 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
const dotenv = require('dotenv').config();
const axios = require('axios');
const makepub = require("nodepub");
const decode = require('unescape');
const Jimp = require("jimp");
const fs = require('fs');
const readline = require('readline');

const path = fs.existsSync('./config.txt') ? 'config.txt' : '.env';

//requiring dotenv with .env file OR config.txt file
const dotenv = require('dotenv').config({path: path});

if (dotenv.error) {
return console.log("Missing .env file");
console.log("Missing .env OR config.txt file");
process.exit()
}

let epub;
let maxPages = process.env.max_pages - 1;
let currentPage = 0;
Expand Down Expand Up @@ -39,18 +47,32 @@ generateEbook();


async function generateEbook() {
console.log("Creating ebook from: " + subreddit);
//creating custom cover with subreddit as text
await createCover();
epub = makepub.document(metadata, "./cover/cover.jpg");
epub.addCSS("h1>a{color:inherit;text-decoration:none}");
await getContent("https://old.reddit.com/" + subreddit + '/new.json?limit=10&sort=new');

epub.writeEPUB(function (e) {
await epub.writeEPUB(function (e) {
console.log("Error:", e);
}, './output', subreddit.split("/").pop(), function () {
console.log("EPUB created.")
}, './output', subreddit.split("/").pop(), async function () {
console.log("EPUB created to output/" + subreddit.split("/").pop() + ".epub\n");

// add "Press enter to continue..." after writing epub
// this is for executables, so you can see what app created
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
await rl.question('Press enter to continue...', () => {
rl.close();
});

});



}


Expand All @@ -68,7 +90,7 @@ async function getContent(url) {
decode(c.data.selftext_html).replace("<!-- SC_ON -->", ""));
})

console.log("Current page: " + (currentPage+1));
console.log("Current page: " + (currentPage+1) + "/" + (maxPages+1));

// if there is more pages (data.after) and we are not over maxPages limit, get more pages
if(r.data.data.after && ++currentPage <= maxPages) {
Expand Down Expand Up @@ -98,6 +120,8 @@ async function createCover() {
},
782, // maxWidth (use same width as base cover image to center correctly)
200 // maxHeight
).write("./cover/cover.jpg");
)
.quality(80) // set JPEG quality. We don't need very high quality output.
.write("./cover/cover.jpg");

}
}
Loading

0 comments on commit a1af059

Please sign in to comment.