· 7 years ago · Mar 10, 2018, 12:08 AM
1var express = require('express'),
2 fs = require('fs'),
3 flickr = require('flickr-with-uploads');
4 request = require('request'),
5 bodyParser = require('body-parser'),
6 app = express()
7 api = flickr (
8 '########', // consumer_key
9 '########', // consumer_secret
10 '########', // oauth_token
11 '########'
12 ); // oauth_token_secret
13
14
15
16app.use(bodyParser());
17
18
19app.post(/^\/?texted-pictures/i, function(req,res){
20
21 // is there a body of information
22 if (!req.body) {
23 res.send("Posting error");
24 return true;
25 }
26
27 // incoming text message text
28 text_message = req.body.Body;
29
30 // If there's a picture URL
31 if (req.body.MediaUrl0) {
32
33 picture_url = req.body.MediaUrl0;
34 console.log(picture_url);
35
36 // the Twilio URL for the picture
37 var stream = request(picture_url);
38
39 var writeStream = fs.createWriteStream('picture-downloads/picture.jpg');
40
41 stream.on('error', function(err) {
42 console.log('something went wrong with download',err);
43 writeStream.close();
44 });
45
46 stream.on('data', function(data) {
47 writeStream.write(data);
48 });
49
50 stream.on('end', function() {
51 writeStream.end();
52 console.log('picture download done');
53
54 sendToFlickr();
55
56 });
57
58 }
59
60
61 function sendToFlickr(){
62
63api({
64 method: 'upload',
65 title: 'picture',
66 is_public: 1,
67 is_friend: 1,
68 is_family: 1,
69 hidden: 2,
70 photo: "picture-downloads/picture.jpg"
71 }, function(err, response) {
72 if (err) {
73 console.error('Could not upload photo:', err);
74 }
75 else {
76 var new_photo_id = response.photoid._content;
77 api({method: 'flickr.photosets.addPhoto', photoset_id: '72157688611249742', photo_id: new_photo_id }, function(err, response) {
78 console.log('Added photo to photoset:', response);
79 });
80 }
81 });
82};
83
84app.listen(80);
85console.log('running!');