-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.coffee
101 lines (77 loc) · 2.16 KB
/
client.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
_ = require('underscore')
http = require('http')
crypto = require('crypto')
debug = require('debug')('FFS:PhotoClient')
class FlickrPhotoClient
constructor: (@client, @opts={}) ->
makeRequest: (method, params, cb) ->
@client.createRequest(method, params, true, cb).send()
writePhoto: (path, dataStream, opts={}, cb) ->
if _.isFunction opts
cb = opts
opts = {}
params =
title: @encodeTitle path
is_public: 0
is_friend: 0
is_family: 0
hidden: 2
photo: dataStream
tags = ['fs']
if opts.tags?
tags.push.apply(tags, opts.tags)
debug tags
params.tags = tags.join(' ')
_.extend params, opts.params
debug 'uploading'
@makeRequest 'upload', params, (err, resp) ->
debug 'done'
cb(err, resp?.photoid)
readPhoto: (id, cb) ->
debug 'getting sizes'
@readSizes id, (err, sizes) =>
return cb(err) if err?
debug 'read sizes'
url = @getOriginalURL sizes
@readURL url, cb
readURL: (url, cb) ->
debug 'getting'
http.get url, (stream) ->
debug 'got'
cb null, stream
getOriginalURL: (sizes) ->
for size in sizes
if size.label is 'Original'
return size.source
throw "Original photo cannot be found in response."
writePhotoTags: (id, tags, cb) ->
if _.isArray(tags)
tags = tags.join(' ')
params =
photo_id: id
tags: tags
@makeRequest 'flickr.photos.setTags', params, cb
encodeTitle: (path) ->
if @opts.encodePath
crypto.createHash('sha1').update(path).digest('hex')
else
path
readSizes: (id, cb) ->
params =
photo_id: id
@makeRequest 'flickr.photos.getSizes', params, (err, resp) ->
return cb(err) if err?
cb(null, resp.sizes.size)
readPhotoInfo: (id, cb) ->
params =
photo_id: id
@makeRequest 'flickr.photos.getInfo', params, (err, resp) ->
if err?
if err.message?.indexOf('not found') isnt -1
err.code = 'ENOENT'
return cb(err)
for key, val of resp.photo
if val?._content
resp.photo[key] = val._content
cb(null, resp.photo)
module.exports = FlickrPhotoClient