nend 36 Posted September 20, 2014 (edited) Hoi There all, This is my first UDF, It has not been completed and there are much improvements possible. This UDF only works for users of Synology NAS server Functions are: - _Synology_Filestation_Login - _Synology_Filestation_Logout - _Synology_Filestation_Get_Shares - _Synology_FileStation_Get_Dir - _Synology_Filestation_File_List This UDF is make use of the webapi. Webapi helpfile from synology (PDF) http://ukdl.synology.com/download/Document/DeveloperGuide/Synology_File_Station_API_Guide.pdf If there are users how wants to finished this UDF (or make improvements) Please be my quest. expandcollapse popupGlobal $o_Synology_HTTP ; #FUNCTION# ==================================================================================================================== ; Name...........: _Synology_Filestation_Login ; Description ...: Logs into filestation API. ; Syntax.........: _Synology_Filestation_Login($http_url, $username, $password, $raw) ; Parameters ....: $http_url - http URL for the Synology webinterface ; $username - Username for the user account ; $password - Password for the user account ; $raw - If true return string with raw API data ; Return values .: Success - Login taskid ; Failure - , Errorcode (read the API pdf for the error codes) ; Author ........: Dave Thijse ; Modified.......: ; Remarks .......: ; Related .......: ; Link ..........: ; Example .......: Yes ; =============================================================================================================================== Func _Synology_Filestation_Login($http_url, $username, $password, $raw = False) local $string_Array, $return_string $o_Synology_HTTP = ObjCreate("winhttp.winhttprequest.5.1") $return_string = _Post_Send($http_url & "/webapi/auth.cgi", "api=SYNO.API.Auth&version=3&method=login&account=" & $username & "&passwd=" & $password & "&session=FileStation&format=cookie") If $raw Then Return $return_string EndIf If StringInStr($return_string, '"error"') Then $string_Array = StringRegExp($return_string, '"code":(.*?)}', 3) Return SetError($string_Array[0]) Else $string_Array = StringRegExp($return_string, '"sid":"(.*?)"},"success"', 1, 1) If StringInStr($return_string, ":true") Then Return $string_Array[0] Else Return False EndIf EndIf EndFunc ; #FUNCTION# ==================================================================================================================== ; Name...........: _Synology_Filestation_Logout ; Description ...: Logs out filestation API. ; Syntax.........: _Synology_Filestation_Logout($http_url, $raw) ; Parameters ....: $http_url - http URL for the Synology webinterface ; $raw - If true return string with raw API data ; Return values .: Success - return true ; Failure - , Errorcode (read the API pdf for the error codes) ; Author ........: Dave Thijse ; Modified.......: ; Remarks .......: ; Related .......: ; Link ..........: ; Example .......: Yes ; =============================================================================================================================== Func _Synology_Filestation_Logout($http_url, $raw = False) Local $return_string $return_string = _Post_send($http_url & "/webapi/auth.cgi", "api=SYNO.API.Auth&version=1&method=logout&session=Filestation") If $raw Then Return $return_string Else If StringInStr($return_string, '"error"') Then $string_Array = StringRegExp($return_string, '"code":(.*?)}', 3) Return SetError($string_Array[0]) Else Return True EndIf EndIf EndFunc ; #FUNCTION# ==================================================================================================================== ; Name...........: _Synology_Filestation_Get_Shares ; Description ...: Gets all shares names ; Syntax.........:_Synology_Filestation_Get_Shares($http_url, $flag, $raw) ; Parameters ....: $http_url - http URL for the Synology webinterface ; $flag - 1 get names, 2 get fullpath ; $raw - If true return string with raw API data ; Return values .: Success - array with shares ; Failure - , Errorcode (read the API pdf for the error codes) ; Author ........: Dave Thijse ; Modified.......: ; Remarks .......: ; Related .......: ; Link ..........: ; Example .......: Yes ; =============================================================================================================================== Func _Synology_Filestation_Get_Shares($http_url, $flag = 1, $raw = False) Local $return_string, $string_Array $return_string = _Post_send($http_url & "/webapi/FileStation/file_share.cgi", "api=SYNO.FileStation.List&version=1&method=list_share&additional= real_path") If StringInStr($return_string, '"error"') Then If $raw Then Return $return_string Else $string_Array = StringRegExp($return_string, '"code":(.*?)}', 3) Return SetError($string_Array[0]) EndIf EndIf If $raw Then Return $return_string Else Switch $flag Case 1 $string_Array = StringRegExp($return_string, '"name":"(.*?)"', 3) Case 2 $string_Array = StringRegExp($return_string, '"real_path":"(.*?)"', 3) EndSwitch Return $string_Array EndIf EndFunc ; #FUNCTION# ==================================================================================================================== ; Name...........: _Synology_FileStation_Get_Dir ; Description ...: Get directorie infomation ; Syntax.........:_Synology_FileStation_Get_Dir($http_url, $path, $raw) ; Parameters ....: $http_url - http URL for the Synology webinterface ; $path - share name ; $raw - If true return string with raw API data ; Return values .: Success - array 0 amount directories 1 amount files 2 total size ; Failure - , Errorcode (read the API pdf for the error codes) ; Author ........: Dave Thijse ; Modified.......: ; Remarks .......: ; Related .......: ; Link ..........: ; Example .......: Yes ; =============================================================================================================================== Func _Synology_FileStation_Get_Dir($http_url, $path, $raw = False) Local $return_string, $return_Array[3], $string_Array, $taskid, $return_raw $return_string = _Post_Send($http_url & "/webapi/FileStation/file_dirSize.cgi", "api=SYNO.FileStation.DirSize&version=1&method=start&path=" & _URIEncode($path)) If $raw Then $return_raw = $return_string EndIf If StringInStr($return_string, '"error"') Then If $raw = False Then $string_Array = StringRegExp($return_string, '"code":(.*?)}', 3) Return SetError($string_Array[0]) EndIf Else $string_Array = StringRegExp($return_string, '{"taskid":"(.*?)"},"success"', 1, 1) $taskid = $string_Array[0] While 1 $return_string = _Post_Send($http_url & "/webapi/FileStation/file_dirSize.cgi", "api=SYNO.FileStation.DirSize&version=1&method=status&taskid=" & $taskid) If StringInStr($return_string, '"error"') Then If $raw Then Return $return_raw = $return_raw & " " & $return_string EndIf $string_Array = StringRegExp($return_string, '"code":(.*?),"', 3) Return SetError($string_Array[0]) Else $string_Array = StringRegExp($return_string, '"finished":(.*?),"', 3) If $string_Array[0] = "true" Then If $raw = True Then $return_raw = $return_raw & $return_string Return $return_raw EndIf $string_Array = StringRegExp($return_string, '"num_dir":(.*?),"', 3) $return_Array[0] = Round($string_Array[0]) $string_Array = StringRegExp($return_string, '"num_file":(.*?),"', 3) $return_Array[1] = Round($string_Array[0]) $string_Array = StringRegExp($return_string, '"total_size":(.*?)},"', 3) $return_Array[2] = $string_Array[0] Return $return_Array EndIf EndIf Sleep(20) WEnd EndIf EndFunc ; #FUNCTION# ==================================================================================================================== ; Name...........: _Synology_Filestation_File_List ; Description ...: List files ; Syntax.........: _Synology_Filestation_File_List($http_url, $path, $pattern, $flag) ; Parameters ....: $http_url - http URL for the Synology webinterface ; $path - share name ; $pattern - Any glob syntax(? and *) if not set it's return all files ; $flag - 1 get names, 2 get fullpath ; Return values .: Success - array ; Failure - , Errorcode (read the API pdf for the error codes) ; Author ........: Dave Thijse ; Modified.......: ; Remarks .......: ; Related .......: ; Link ..........: ; Example .......: Yes ; =============================================================================================================================== Func _Synology_Filestation_File_List($http_url, $path, $pattern = 1, $flag = 1) Local $return_string, $string_Array, $taskid, $return_Array[1][1] $return_string = _Post_Send($http_url & "/webapi/FileStation/file_find.cgi", "api=SYNO.FileStation.Search&version=1&method=start&folder_path=" & _URIEncode($path) & "&pattern=" & $pattern) If StringInStr($return_string, '"error"') Then $string_Array = StringRegExp($return_string, '"code":(.*?),"', 3) Return SetError($string_Array[0]) Else $string_Array = StringRegExp($return_string, '{"taskid":"(.*?)"},"success"', 1, 1) $taskid = $string_Array[0] While 1 $return_string = _Post_Send($http_url & "/webapi/FileStation/file_find.cgi", "api=SYNO.FileStation.Search&version=1&method=list&taskid=" & $taskid & "&additional=real_path&limit=-1") If StringInStr($return_string, '"error"') Then $string_Array = StringRegExp($return_string, '"code":(.*?),"', 3) Return SetError($string_Array[0]) Else $string_Array = StringRegExp($return_string, '"finished":(.*?),"', 3) If $string_Array[0] = "true" Then Switch $flag Case 1 $path_array = StringRegExp($return_string, '"name":"(.*?)",', 3) Case 2 $path_array = StringRegExp($return_string, '"real_path":"(.*?)"', 3) EndSwitch Return $path_array EndIf EndIf Sleep(20) WEnd EndIf EndFunc ;------------------------------------------------------------------------------------------- Func _Post_Send($link, $sendstring) $o_Synology_HTTP.Open("POST", $link) $o_Synology_HTTP.SetRequestHeader("Content-Type", "application/x-www-form-urlencoded") $o_Synology_HTTP.Send($sendstring) Return $o_Synology_HTTP.ResponseText EndFunc Func _URIEncode($sData); Made by ProgAndy Local $aData = StringSplit(BinaryToString(StringToBinary($sData,4),1),"") Local $nChar $sData="" For $i = 1 To $aData[0] $nChar = Asc($aData[$i]) Switch $nChar Case 45, 46, 48 To 57, 65 To 90, 95, 97 To 122, 126 $sData &= $aData[$i] Case 32 $sData &= "+" Case Else $sData &= "%" & Hex($nChar,2) EndSwitch Next Return $sData EndFunc How to use 3x example expandcollapse popup#include <Array.au3> #include <Synology.au3> $username = "admin" $password = "*******" $http_url = "http://192.168.0.10:5000" _Synology_Filestation_Login($http_url, $username, $password) If @error Then ConsoleWrite("Error code = " & @error & @CRLF); see pdf voor error codes Else $path = "/homes"; begin with a share name _test1($path); get dir info _test2(); arraydisplay with all shares _test3($path); arraydisplay of all files _Synology_Filestation_Logout($http_url); logout If @error Then ConsoleWrite("Error code = " & @error & @CRLF) Else Exit EndIf EndIf Func _test1($path) local $dir_array $dir_array = _Synology_FileStation_Get_Dir($http_url, $path) If @error Then ConsoleWrite("Error code = " & @error & @CRLF); see pdf voor error codes Else ConsoleWrite("Directories = " & $dir_array[0] & @CRLF & "Files = " & $dir_array[1] & @CRLF & "Total size = " & ConvertSize($dir_array[2]) & @CRLF) EndIf EndFunc Func _test2() local $shares_array $shares_array = _Synology_Filestation_Get_Shares($http_url, 1); array with all shares If @error Then ConsoleWrite("Error code = " & @error & @CRLF); see pdf voor error codes Else _ArrayDisplay($shares_array) EndIf EndFunc Func _test3($path) Local $list_array $list_array = _Synology_Filestation_File_List($http_url, $path, "*.jpg", 2); filter = *.jpg If @error Then ConsoleWrite("Error code = " & @error & @CRLF); see pdf voor error codes Else _ArrayDisplay($list_array) EndIf EndFunc Func ConvertSize($inputSize, $outputPlaces = 2) Local $unitNames[5] = ["","K","M","G","T"] Local $bytes, $outputUnit $bytes = $inputSize * 1024 ^ 0 $outputUnit = Int(Log($bytes)/Log(1024)) If $outputUnit > 4 Then $outputUnit = 4 Return String(Round($bytes / 1024 ^ $outputUnit, $outputPlaces)) & " " & $unitNames[$outputUnit] & "B" EndFunc Edited September 20, 2014 by nend 2 Share this post Link to post Share on other sites
Enforcer 4 Posted February 21, 2015 (edited) Hmmm, tried to make this thing work with my synology. Corrected login parameters, Started script waited for 4 minutes nothing happened... Weird... We need also some functions to work with download station... for example - add new task (url download) I'l try to do this by myself ... Edited February 21, 2015 by Enforcer [RU] Zone Share this post Link to post Share on other sites
TheSaint 1,619 Posted February 21, 2015 Thanks for sharing. AutoIt.4.Life Clubrooms - Life is like a Donut (secret key) Make sure brain is in gear before opening mouth! Remember, what is not said, can be just as important as what is said. Spoiler What is the Secret Key? Life is like a Donut If I put effort into communication, I expect you to read properly & fully, or just not comment. Ignoring those who try to divert conversation with irrelevancies. If I'm intent on insulting you or being rude, I will be obvious, not ambiguous about it. I'm only big and bad, to those who have an over-active imagination. I may have the Artistic Liesense to disagree with you. TheSaint's Toolbox (be advised many downloads are not working due to ISP screwup with my storage) Share this post Link to post Share on other sites
nend 36 Posted February 22, 2015 @Enforcer, I've just tested it again and It works perfect. Just make sure you set the path, username, password and the URL correct. And make sure the port number is corrected on the Synology. If the directory are big it can take a while before you see the infomartion. Share this post Link to post Share on other sites