-
Notifications
You must be signed in to change notification settings - Fork 0
/
sharinpix-import.coffee
42 lines (40 loc) · 1.34 KB
/
sharinpix-import.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
csv = require 'fast-csv'
fs = require 'fs'
sharinpix = require 'sharinpix'
async = require 'async'
_ = require 'lodash'
importImages = (path)->
readStream = fs.createReadStream path
albums = []
import_id = Math.round(Math.random()*10000)
csv
.fromStream readStream, delimiter: ';'
.on 'data', (data)->
[album_id, url, tags, metadatas] = _.values data
metadatas = '{}' if not metadatas? || metadatas == ''
try
json = JSON.parse metadatas
catch error
errorFlag = true
console.error "#{album_id};#{url};#{tags};#{metadatas}"
unless errorFlag
albums.push async.reflect (callback) ->
json['import_id'] = import_id
sharinpix.get_instance().post '/imports',
import_type: 'url'
album_id: album_id
url: url
tags: _.split(tags, ',')
metadatas: json
.then (res)->
{ album_id, url, tags, metadatas } = res.params
console.log "#{album_id};#{url};#{tags};#{JSON.stringify metadatas}"
callback null, res
, (err) ->
callback err, null
.catch (err)->
metadatas = JSON.stringify metadatas
console.error "#{album_id};#{url};#{tags};#{metadatas}"
.on 'end', ->
async.parallelLimit albums, 10
module.exports = importImages