Compare commits

..

2 Commits

Author SHA1 Message Date
shimun
f5ba8377d8 Merge branch 'sessions' of shimun/screencloud-seafile into master 2019-07-30 10:05:34 +00:00
867969b660 Use requests.Session() 2018-10-02 13:43:48 +00:00
3 changed files with 63 additions and 112 deletions

View File

@ -27,7 +27,6 @@ class SeafileUploader():
self.parentWidget = parentWidget
self.settingsDialog = self.uil.load(QFile(workingDir + "/settings.ui"), parentWidget)
self.settingsDialog.group_account.widget_loggedIn.loginButton.connect("clicked()", self.startAuthenticationProcess)
self.settingsDialog.group_processor.widget_processor.configureButton.connect("clicked()", self.processor.configure(self.settingsDialog))
#self.settingsDialog.group_name.input_name.connect("textChanged(QString)", self.nameFormatEdited)
self.settingsDialog.group_location.widget_location.pathEdit.connect("textChanged(QString)", self.locationUpdate)
self.settingsDialog.group_location.widget_location.libraryEditDrop.connect("currentIndexChanged(QString)", self.locationUpdate)

View File

@ -4,6 +4,7 @@ import requests
class SeafileClient:
def __init__(self,server,username,password=None,token=None):
self.session = requests.Session()
self.server = server
self.token = token
self.login = (username,password)
@ -13,7 +14,7 @@ class SeafileClient:
def ping(self):
try:
return requests.get("%s/ping" % self.api_endpoint()).text == "pong"
return self.session.get("%s/ping" % self.api_endpoint()).text == "pong"
except:
return False
@ -21,7 +22,7 @@ class SeafileClient:
def obtain_token(self):
user,passw = self.login
try:
req=requests.post("%s/auth-token/" % self.api_endpoint(), data = {'username': user, 'password': passw })
req=self.session.post("%s/auth-token/" % self.api_endpoint(), data = {'username': user, 'password': passw })
json = req.json()
if "non_field_errors" in json:
print json["non_field_errors"]
@ -32,11 +33,12 @@ class SeafileClient:
def authorize(self):
self.token = self.obtain_token()
self.session.headers.update({'Authorization': "Token %s" % self.token.token})
return self.token != False
#curl -H 'Authorization: Token 24fd3c026886e3121b2ca630805ed425c272cb96' -H 'Accept: application/json; indent=4' https://cloud.seafile.com/api2/repos/
def libraries(self):
resp=requests.get("%s/repos/" % self.api_endpoint(), headers = {'Authorization': "Token %s" % self.token.token, 'Accept': 'application/json; indent=4' })
resp=self.session.get("%s/repos/" % self.api_endpoint(), headers = {'Authorization': "Token %s" % self.token.token, 'Accept': 'application/json; indent=4' })
if not resp.status_code == 200: return
libraries=[]
for lib in resp.json():
@ -48,6 +50,7 @@ class SeafileLibrary:
def __init__(self,client,id,name,owner):
self.client = client
self.session = self.client.session
self.id = id
self.name = name
self.owner = owner
@ -59,11 +62,11 @@ class SeafileLibrary:
def obtain_link():
#curl -H "Authorization: Token f2210dacd9c6ccb8133606d94ff8e61d99b477fd" https://cloud.seafile.com/api2/repos/99b758e6-91ab-4265-b705-925367374cf0/upload-link/
print "%s/upload-link" % self.api_endpoint()
quoted = requests.get("%s/upload-link" % self.api_endpoint(), headers = {'Authorization': "Token %s" % self.client.token.token,}).text
quoted = self.session.get("%s/upload-link" % self.api_endpoint(), headers = {'Authorization': "Token %s" % self.client.token.token,}).text
return quoted[1:-1]
#curl -H "Authorization: Token f2210dacd9c6ccb8133606d94ff8e61d99b477fd" -F file=@test.txt -F filename=test.txt -F parent_dir=/ http://cloud.seafile.com:8082/upload-api/73c5d117-3bcf-48a0-aa2a-3f48d5274ae3
resp = requests.post(link or obtain_link() ,
resp = self.session.post(link or obtain_link() ,
files={'file': (file_name,open(file, 'rb')), 'parent_dir': directory, 'target_file': "%s/%s" % (directory,file_name)},
headers = {'Authorization': "Token %s" % self.client.token.token,}
)
@ -73,7 +76,7 @@ class SeafileLibrary:
#curl -H 'Authorization: Token f2210dacd3606d94ff8e61d99b477fd' -H 'Accept: application/json; charset=utf-8; indent=4' https://cloud.seafile.com/api2/repos/dae8cecc-2359-4d33-aa42-01b7846c4b32/file/detail/?p=/foo.c
def file_info(self,path):
resp=requests.get("%s/file/detail/?p=%s" % (self.api_endpoint(),path), headers = {'Authorization': "Token %s" % self.token.token, 'Accept': 'application/json; indent=4' })
resp=self.session.get("%s/file/detail/?p=%s" % (self.api_endpoint(),path), headers = {'Authorization': "Token %s" % self.token.token, 'Accept': 'application/json; indent=4' })
if resp.status_code == 200:
json = resp.json()
return SeafileFile(self,path,json['id'],json['size'])
@ -88,11 +91,12 @@ class SeafileFile:
self.id = id
self.path = path
self.library = library
self.session = self.library.session
self.size = size
#curl -v -X PUT -d "p=/foo.md" -H 'Authorization: Token f2210dacd9c6ccb8133606d94ff8e61d99b477fd' -H 'Accept: application/json; indent=4' https://cloud.seafile.com/api2/repos/afc3b694-7d4c-4b8a-86a4-89c9f3261b12/file/shared-link/
def share(self):
resp = requests.put("%s/repos/%s/file/shared-link/" % (self.library.client.api_endpoint(),self.library.id),
resp = self.session.put("%s/repos/%s/file/shared-link/" % (self.library.client.api_endpoint(),self.library.id),
headers = {'Authorization': "Token %s" % self.library.client.token.token, 'Accept': 'application/json; indent=4'},
data = {'p': self.path}
)
@ -101,7 +105,7 @@ class SeafileFile:
def update(self,file):
def obtain_link():
#curl -H "Authorization: Token f2210dacd9c6ccb8133606d94ff8e61d99b477fd" https://cloud.seafile.com/api2/repos/99b758e6-91ab-4265-b705-925367374cf0/upload-link/
quoted = requests.get("%s/update-link" % self.library.api_endpoint(), headers = {'Authorization': "Token %s" % self.library.client.token.token,}).text
quoted = self.session.get("%s/update-link" % self.library.api_endpoint(), headers = {'Authorization': "Token %s" % self.library.client.token.token,}).text
return quoted[1:-1]
directory, name = os.path.split(self.path)
return self.library.upload(file,name,directory,obtain_link())

View File

@ -6,16 +6,10 @@
<rect>
<x>0</x>
<y>0</y>
<width>500</width>
<height>589</height>
<width>437</width>
<height>475</height>
</rect>
</property>
<property name="maximumSize">
<size>
<width>500</width>
<height>600</height>
</size>
</property>
<property name="windowTitle">
<string>Seafile - Settings</string>
</property>
@ -84,40 +78,6 @@
</layout>
</widget>
</item>
<item>
<widget class="QGroupBox" name="group_processor">
<property name="enabled">
<bool>true</bool>
</property>
<property name="title">
<string>Processor</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout_4">
<item>
<widget class="QWidget" name="widget_processor" native="true">
<layout class="QFormLayout" name="formLayout">
<item row="0" column="0">
<widget class="QComboBox" name="processorsDrop"/>
</item>
<item row="0" column="1">
<widget class="QPushButton" name="configureButton">
<property name="maximumSize">
<size>
<width>100</width>
<height>16777215</height>
</size>
</property>
<property name="text">
<string>Configure</string>
</property>
</widget>
</item>
</layout>
</widget>
</item>
</layout>
</widget>
</item>
<item>
<widget class="QGroupBox" name="group_location">
<property name="enabled">
@ -129,12 +89,6 @@
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="maximumSize">
<size>
<width>16777215</width>
<height>300</height>
</size>
</property>
<property name="baseSize">
<size>
<width>0</width>
@ -150,62 +104,56 @@
<property name="title">
<string>Location</string>
</property>
<layout class="QHBoxLayout" name="horizontalLayout_2">
<item>
<widget class="QWidget" name="widget_location" native="true">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="maximumSize">
<size>
<width>16777215</width>
<height>300</height>
</size>
</property>
<property name="layoutDirection">
<enum>Qt::LeftToRight</enum>
</property>
<layout class="QVBoxLayout" name="verticalLayout_3">
<property name="sizeConstraint">
<enum>QLayout::SetDefaultConstraint</enum>
<widget class="QWidget" name="widget_location" native="true">
<property name="geometry">
<rect>
<x>20</x>
<y>20</y>
<width>391</width>
<height>101</height>
</rect>
</property>
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="layoutDirection">
<enum>Qt::LeftToRight</enum>
</property>
<layout class="QVBoxLayout" name="verticalLayout_3" stretch="1,1,1,1">
<property name="sizeConstraint">
<enum>QLayout::SetDefaultConstraint</enum>
</property>
<item>
<widget class="QLabel" name="label_5">
<property name="text">
<string>Library</string>
</property>
<item>
<widget class="QLabel" name="label_5">
<property name="text">
<string>Library</string>
</property>
</widget>
</item>
<item>
<widget class="QComboBox" name="libraryEditDrop"/>
</item>
<item>
<widget class="QLabel" name="label_4">
<property name="maximumSize">
<size>
<width>279</width>
<height>16777215</height>
</size>
</property>
<property name="text">
<string>Screenshots Folder</string>
</property>
</widget>
</item>
<item>
<widget class="QLineEdit" name="pathEdit"/>
</item>
</layout>
<zorder>libraryEditDrop</zorder>
<zorder>label_5</zorder>
<zorder>label_4</zorder>
<zorder>pathEdit</zorder>
</widget>
</item>
</layout>
</widget>
</item>
<item>
<widget class="QComboBox" name="libraryEditDrop"/>
</item>
<item>
<widget class="QLabel" name="label_4">
<property name="maximumSize">
<size>
<width>279</width>
<height>16777215</height>
</size>
</property>
<property name="text">
<string>Screenshots Folder</string>
</property>
</widget>
</item>
<item>
<widget class="QLineEdit" name="pathEdit"/>
</item>
</layout>
</widget>
</widget>
</item>
<item>