Browse Source

chore(cron) add cron job

Joe Richardson 1 year ago
parent
commit
af58d7c6a7
2 changed files with 28 additions and 22 deletions
  1. 27
    22
      index.js
  2. 1
    0
      package.json

+ 27
- 22
index.js View File

@@ -3,42 +3,47 @@ require("dotenv").config();
3 3
 // Imports
4 4
 const http = require("http");
5 5
 const fetch = require("node-fetch");
6
-var Twitter = require("twitter");
6
+const twitter = require("twitter");
7
+const cron = require("node-cron");
8
+
7 9
 
8 10
 // Node
9 11
 const hostname = "127.0.0.1";
10 12
 const port = 3000;
11 13
 
12 14
 // Twitter
13
-const client = new Twitter({
15
+const client = new twitter({
14 16
   consumer_key: process.env.TWITTER_CONSUMER_KEY,
15 17
   consumer_secret: process.env.TWITTER_CONSUMER_SECRET,
16 18
   access_token_key: process.env.TWITTER_ACCESS_TOKEN_KEY,
17 19
   access_token_secret: process.env.TWITTER_ACCESS_TOKEN_SECRET
18 20
 });
19 21
 
22
+// Weather
23
+const API_KEY = process.env.WEATHER_API_KEY;
24
+const LOCATION = process.env.WEATHER_LOCATION;
25
+const url = `http://api.openweathermap.org/data/2.5/weather?q=${LOCATION}&APPID=${API_KEY}&units=metric`;
26
+
20 27
 const server = http.createServer((req, res) => {
21
-  const API_KEY = process.env.WEATHER_API_KEY;
22
-  const LOCATION = process.env.WEATHER_LOCATION;
23
-  const url = `http://api.openweathermap.org/data/2.5/weather?q=${LOCATION}&APPID=${API_KEY}&units=metric`;
24
-
25
-  fetch(url)
26
-    .then(function(response) {
27
-      return response.json();
28
-    })
29
-    .then(function(weather) {
30
-      const output = `Currently in Leicester the tempature is ${weather.main.temp} with a high of ${
31
-        weather.main.temp_min
32
-      } and a low of ${weather.main.temp_max}. #${weather.weather[0].main} #Weather`;
33
-
34
-      const params = { screen_name: 'nodejs' };
35
-      client.post('statuses/update', {status: output}, function(error, tweet, response) {
36
-        if (!error) {
37
-          console.log(tweet);
38
-        }
28
+  cron.schedule('0 0 */3 * * *', function () {
29
+    fetch(url)
30
+      .then(function(response) {
31
+        return response.json();
32
+      })
33
+      .then(function(weather) {
34
+        const output = `Currently in Leicester the tempature is ${weather.main.temp} with a high of ${
35
+          weather.main.temp_min
36
+        } and a low of ${weather.main.temp_max}. #${weather.weather[0].main} #Weather`;
37
+
38
+        const params = { screen_name: "nodejs" };
39
+        client.post("statuses/update", { status: output }, function(error, tweet, response) {
40
+          if (!error) {
41
+            console.log(tweet);
42
+          }
43
+        });
44
+        res.end(output);
39 45
       });
40
-      res.end(output);
41
-    });
46
+  });
42 47
 });
43 48
 
44 49
 server.listen(port, hostname, () => {

+ 1
- 0
package.json View File

@@ -11,6 +11,7 @@
11 11
   "dependencies": {
12 12
     "dotenv": "^6.2.0",
13 13
     "forever": "^0.15.3",
14
+    "node-cron": "^2.0.3",
14 15
     "node-fetch": "^2.3.0",
15 16
     "twitter": "^1.7.1"
16 17
   }