Advertisement
Guest User

Untitled

a guest
May 23rd, 2018
451
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.27 KB | None | 0 0
  1. curl -d "apiKey=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxx
  2. &fileUri=%2Fpath%2Fto%2Ffolder%2Fstrings.xml
  3. &projectId=1234567890" "https://api.smartling.com/v1/file/get/"
  4.  
  5. ./download-smartling-files.sh -t published -a xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxx -p 1234567890 -l "nl-NL ru-RU"
  6.  
  7. ./download-smartling-files.sh -t pending -a xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxx -p 1234567890 -l "nl-NL ru-RU"
  8.  
  9. ./download-smartling-files.sh
  10.  
  11. #!/bin/bash
  12. # author: Eric Negron (enegron@smartling.com)
  13. # last updated February 20, 2014
  14.  
  15. shopt -s nullglob
  16.  
  17.  
  18. function delete_files () {
  19.  
  20. # print the files first to make damn sure they know what they are deleting
  21. COUNT=1
  22. for CURRENT_FILE in ${uris[@]}
  23. do
  24. echo $'n'$COUNT" "$CURRENT_FILE
  25. ((COUNT++))
  26. done
  27.  
  28. echo "Are you SURE you want to delete all "${#uris[@]}" files?"
  29.  
  30. # confirm by getting the user to enter 'YES' to input, and if YES then delete these files
  31. COUNT=1
  32. read delete_confirmation
  33. if [ $delete_confirmation != "YES" ]; then
  34. echo "sorry, must answer YES to delete. exiting"
  35. exit
  36.  
  37. else
  38. for CURRENT_FILE in ${uris[@]}
  39. do
  40. echo $'n'$COUNT" "$CURRENT_FILE
  41. curl -sS -d "apiKey=$SL_APIKEY&projectId=$SL_PROJECT&fileUri=$CURRENT_FILE" "https://$SERVER_URL/v1/file/delete"
  42. ((COUNT++))
  43. done
  44. fi
  45. }
  46.  
  47. function usage {
  48. cat << EOF
  49. This script uses the Smartling API
  50. OPTIONS:
  51. -h Show this message
  52. -t (REQUIRED) download type. Acceptable types: original | published | p100 | pseudo | pending | TMX | DELETE
  53. -a (REQUIRED) API key
  54. -p (REQUIRED) project ID
  55. -u (OPTIONAL) URI mask
  56. -l (OPTIONAL) locales list - a qouted array of Smartling Locales. E.G. "es-ES ja-JP"
  57. -x (OPTIONAL) TMX download type: full (default) | published - only valid with -t TMX
  58. -d (OPTIONAL) when downloading translations or TMX set this option to false to append filename with locale instead of creating locale folders. Not valid with -t original option.
  59. -E (OPTIONAL) DELETE files -E must be true and type = DELETE and must use -u MASK option
  60. EOF
  61. }
  62.  
  63. # hardcode to regular API, not sandbox
  64. SERVER_URL=api.smartling.com
  65.  
  66. #opt t
  67. DL_TYPE=
  68.  
  69. #opt a
  70. SL_APIKEY=
  71.  
  72. #opt p
  73. SL_PROJECT=
  74.  
  75. #opt u
  76. URI_MASK=
  77.  
  78. #opt u
  79. LOCALES=
  80.  
  81. #opt x
  82. TMX_FLAG="full"
  83.  
  84. #opt d
  85. USE_LOCALE_DIR="true"
  86.  
  87. #opt E
  88. DELETE_CONFIRM="false"
  89.  
  90. while getopts "ht:a:p:u:l:x:d:E:" OPTION
  91. do
  92. case $OPTION in
  93. h)
  94. usage
  95. exit 1
  96. ;;
  97. t)
  98. DL_TYPE=$OPTARG
  99. ;;
  100. a)
  101. SL_APIKEY=$OPTARG
  102. ;;
  103. p)
  104. SL_PROJECT=$OPTARG
  105. ;;
  106. u)
  107. URI_MASK=$OPTARG
  108. ;;
  109. l)
  110. LOCALES=($OPTARG)
  111. ;;
  112. x)
  113. TMX_FLAG=$OPTARG
  114. ;;
  115. d)
  116. USE_LOCALE_DIR=$OPTARG
  117. ;;
  118. E)
  119. DELETE_CONFIRM=$OPTARG
  120. ;;
  121.  
  122. esac
  123. done
  124.  
  125. # make sure the required paramaters are set to something
  126. # TODO check if getops handles this natively in some way
  127. if [ "$DL_TYPE" == "" ] || [ "$SL_APIKEY" == "" ] || [ "$SL_PROJECT" == "" ]; then
  128. usage
  129. exit
  130. fi
  131.  
  132. # make sure download type is valid
  133. if [ "$DL_TYPE" != "original" ]
  134. && [ "$DL_TYPE" != "published" ]
  135. && [ "$DL_TYPE" != "p100" ]
  136. && [ "$DL_TYPE" != "pseudo" ]
  137. && [ "$DL_TYPE" != "pending" ]
  138. && [ "$DL_TYPE" != "TMX" ]
  139. && [ "$DL_TYPE" != "DELETE" ]; then
  140. echo "INVALID TYPE. Acceptable types: original | published | p100 | pseudo | pending | TMX. Exiting."
  141. exit
  142. fi
  143.  
  144. # if user has specified mask confirm it - note mask means nothing to TMX
  145. if [ "$URI_MASK" != "" ]; then
  146. if [ $DL_TYPE == "TMX" ]; then
  147. echo "Mask option has no effect when downloading TMX. Ignoring."
  148. URI_MASK=""
  149. else
  150. echo "mask was set!"$'n'
  151. fi
  152. fi
  153.  
  154. IFS=$'n'
  155.  
  156. #make sure there are no API configuration errors by doing a file/list call just to check for error conditions
  157. errors=($(curl -sS -d "apiKey=$SL_APIKEY&projectId=$SL_PROJECT&uriMask=$URI_MASK" "https://$SERVER_URL/v1/file/list" | grep -Eo "ERROR"))
  158. error_count=${#errors[@]}$'n'
  159. if [ $error_count -gt 0 ]; then
  160. echo "error with either the API key or Project ID - verify your values."
  161. exit
  162. fi
  163.  
  164. # figure out how many total files there - because if more than standard 500 returned in list, then need to paginate to build the list of uris
  165. # need to move this check since if downloading TMX it's possible there are no files.
  166. total_files=($(curl -sS -d "apiKey=$SL_APIKEY&projectId=$SL_PROJECT&uriMask=$URI_MASK" "https://$SERVER_URL/v1/file/list" | grep -Eo "fileCount":[0-9]+" | sed -e 's/fileCount"://g'))
  167.  
  168.  
  169. # The /file/list API has a limit of 500 items, if the total needed is more, need to make multiple calls to build the full URI list
  170.  
  171. listLimit=500
  172.  
  173. # figure out how many passes in batches of 500 needed
  174.  
  175. let "passes=$total_files/$listLimit"
  176.  
  177. # if more than 1 batch needed, then make as many passes as needed to build the list
  178.  
  179. pass=0
  180.  
  181. while [ $pass -le $passes ]; do
  182.  
  183. let "passOffset=$pass*$listLimit"
  184.  
  185. uris+=($(curl -sS -d "apiKey=$SL_APIKEY&projectId=$SL_PROJECT&uriMask=$URI_MASK&offset=$passOffset" "https://$SERVER_URL/v1/file/list" | grep -Eo "fileUri":"[^"]*" | sed -e 's/fileUri":"//g'))
  186. ((pass++))
  187.  
  188. done
  189.  
  190. # exit if requesting to download files, but nothing matched at all, otherwise show the count of files that matched and the names
  191. # TODO: refactor since TMX handling is different with getopts
  192.  
  193. file_count=${#uris[@]}
  194. if [ $file_count = "0" ] && [ $DL_TYPE != "TMX" ]; then echo "nothing to operate on, exiting!"; exit; fi
  195.  
  196. # if deleting check all the parameters set and if so pass of URI mask list to delete function
  197. if [ "$DL_TYPE" == "DELETE" ]; then
  198. if [ "$DELETE_CONFIRM" == "true" ] && [ "$URI_MASK" != "" ]; then
  199. delete_files $uris
  200. exit
  201. else
  202. echo "Must set -u to a uri mask value and must set -E true to confirm deletion. Exiting"
  203. exit
  204. fi
  205. fi
  206.  
  207. # if not downloading TMX - then list the files we found
  208. echo "total files to download "$total_files$'n'
  209. if [ $DL_TYPE != "TMX" ]; then echo "URIs: "${uris[@]}$'n'; fi
  210.  
  211.  
  212. #BEGIN ORIGINALS
  213. if [ "$DL_TYPE" == "original" ] ; then
  214. echo "downloading originals"
  215. echo "files to download: "$file_count$'n'
  216.  
  217. # create the originals folder if it doesn't exist
  218. if [ ! -d "originals" ]; then
  219. echo "making originals folder"$'n'
  220. mkdir originals
  221. fi
  222.  
  223. COUNT=0
  224. # go through the list of files to download (in each language)
  225. for CURRENT_FILE in ${uris[@]}
  226. do
  227. echo $CURRENT_FILE
  228. ((COUNT++))
  229. echo $COUNT
  230.  
  231. # since the URI includes full URI including default smartling prefix '/files/' or any other prefix specified - strip that and just use the last part after the last /
  232. # e.g. if the URI is /files/filename.ext then BF_NAME will be filename.ext - this what we use for the local file name (in the folder)
  233. BF_NAME=${CURRENT_FILE##*/}
  234. # This is curl call that downloads the originals. Since no LOCALE is set that is the behavior of /file/get
  235.  
  236. # becuase we are flattening the URIs to basename it's possible filenames could be duplicated - so to avoid this check if the file already exist and if it does use count to append name to create unique
  237. # TODO USE COUNT
  238. if [ -e originals/$BF_NAME ]; then
  239. BF_NAME=$COUNT.$BF_NAME
  240. echo "Updated BF_NAME"
  241. fi
  242.  
  243. curl -sS -d "apiKey=$SL_APIKEY&projectId=$SL_PROJECT&fileUri=$CURRENT_FILE" "https://$SERVER_URL/v1/file/get" > originals/$BF_NAME
  244. echo "downloaded URI: "$CURRENT_FILE" to: originals/"$BF_NAME$'n'
  245.  
  246. done
  247. exit
  248. fi
  249. #end of originals
  250.  
  251.  
  252. # if not getting originals - then getting translations OR TMX - so get locales and then use that to download all
  253. # get the list of locales for this project. Similar to above grep but the locale key value then sed for just the value
  254.  
  255. # if user has not set the locales array in the call, then get the full list via API
  256. # TODO - validate the locales are good - otherwise user will just get empty files
  257. if [ "$LOCALES" = "" ]; then
  258. LOCALES=($(curl -sS -d "apiKey=$SL_APIKEY&projectId=$SL_PROJECT" "https://$SERVER_URL/v1/project/locale/list" | grep -Eo "locale":"[^"]*" | sed -e 's/locale":"//g'))
  259. fi
  260.  
  261. echo "locales: "${#LOCALES[@]}$'n'
  262. # count of the locales
  263. echo ${LOCALES[@]}$'n'
  264.  
  265. #script puts the translated' versions in 'translated folder' with sub-folders for locales
  266. if [ ! -d "translated" ]; then
  267. echo "making translated folder"$'n'
  268. mkdir translated
  269. fi
  270.  
  271. if [ $USE_LOCALE_DIR == "true" ]; then
  272. #if [ "$USE_LOCALE_DIR" == "true" ]; then
  273. # make the subfolders in the translated folder if they don't exist
  274. for CURRENT_LOCALE in ${LOCALES[@]}
  275. do
  276. if [ ! -d translated/$CURRENT_LOCALE ]; then
  277. echo "making "$CURRENT_LOCALE" folder" $'n'
  278. mkdir translated/$CURRENT_LOCALE
  279. fi
  280. done
  281. fi
  282.  
  283. #TODO need to refactor TMX / MASK / PUBLISHED / FULL
  284. # Download TMX
  285. # Downloading TMX uses a different API than downloading tranlated files
  286. if [ "$DL_TYPE" == "TMX" ] ; then
  287. echo "downloading TMX "$TMX_FLAG
  288. for CURRENT_LOCALE in ${LOCALES[@]}
  289. do
  290. # if using folders - set the folder locale
  291. # else using file-names so set the filename locale
  292. if [ "$USE_LOCALE_DIR" == "true" ]; then
  293. LOCALE_DIR=$CURRENT_LOCALE"/"
  294. FILE_LOCALE="TMX-"
  295. else
  296. FILE_LOCALE=$CURRENT_LOCALE"-"
  297. LOCALE_DIR=""
  298. fi
  299.  
  300. curl -sS -d "apiKey=$SL_APIKEY&projectId=$SL_PROJECT&locale=$CURRENT_LOCALE&format=TMX&dataSet=$TMX_FLAG" "https://$SERVER_URL/v1/translations/download" > translated/$LOCALE_DIR$FILE_LOCALE"TMX.xml"
  301. echo "downloaded " $CURRENT_LOCALE "TMX" $'n'
  302. done
  303.  
  304. exit
  305. fi
  306.  
  307.  
  308. # downloading translations of files
  309. # by this point the file list has already been filtered and the and the locale list has been created
  310. # go through the list of files to download (in each language)
  311.  
  312. COUNT=0
  313. P100=0
  314.  
  315. # since I use DL_TYPE as actual parameter to API call need to set this back to actual "published" before entering loop to download
  316. if [ $DL_TYPE == "p100" ]; then
  317. DL_TYPE="published"
  318. P100=1
  319. fi
  320.  
  321.  
  322. for CURRENT_FILE in ${uris[@]}
  323. do
  324. echo $CURRENT_FILE
  325. ((COUNT++))
  326. echo $COUNT
  327. # loop through all the locales for each file
  328.  
  329. for CURRENT_LOCALE in ${LOCALES[@]}
  330. do
  331.  
  332. # if using folders - set the folder locale
  333. # else using file-names so set the filename locale
  334. if [ $USE_LOCALE_DIR == "true" ]; then
  335. LOCALE_DIR=$CURRENT_LOCALE"/"
  336. FILE_LOCALE=""
  337. else
  338. FILE_LOCALE=$CURRENT_LOCALE"-"
  339. LOCALE_DIR=""
  340. fi
  341.  
  342. # since the URI includes full URI including default smartling prefix '/files/' or any other prefix specified - strip that and just use the last part after the last /
  343. # e.g. if the URI is /files/filename.ext then BF_NAME will be filename.ext - this what we use for the local file name (in the locale folder)
  344. BF_NAME=${CURRENT_FILE##*/}
  345.  
  346. EXT=${BF_NAME##*.}
  347. # rename .pot files to .po - we really should be checking the header from the /file/get API call but this is just simpler
  348. if [ "$EXT" == "pot" ]; then
  349. filename=${BF_NAME%.*}
  350. BF_NAME=$filename".po"
  351. echo "renamed pot to po"
  352. fi
  353.  
  354. # because we are flattening the URIs to basename it's possible filenames could be duplicated - so to avoid this check if the file already exist and if it does use COUNT to append name to create unique
  355. if [ -e translated/$LOCALE_DIR$FILE_LOCALE$BF_NAME ]; then
  356. BF_NAME=$COUNT.$BF_NAME
  357. echo "Updated BF_NAME"
  358. fi
  359.  
  360. # Here if user is doing 100% published need to test and if it's not then skip it
  361. # probably should do this earlier in this loops to avoid other tests but thare not needed if we are just going to skip the file
  362.  
  363. if [ $P100 == 1 ]; then
  364.  
  365. # echo "requested 100 percent only"
  366.  
  367. # use api status call and pull out the part of the response that has the string count and completed string count
  368. completeStatus=($(curl -sS -d "apiKey=$SL_APIKEY&projectId=$SL_PROJECT&fileUri=$CURRENT_FILE&locale=$CURRENT_LOCALE" "https://$SERVER_URL/v1/file/status" | grep -Eo "stringCount":[0-9]+.*"completedStringCount":[0-9]+" ))
  369.  
  370. # strip those out as separate variables to test
  371. stringCount=($(echo $completeStatus | sed -E 's/stringCount":([0-9]+).*/1/g'))
  372. completedStringCount=($(echo $completeStatus | sed -E 's/.*completedStringCount":([0-9]+).*/1/g'))
  373. #echo $stringCount","$completedStringCount
  374.  
  375. # set download to 1 only if 100%
  376. if [ $stringCount == $completedStringCount ]; then
  377. download=1
  378. echo $CURRENT_FILE" in "$CURRENT_LOCALE" is 100% complete!"$'n'
  379.  
  380. else
  381. download=0
  382. echo "100% complete requested. Skipping URI: "$CURRENT_FILE" in "$CURRENT_LOCALE" total:"$stringCount", completed:"$completedStringCount$'n'
  383. fi
  384.  
  385. else
  386. # didn't ask for 100% so always download
  387. download=1
  388.  
  389. fi
  390.  
  391.  
  392. if [ $download == 1 ]; then
  393. # This is the actual curl command that downloads the given file for given locale, and state as specified when called
  394. curl -sS -d "apiKey=$SL_APIKEY&projectId=$SL_PROJECT&fileUri=$CURRENT_FILE&locale=$CURRENT_LOCALE&retrievalType=$DL_TYPE" "https://$SERVER_URL/v1/file/get" > translated/$LOCALE_DIR$FILE_LOCALE$BF_NAME
  395. echo "downloaded URI: "$CURRENT_FILE" to: translated/"$LOCALE_DIR$FILE_LOCALE$BF_NAME$'n'
  396. fi
  397.  
  398.  
  399. done
  400.  
  401. done
  402.  
  403. exit
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement