Skip to content

Instantly share code, notes, and snippets.

@angusdev
Last active March 19, 2017 02:05
Show Gist options
  • Save angusdev/7d7aa639ff882e36ed47 to your computer and use it in GitHub Desktop.
Save angusdev/7d7aa639ff882e36ed47 to your computer and use it in GitHub Desktop.
Push CSV data to Cyfe in command line
BEGIN{
FS=","
OFS=""
getline
for(i=1;i<=NF;i++)
names[i] = ($i)
}
{
print "{"
for(i=1;i<=NF;i++)
{
printf " %s:%s%s\n",names[i],($i),(i == NF ? "" : ",")
}
print "}\n,"
}
@echo OFF
setlocal
REM Push CSV data to Cyfe using Push API (https://www.cyfe.com/api)
REM
REM http://ellab.org/
REM @angusdev
REM
REM %1 json template file
REM %2 csv file
REM %3 the output json file (you will need to delete it manually)
REM %4 Push API Endpoint
REM
REM convert csv to data json
REM If you need to modify the header of the CSV, you can use below script instead
REM sed "1s/^.*$/\"Column1\",\"Column2\"/" %2 > %2.0.tmp
REM gawk -f csv2json.awk %2.0.tmp > %2.1.tmp
REM del %2.0.tmp
gawk -f csv2json.awk %2 > %2.1.tmp
REM remove the last line (which is a ',')
sed "$ d" %2.1.tmp > %2.2.tmp
REM make the json
set JSON_HEAD="{ "data": ["
call :dequote %JSON_HEAD%
echo %ret% > %3
type %2.2.tmp >> %3
type %1 >> %3
REM remove temp files
del %2.1.tmp
del %2.2.tmp
REM call Cyfe Push API
REM use -k if encounter certificate problem
curl -H "Content-Type: application/json" --data @%3 https://app.cyfe.com/api/push/%4
REM curl -k -H "Content-Type: application/json" --data @%3 https://app.cyfe.com/api/push/%4
endlocal
goto :eof
:dequote
setlocal
rem The tilde in the next line is the really important bit.
set thestring=%~1
endlocal&set ret=%thestring%
goto :eof
:eof

Push CSV Data to Cyfe in command line

Usage

In Cyfe.com

  • Add Widget -> Custom -> Push API
  • Click Configure Widget
  • Copy the API Endpoint

For example, if the API Endpoint is https://app.cyfe.com/api/push/54cbb5c62f21f359341803978798

Run in command line:

./cyfe-push-csv.sh example.tmpl example.csv example.json 54cbb5c62f21f359341803978798
{"status":"ok","message":"Data pushed"}

On error:

./cyfe-push-csv.sh example.tmpl example.csv example.json 54cbb5c62f21f359341803978798
{"status":"error","message":"No data"}

Windows

On Windows, you will need to download the Windows port of gawk, sed and curl. UnxUtils is preferred as there is no need for extra DLLs.

cURL

UnxUtils

GnuWin32

Tested Platforms

  • OS X Mavericks
  • Windows 7 64 bit

Disclaimer

csv2json.awk program is modified from perkee's work

@angusdev
© 2015 ellab.org

#!/bin/sh
#
# Push CSV data to Cyfe using Push API (https://www.cyfe.com/api)
#
# http://ellab.org/
# @angusdev
#
# $1 json template file
# $2 csv file
# $3 the output json file (you will need to delete it manually)
# $4 Push API Endpoint
#
# convert csv to data json
# If you need to modify the header of the CSV, you can use below script instead
# sed '1s/^.*$/"Column1","Column2"/' $2 > $2.0.tmp
# awk -f csv2json.awk $2.0.tmp > $2.1.tmp
# rm -f $2.0.tmp
awk -f csv2json.awk $2 > $2.1.tmp
# remove the last line (which is a ',')
sed '$ d' $2.1.tmp > $2.2.tmp
# make the json
echo "{ \"data\": [" > $3
cat $2.2.tmp >> $3
cat $1 >> $3
# remove temp files
rm -f $2.1.tmp
rm -f $2.2.tmp
# call Cyfe Push API
curl -H "Content-Type: application/json" --data @$3 https://app.cyfe.com/api/push/$4
echo
Date Users
20150101 10
20150102 21
20150103 15
20150104 11
20150105 14
20150106 15
20150107 9
20150108 15
20150109 15
20150110 81
20150111 15
20150112 25
20150113 42
20150114 12
20150115 15
20150116 17
20150117 15
20150118 15
20150119 11
20150120 25
20150121 35
20150122 15
{ "data": [
{
"Date":20150101,
"Users":10
}
,
{
"Date":20150102,
"Users":21
}
,
{
"Date":20150103,
"Users":15
}
],
"onduplicate": {
"Users": "replace"
},
"color": {
"Users": "#52ff7f"
},
"type": {
"Users": "line"
}
}
],
"onduplicate": {
"Users": "replace"
},
"color": {
"Users": "#52ff7f"
},
"type": {
"Users": "line"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment