Skip to content

Commit

Permalink
first version working
Browse files Browse the repository at this point in the history
  • Loading branch information
Christian Holl committed Jan 5, 2014
1 parent 466128e commit bc39be7
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 18 deletions.
10 changes: 6 additions & 4 deletions src/PyFlashAir/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@
@author: cyborg-x1
'''
from PyFlashAir import flashair
import time

if __name__ == '__main__':

print(len(flashair.command.Disable_Photo_Share_mode))
a=flashair.connection('192.168.0.16', 80,1000)
file='/DCIM/101EOS5D/RL0_0001.JPG'
print(a.download_file(file,'/home/cyborg-x1'))
#print(a.send_command(flashair.command.Get_file_list,directory='/'))
while True:
print('sync')
a.sync_folder_to_remote_folder('/DCIM/101EOS5D', '/home/cyborg-x1/dwhelper')
time.sleep(1)
pass
51 changes: 37 additions & 14 deletions src/PyFlashAir/flashair.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import os
import http.client
# import urllib2
import datetime

class file_list_entry(object):
file_name=''
Expand Down Expand Up @@ -127,8 +127,9 @@ def send_command(self, opcode, directory='', date=-1, addr=-1, data=(), length=-
if(len(opcode[6])!=0):

if(len(self.fwversion)==0):

(ret, ver)=self.send_command(command.Get_the_firmware_version)
if(ret):
if(not ret):
self.fwversion=ver.decode("utf-8")[9:]
print("Firmware is: ")
print(self.fwversion)
Expand All @@ -149,15 +150,19 @@ def send_command(self, opcode, directory='', date=-1, addr=-1, data=(), length=-
#Connect
connection = http.client.HTTPConnection(self.host, self.port, timeout=self.timeout)

connection.request("GET", url)
response=connection.getresponse();
return (response.status!=200,response.read())
try:
connection.request("GET", url)
response=connection.getresponse();
return (response.status!=200,response.read())
except:
return (-2,'')
pass


def get_file_list(self,directory):
(ret,lst)=self.send_command(command.Get_file_list,directory)

(ret,lst)=self.send_command(command.Get_file_list,directory=directory)

if(ret>0):
if(ret==0):

lines=lst.decode("utf-8").split("\r\n")
if(lines[0]=="WLANSD_FILELIST"):
Expand All @@ -174,8 +179,8 @@ def get_file_list(self,directory):
if(len(e)!=6):
print("Error file list entry has " +str(len(e)) +" entrie(s) instead of expected 6, skipping entry")
continue;

f=file_list_entry(e[1],e[0],e[2],e[3],e[4],e[5])
#(file_name, directory_name, size, attributes, date, time):
f=file_list_entry(e[1],e[0],int(e[2]),int(e[3]),int(e[4]),int(e[5]))
outlst.append(f)

return (0,outlst)
Expand All @@ -190,7 +195,7 @@ def download_file(self, remote_location, local_path='', local_file_name=''):

#does folder exist?
if(not os.access(local_path, os.R_OK)):
return 2
return (2,0,'')

#add / if it is not there already
if(len(local_path)!=0 and local_path[-1]!='/'):
Expand All @@ -201,7 +206,7 @@ def download_file(self, remote_location, local_path='', local_file_name=''):

#does file exist already?
if(os.path.isfile(local_path)):
return (3,0)
return (3,0,'')

#get the stuff from the FlashAir
conn.request("GET", remote_location)
Expand All @@ -216,14 +221,32 @@ def download_file(self, remote_location, local_path='', local_file_name=''):
file_size += len(buffer)
file.write(buffer)
file.close()
return (int(download.status!=200), file_size)
return (int(download.status!=200), file_size,local_path)

def download_file_list_entry(self, entry,local_path='', local_filename=''):
(status,size)=self.download_file(entry.directory_name + '/' + entry.file_name, local_path, local_filename)
(status,size,local_filename)=self.download_file(entry.directory_name + '/' + entry.file_name, local_path, local_filename)
if(status):
return(1)


if(size!=entry.byte_size):
os.remove(local_filename)
return(2)

return(0)

def sync_folder_to_remote_folder(self,remote_path='',local_path='',extensions=['JPG']):
#all extensions to upper case
extensions=[x.upper() for x in extensions]

#get list of remote files
(status, outlist)=self.get_file_list(remote_path)
if(not status and len(outlist)):
if(not os.access(local_path, os.R_OK)):
return 2
for entry in outlist:
if ((entry.file_name.split('.')[-1].upper() in extensions) or len(extensions)==0):
self.download_file_list_entry(entry, local_path)



0 comments on commit bc39be7

Please sign in to comment.