Permalink
Browse files

first version online

  • Loading branch information...
1 parent 71fcfab commit c8c19d8384cfe35c0fd4bf5051b046c846fa0436 @mariokonschake committed Apr 29, 2012
View
@@ -0,0 +1,10 @@
+var fs = require('fs');
+
+var stream = fs.createWriteStream('../log/server.log', {flags: 'a'});
+
+function log(text) {
+ stream.write('[' + Date() + '] ' + text + '.\n\n');
+};
+
+exports.log = log;
+exports.stream = stream;
View
@@ -0,0 +1,21 @@
+var fs = require('fs');
+
+function read(time, callback) {
+ fs.readFile('../database/subscriptions.json', function(error, data) {
+ if (error) {
+ callback(error, null)
+ } else {
+ subscriptionsJSON = JSON.parse(data)[time + 'CET'];
+ subscriptions = new Array();
+ for (paper in subscriptionsJSON) {
+ subscriptions.push({'name': paper, 'subscribers': subscriptionsJSON[paper]});
+ };
+ callback(null, subscriptions);
+ };
+ });
+};
+
+read("4", function(err, sub) { console.log(sub) });
+
+exports.read = read;
+
View
@@ -0,0 +1,37 @@
+var nodemailer = require('nodemailer');
+var fs = require('fs');
+var database = require('database');
+
+function send(subscription, callback) {
+
+ var myAuth = JSON.parse(fs.readFileSync('../database/.authSendGrid.json'));
+
+ // opens pool of SMTP connections
+ var smtpTransport = nodemailer.createTransport('SMTP', myAuth);
+
+ // setup e-mail data
+ var mailOptions = {
+ from: 'Zustellung <zustellung@zeitungsjunge.tk>',
+ to: subscription['subscribers'].join(', '),
+ subject: subscription['name'],
+ text: '',
+ attachments:[{
+ fileName: subscription['name'] + '.mobi',
+ streamSource: fs.createReadStream( 'mobi/' + subscription['name'] + '.mobi'),
+ cid: new Date().getTime() // should be as unique as possible
+ }]
+ };
+
+ // send e-mail
+ smtpTransport.sendMail(mailOptions, function(error, response){
+ if(error){
+ callback(error, response);
+ } else {
+ callback(null, response);
+ };
+ smtpTransport.close();
+ });
+};
+
+
+exports.send = send;
View
@@ -0,0 +1,83 @@
+var database = require('./database'),
+ mailer = require ('./mailer'),
+ exec = require('child_process').exec,
+ fs = require('fs'),
+ cronJob = require('cron').CronJob,
+ async = require('async'),
+ inspect = require("util").inspect,
+ logger = require('../log/logger');
+
+function sendPaper(subscription, callback) {
+ //downloading websites using calibre (http://calibre-ebook.com)
+ var recipeName = {Faz: '"FAZ.NET.recipe"',
+ Tagesspiegel: '"Der Tagesspiegel.recipe"',
+ Sueddeutsche: '"Süddeutsche.recipe"',
+ Zeit: '"Zeit Online.recipe"',
+ Taz: '"Taz.de (die tageszeitung) RSS Feed - German.recipe"',
+ SpOn: '"Spiegel Online - German.recipe"'
+ };
+ exec('ebook-convert ' + recipeName[subscription["name"]] + ' mobi/' + subscription["name"] + '.mobi', function (error, stdout, stderr) {
+ if (error) {
+ logger.log('Download error: ' + subscription["name"] + '. Calibre output following');
+ logger.log('stdout: ' + stdout);
+ logger.log('stderr: ' + stderr);
+ callback(error, null);
+ } else {
+ logger.log ('Successful download of ' + subscription["name"]);
+ //send .mobi to kindle subscribers
+ mailer.send(subscription, function(error, response) {
+ if (error) {
+ logger.log('Sending error. Response: ' + response);
+ logger.log('List of subscribers: ' + subscription['subscribers']);
+ callback(error, null);
+ } else {
+ logger.log(subscription['name'] + ' successful sent to: ' + subscription['subscribers']);
+ callback(null, null);
+ };
+ });
+ };
+ });
+};
+
+
+var startJob = function(time) {
+
+ //start cron job to download and send paper daily at <time> o'clock
+ new cronJob(time + ' ' + time + ' ' + time + ' * * *', function() {
+ logger.log('Start cron job: ' + time + 'h');
+
+ //read name of papers and its subscribers from database
+ database.read(time, function (error, subscriptions) {
+ if (error) {
+ logger.log('Data base error: '+ error);
+ } else {
+ //download all papers, convert to .mobi format and send to subscribers
+ async.forEachLimit(subscriptions, 2 ,sendPaper, function(error) {
+ if (error) {
+ logger.log('Sending error: ' + error);
+ } else {
+ logger.log('Finished cron job: ' + time + "h");
+ };
+ });
+ };
+ });
+ }, null, true);
+};
+
+var sendMail = function(timeStr) {
+ switch (timeStr) {
+ case 'morning': startJob('4');
+ break;
+ case 'noon': startJob('9');
+ break;
+ case 'evening': startJob('15');
+ break;
+ default: throw new Error('Unaccepted time string.');
+ };
+};
+
+logger.log('SendServer started');
+sendMail('morning');
+sendMail('noon');
+sendMail('evening');
+
View
@@ -0,0 +1,72 @@
+var fs = require("fs");
+
+/*
+ * using blocking functions to permit two requests changing
+ * the database file at the same time on the cost of poor scaling
+ */
+
+var databaseFilename = '../database/subscriptions.json';
+
+function init () {
+ var subscriptions = {
+ "4CET" : {},
+ "9CET" : {},
+ "15CET" : {}
+ };
+ fs.writeFileSync(databaseFilename, JSON.stringify(subscriptions, null, 2));
+};
+
+function removeSubscription(formData) {
+ subscriptions = JSON.parse(fs.readFileSync(databaseFilename));
+
+ //remove subscriber
+ for (var time in subscriptions) {
+ for (var paper in subscriptions[time]) {
+ var idx = subscriptions[time][paper].indexOf(formData.email);
+ if (idx !== -1) {
+ subscriptions[time][paper].splice(idx, 1);
+ };
+ };
+ };
+
+ //remove empty subscriptions
+ for (var time in subscriptions) {
+ for (var paper in subscriptions[time]) {
+ if (subscriptions[time][paper].length === 0) {
+ delete subscriptions[time][paper];
+ };
+ };
+ };
+
+ fs.writeFileSync(databaseFilename, JSON.stringify(subscriptions, null, 2));
+};
+
+
+function addSubscription(formData) {
+ subscriptions = JSON.parse(fs.readFileSync(databaseFilename));
+
+ if ('subscribeTo' in formData && 'email' in formData) {
+
+ //single subscription
+ if (typeof formData.subscribeTo === 'string') {
+ formData.subscribeTo = new Array(formData.subscribeTo);
+ }
+
+ for (var i = 0; i < formData.subscribeTo.length; i++) {
+ var paper = formData.subscribeTo[i].split("_")[0];
+ var time = formData.subscribeTo[i].split("_")[1];
+
+ if (!(paper in subscriptions[time])) {
+ subscriptions[time][paper] = new Array();
+ };
+ if (subscriptions[time][paper].indexOf(formData.email) === -1) {
+ subscriptions[time][paper].push(formData.email);
+ };
+ };
+
+ fs.writeFileSync(databaseFilename, JSON.stringify(subscriptions, null, 2));
+ };
+};
+
+exports.addSubscription = addSubscription;
+exports.removeSubscription = removeSubscription;
View
@@ -0,0 +1,72 @@
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
+<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
+<head>
+ <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
+
+ <title>Zeitungsjunge: Unsubscribe</title>
+ <link rel="stylesheet" type="text/css" href="main.css">
+
+<style type="text/css"></style></head>
+<body>
+ <div id="page">
+ <div id="header">
+ <div class="wrap">
+ <p class="head">
+ <a href="/home" style="text-decoration: none;">
+ <span style="color:#ffcc32">zeitungsjunge</span>
+ </a>
+ </p>
+ <p class="tagline">Subscribe to online news for your Kindle</p>
+ <ul class="nav">
+ <li><a href="/home" title="home">Home</a></li>
+ <li>•</li>
+ <li><a href="/subscribe" title="subscribe">Subscribe</a></li>
+ <li>•</li>
+ <li><a href="/unsubscribe" title="unsubscribe">Unsubscribe</a></li>
+ <li>•</li>
+ <li><a href="/faq" title="faq">FAQ</a></li>
+ <li>•</li>
+ <li><a href="/contact" title="contact">Contact</a></li>
+ </ul>
+ </div>
+ </div>
+ <div id="main">
+ <div class="wrap" style="text-align:center;">
+ <img src="/image">
+ </div>
+ </div>
+
+ <div id="content">
+ <div class="wrap">
+ If you need to tell me something, this is the place. Every feedback is appreciated.
+ </div>
+ </div>
+
+ <div id="submain2" style="line-height:1.5em;">
+
+
+ <div class="wrap">
+ <form action="/contactConfirm", method="post">
+ <h2>Your Kindle e-mail</h2>
+ <p>
+ <input name="kindleEmail" type="text" size="30" maxlength="50", value="@kindle.com">
+ </p><br>
+ <h2>Your contact e-mail</h2>
+ <p>
+ <input name="contactEmail" type="text" size="30" maxlength="50">
+ </p><br>
+ <h2>Your message</h2>
+ <p>
+ <textarea name="message" type="text" rows="7" cols="50"></textarea>
+ </p><br>
+
+
+ <h2></h2>
+ <div align="center">
+ <input type="submit" value=" Submit ", class="awesome large orange">
+ <br><br>
+ </div>
+
+ </form>
+
+</body></html>
@@ -0,0 +1,45 @@
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
+<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
+<head>
+ <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
+
+ <title>Zeitungsjunge Unsubscribe</title>
+ <link rel="stylesheet" type="text/css" href="main.css">
+
+<style type="text/css"></style></head>
+<body>
+ <div id="page">
+ <div id="header">
+ <div class="wrap">
+ <p class="head">
+ <a href="/home" style="text-decoration: none;">
+ <span style="color:#ffcc32">zeitungsjunge</span>
+ </a>
+ </p>
+ <p class="tagline">Subscribe to online news for your Kindle</p>
+ <ul class="nav">
+ <li><a href="/home" title="home">Home</a></li>
+ <li>•</li>
+ <li><a href="/subscribe" title="subscribe">Subscribe</a></li>
+ <li>•</li>
+ <li><a href="/unsubscribe" title="Unsubscribe">Unsubscribe</a></li>
+ <li>•</li>
+ <li><a href="/faq" title="faq">FAQ</a></li>
+ <li>•</li>
+ <li><a href="/contact" title="contact">Contact</a></li>
+ </ul>
+ </div>
+ </div>
+ <div id="main">
+ <div class="wrap" style="text-align:center;">
+ <img src="/image">
+ </div>
+ </div>
+
+ <div id="content">
+ <div class="wrap">
+ Thank you for the feedback. I will get back to you as soon as possible.
+ </div>
+ </div>
+
+</body></html>
View
@@ -0,0 +1,47 @@
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
+<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
+<head>
+ <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
+
+ <title>Zeitungsjunge Unsubscribe</title>
+ <link rel="stylesheet" type="text/css" href="main.css">
+
+<style type="text/css"></style></head>
+<body>
+ <div id="page">
+ <div id="header">
+ <div class="wrap">
+ <p class="head">
+ <a href="/home" style="text-decoration: none;">
+ <span style="color:#ffcc32">zeitungsjunge</span>
+ </a>
+ </p>
+ <p class="tagline">Subscribe to online news for your Kindle</p>
+ <ul class="nav">
+ <li><a href="/home" title="home">Home</a></li>
+ <li>•</li>
+ <li><a href="/subscribe" title="subscribe">Subscribe</a></li>
+ <li>•</li>
+ <li><a href="/unsubscribe" title="Unsubscribe">Unsubscribe</a></li>
+ <li>•</li>
+ <li><a href="/faq" title="faq">FAQ</a></li>
+ <li>•</li>
+ <li><a href="/contact" title="contact">Contact</a></li>
+ </ul>
+ </div>
+ </div>
+ <div id="main">
+ <div class="wrap" style="text-align:center;">
+ <img src="/image">
+ </div>
+ </div>
+
+ <div id="content">
+ <div class="wrap">
+ Oops. Something went wrong. Please try again.
+ <br><br>
+ An error report was just send to me, but you can also <a href="/contact" title="contact">contact</a> me.
+ </div>
+ </div>
+
+</body></html>
Oops, something went wrong.

0 comments on commit c8c19d8

Please sign in to comment.