Browse Source

Initial commit

Joe Richardson 1 year ago
commit
eafe703671
4 changed files with 74 additions and 0 deletions
  1. 6
    0
      .env-sample
  2. 5
    0
      .gitignore
  3. 46
    0
      index.js
  4. 17
    0
      package.json

+ 6
- 0
.env-sample View File

@@ -0,0 +1,6 @@
1
+WEATHER_API_KEY=
2
+WEATHER_LOCATION=
3
+TWITTER_CONSUMER_KEY=
4
+TWITTER_CONSUMER_SECRET=
5
+TWITTER_ACCESS_TOKEN_KEY=
6
+TWITTER_ACCESS_TOKEN_SECRET=

+ 5
- 0
.gitignore View File

@@ -0,0 +1,5 @@
1
+.DS_Store
2
+node_modules
3
+.env
4
+package-lock.json
5
+yarn.lock

+ 46
- 0
index.js View File

@@ -0,0 +1,46 @@
1
+require("dotenv").config();
2
+
3
+// Imports
4
+const http = require("http");
5
+const fetch = require("node-fetch");
6
+var Twitter = require("twitter");
7
+
8
+// Node
9
+const hostname = "127.0.0.1";
10
+const port = 3000;
11
+
12
+// Twitter
13
+const client = new Twitter({
14
+  consumer_key: process.env.TWITTER_CONSUMER_KEY,
15
+  consumer_secret: process.env.TWITTER_CONSUMER_SECRET,
16
+  access_token_key: process.env.TWITTER_ACCESS_TOKEN_KEY,
17
+  access_token_secret: process.env.TWITTER_ACCESS_TOKEN_SECRET
18
+});
19
+
20
+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
+        }
39
+      });
40
+      res.end(output);
41
+    });
42
+});
43
+
44
+server.listen(port, hostname, () => {
45
+  console.log(`Server running at http://${hostname}:${port}/`);
46
+});

+ 17
- 0
package.json View File

@@ -0,0 +1,17 @@
1
+{
2
+  "name": "lcweather",
3
+  "version": "1.0.0",
4
+  "description": "Twitter bot for weather",
5
+  "main": "index.js",
6
+  "scripts": {
7
+    "test": "echo \"Error: no test specified\" && exit 1"
8
+  },
9
+  "author": "",
10
+  "license": "ISC",
11
+  "dependencies": {
12
+    "dotenv": "^6.2.0",
13
+    "forever": "^0.15.3",
14
+    "node-fetch": "^2.3.0",
15
+    "twitter": "^1.7.1"
16
+  }
17
+}