Source

gett-cli / gett.py

Diff from to

File gett.py

 # You should have received a copy of the GNU General Public License
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
+import datetime
+import json
 import logging
-import json
 import pprint
-import datetime
 import threading
-import os.path
 
+from urllib.error import HTTPError
+from urllib.parse import urlencode
+from urllib.parse import urlparse
 from urllib.request import urlopen, Request
-from urllib.parse import urlencode
-from urllib.error import HTTPError
-from urllib.parse import urlparse
 
 from http.client import HTTPConnection
 
 API_BASE = 'https://open.ge.tt/1/'
 API_KEY = 't05kormjprb2o6rm8f8wmts2thjjor'
 
+
 class APIError(Exception):
     pass
 
+
 def _post_request(path, **kwargs):
     qskeys = {}
     url = API_BASE + path
 
     return _request(request)
 
+
 def _get_request(path, **kwargs):
     url = API_BASE + path
 
     request = Request(url)
     return _request(request)
 
+
 def _request(req):
     logger.debug("%s request to %s", req.get_method(), req.full_url)
-    if req.data: logger.debug("data: %s", req.data.decode('utf-8'))
+    if req.data:
+        logger.debug("data: %s", req.data.decode('utf-8'))
 
     try:
         resp = urlopen(req)
         raw = resp.read()
     except HTTPError as ex:
         resp = ex
-        logger.debug("got %d error", resp.code) # urllib sucks
+        logger.debug("got %d error", resp.code)  # urllib sucks
         raw = ex.read()
 
     raw = raw.decode('utf-8')
 
     return result
 
+
 class User(object):
     def _load(self, result):
         self.atoken = result['accesstoken']
 
     def login_auth(self, email, password):
         logger.debug("Loging-in user %r", email)
-        result = _post_request('users/login', apikey=API_KEY, email=email, password=password)
+        result = _post_request('users/login',
+            apikey=API_KEY, email=email, password=password
+        )
         self._load(result)
 
     def login_token(self, rtoken):
     def list_shares(self, skip=None, limit=None):
         logger.debug("Listing shares")
         if skip is not None and limit is not None:
-            results = _get_request('shares', accesstoken=self.atoken, skip=str(skip), limit=str(limit))
+            results = _get_request('shares', accesstoken=self.atoken,
+                skip=str(skip), limit=str(limit)
+            )
         else:
             results = _get_request('shares', accesstoken=self.atoken)
 
         for share_result in results:
-             share = UserShare(self)
-             share._load(share_result)
-             yield share
+            share = UserShare(self)
+            share._load(share_result)
+            yield share
 
     def get_share(self, name):
         logger.debug("Getting share %r", name)
     def create_share(self, title=None):
         logger.debug("Creating share with title %r", title)
         if title is not None:
-            result = _post_request('shares/create', _accesstoken=self.atoken, title=title)
+            result = _post_request('shares/create',
+                _accesstoken=self.atoken, title=title
+            )
         else:
             result = _post_request('shares/create', _accesstoken=self.atoken)
 
 
         return share
 
+
 class Share(object):
     def __init__(self, name):
         self.name = name
         result = _get_request('shares/%s' % self.name)
         self._load(result)
 
+
 class UserShare(Share):
     def __init__(self, user):
         self.user = user
 
     def update(self, **fields):
         logger.debug("Updating user share %r", self.name)
-        result = _post_request('shares/%s/update' % self.name, _accesstoken=self.user.atoken, **fields)
+        result = _post_request('shares/%s/update' % self.name,
+            _accesstoken=self.user.atoken, **fields
+        )
         self._load(result)
 
     def destroy(self):
         logger.debug("Destroying user share %r", self.name)
-        _post_request('shares/%s/destroy' % self.name, _accesstoken=self.user.atoken)
+        _post_request('shares/%s/destroy' % self.name,
+            _accesstoken=self.user.atoken
+        )
 
     def create_file(self, filename, size=None):
         logger.debug("Creating file %r in user share %r", filename, self.name)
-        result = _post_request('files/%s/create' % self.name, _accesstoken=self.user.atoken, filename=filename)
+        result = _post_request('files/%s/create' % self.name,
+            _accesstoken=self.user.atoken, filename=filename
+        )
         file = File(self)
         file._load(result)
         file.size = size
         self.files[file.id] = file
         return file
 
+
 class File(object):
     def __init__(self, share):
         self.share = share
 
     def destroy(self):
         logger.debug("Destoying file %s/%s", self.share.name, self.id)
-        _post_request('files/%s/%s/destroy' % (self.share.name, self.id), _accesstoken=self.share.user.atoken)
+        _post_request('files/%s/%s/destroy' % (self.share.name, self.id),
+            _accesstoken=self.share.user.atoken
+        )
         del self.share.files[self.id]
 
     def refresh(self):
         result = _get_request('files/%s/%s' % (self.share.name,  self.id))
         self._load(result)
 
+
 class FileUpload(threading.Thread):
     def __init__(self, file, fp):
         super().__init__()
                 self.bytes_written += len(data)
                 self.percent_done = self.bytes_written * 100 / self.file_size
 
-            self.percent_done = 100 # needed when file_size is zero...
+            self.percent_done = 100  # needed when file_size is zero...
 
-            resp = conn.getresponse()
+            conn.getresponse()
+
         except Exception as ex:
             self.ex = ex