Advertisement
Guest User

Untitled

a guest
Mar 21st, 2013
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.03 KB | None | 0 0
  1. '''
  2.    This program is free software: you can redistribute it and/or modify
  3.    it under the terms of the GNU General Public License as published by
  4.    the Free Software Foundation, either version 3 of the License, or
  5.    (at your option) any later version.
  6.  
  7.    This program is distributed in the hope that it will be useful,
  8.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  9.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  10.    GNU General Public License for more details.
  11.  
  12.    You should have received a copy of the GNU General Public License
  13.    along with this program.  If not, see <http://www.gnu.org/licenses/>.
  14. '''
  15. if __name__ == "__main__":
  16.  
  17.     import sys, os, pickle, threading, pprint, shutil, re
  18.  
  19.     addon_path = dirname(__file__)
  20.     libs = os.path.join(addon_path, 'lib')
  21.  
  22.     sys.path.append(libs)
  23.     from dropbox import client, rest, session
  24.  
  25.     pp = pprint.PrettyPrinter(indent=4)
  26.  
  27.     def log(msg):
  28.         pp.pprint(msg)
  29.  
  30.     # Get your app key and secret from the Dropbox developer website
  31.     APP_KEY = 'key'
  32.     APP_SECRET = 'secret' #changed these for obvious reasons
  33.  
  34.     # ACCESS_TYPE should be 'dropbox' or 'app_folder' as configured for your app
  35.     ACCESS_TYPE = 'app_folder'
  36.  
  37.     sess = session.DropboxSession(APP_KEY, APP_SECRET, ACCESS_TYPE)
  38.  
  39.     access_token_location = os.path.join(addon_path, 'access_token.txt')
  40.     out = open(access_token_location, 'rb')
  41.     access_token = pickle.load(out)
  42.     log("SKDownloader: access_token: %s" % access_token)
  43.     out.close()
  44.  
  45.     sess.set_token(access_token=access_token['key'], access_token_secret=access_token['secret'])
  46.     client = client.DropboxClient(sess)
  47.  
  48.  
  49.     #__settings__   = xbmcaddon.Addon(script.service.skplayer)
  50.     #c_ipaddr = __settings__.getSetting("oath")
  51.  
  52.     path = '/storage/download'
  53.  
  54.     #path = ADDON.getSetting('lcl_path')
  55.     #log("SKDownloader: directory %s" % ADDON.getSetting('lcl_path'))
  56.     #pp = pprint.PrettyPrinter(indent=4)
  57.  
  58.     access_token_location = os.path.join(addon_path, 'access_token.txt')
  59.  
  60.     cursor_location = os.path.join(addon_path, 'cursor.txt')
  61.     out = open(cursor_location, 'rb')
  62.     try:
  63.         cursor = pickle.load(out)
  64.     except EOFError:
  65.         cursor = None
  66.     out.close()
  67.  
  68.     db_path = '/devices/device1'
  69.     regex = re.compile("^" + db_path)
  70.  
  71.     def checkdb():
  72.         global cursor
  73.         log("SKDownloader: polling for delta")
  74.         delta = client.delta(cursor=cursor)
  75.         log("SKDownloader: received delta: %s" % delta)
  76.         for entry in delta['entries']:
  77.             if regex.match(entry[0]) is None:
  78.                 continue
  79.             log("SKDownloader, reading: %s" % entry[0])
  80.             cpath = os.path.join(path, entry[0][1:])
  81.             if entry[1] is None:
  82.                 if not os.path.exists(cpath):
  83.                     continue
  84.                 if os.path.isdir(cpath):
  85.                     shutil.rmtree(cpath)
  86.                     log("SKDownloader: removing path: %s" % cpath)
  87.                 else:
  88.                     os.remove(cpath)
  89.                     log("SKDownloader: removing file: %s" % cpath)
  90.             else:
  91.                 if os.path.exists(cpath):
  92.                     continue
  93.                 if entry[1]['is_dir']:
  94.                     os.makedirs(cpath)
  95.                     log("SKDownloader: created directory: %s" % cpath)
  96.                 else:
  97.                     f, metadata = client.get_file_and_metadata(entry[0])
  98.                     out = open(cpath, 'wb')
  99.                     log("SKDownloader: writing to: %s" % cpath)
  100.                     out.write(f.read())
  101.                     out.close()
  102.             cursor = delta['cursor']
  103.             out = open(cursor_location, 'wb')
  104.             pickle.dump(cursor, out, -1)
  105.             out.close()
  106.         if len(delta['entries']):
  107.             pass
  108.             #xbmc.executebuiltin("XBMC.RecursiveSlideShow(" + path + ")")
  109.         threading.Timer(20, checkdb).start()
  110.  
  111.     # start calling f now and every 60 sec thereafter
  112.     checkdb()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement