Private GIT
Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
S
Seedbox-Statistics-For-InfluxDB
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
vlbox
Seedbox-Statistics-For-InfluxDB
Commits
9a5e22a1
Commit
9a5e22a1
authored
8 years ago
by
Matt
Browse files
Options
Downloads
Patches
Plain Diff
Moved HTTP requests into 1 method to cleanup exception handling
parent
8c98cf7f
No related branches found
No related tags found
No related merge requests found
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
clients/deluge.py
+15
-25
15 additions, 25 deletions
clients/deluge.py
clients/torrentclient.py
+33
-1
33 additions, 1 deletion
clients/torrentclient.py
clients/utorrent.py
+28
-22
28 additions, 22 deletions
clients/utorrent.py
with
76 additions
and
48 deletions
clients/deluge.py
+
15
−
25
View file @
9a5e22a1
...
...
@@ -21,7 +21,7 @@ class DelugeClient(TorrentClient):
Add common headers needed to make the API requests
:return: request
"""
# TODO pass this to parent
self
.
send_log
(
'
Adding headers to request
'
,
'
debug
'
)
headers
=
{
...
...
@@ -47,13 +47,10 @@ class DelugeClient(TorrentClient):
req
=
self
.
_create_request
(
method
=
'
auth.check_session
'
,
params
=
[
''
])
try
:
res
=
urlopen
(
req
)
except
URLError
as
e
:
msg
=
'
Failed To check session state. HTTP Error
'
self
.
send_log
(
msg
,
'
error
'
)
print
(
e
)
return
None
res
=
self
.
_make_request
(
req
,
fail_msg
=
'
Failed To check session state. HTTP Error
'
)
if
not
res
:
return
result
=
self
.
_process_response
(
res
)
...
...
@@ -108,17 +105,10 @@ class DelugeClient(TorrentClient):
:return: None
"""
msg
=
'
Attempting to authenticate against {} API
'
.
format
(
self
.
torrent_client
)
self
.
send_log
(
msg
,
'
info
'
)
req
=
self
.
_create_request
(
method
=
'
auth.login
'
,
params
=
[
self
.
password
])
try
:
res
=
urlopen
(
req
)
except
URLError
as
e
:
msg
=
'
Failed To Authenticate with torrent client. HTTP Error. Aborting
'
self
.
send_log
(
msg
,
'
critical
'
)
print
(
e
)
sys
.
exit
(
1
)
res
=
self
.
_make_request
(
req
,
genmsg
=
msg
,
fail_msg
=
'
Failed to contact API for authentication
'
,
abort_on_fail
=
True
)
output
=
self
.
_process_response
(
res
)
...
...
@@ -138,7 +128,6 @@ class DelugeClient(TorrentClient):
msg
=
'
Successfully Authenticated With {} API
'
.
format
(
self
.
torrent_client
)
self
.
send_log
(
msg
,
'
info
'
)
def
_build_torrent_list
(
self
,
torrents
):
"""
Take the resulting torrent list and create a consistent structure shared through all clients
...
...
@@ -160,25 +149,26 @@ class DelugeClient(TorrentClient):
self
.
torrent_list
[
hash
][
'
tracker
'
]
=
data
[
'
tracker_host
'
]
self
.
torrent_list
[
hash
][
'
total_files
'
]
=
data
[
'
num_files
'
]
def
get_all_torrents
(
self
):
"""
Return a list of all torrents from the API
:return:
"""
self
.
send_log
(
'
Getting list of torrents
'
,
'
debug
'
)
req
=
self
.
_create_request
(
method
=
'
core.get_torrents_status
'
,
params
=
[
''
,
''
])
try
:
self
.
_check_session
()
# Make sure we still have an active session
res
=
urlopen
(
req
)
except
URLError
as
e
:
msg
=
'
Failed to get list of torrents. HTTP Error
'
self
.
send_log
(
msg
,
'
error
'
)
print
(
e
)
res
=
self
.
_make_request
(
req
,
fail_msg
=
'
Failed to get list of torrents from API
'
)
if
not
res
:
self
.
torrent_list
=
{}
return
output
=
self
.
_process_response
(
res
)
if
not
output
:
return
...
...
This diff is collapsed.
Click to expand it.
clients/torrentclient.py
+
33
−
1
View file @
9a5e22a1
...
...
@@ -47,7 +47,7 @@ class TorrentClient:
if
not
headers
:
return
req
self
.
send_log
(
'
Adding headers to request
'
,
'
info
'
)
self
.
send_log
(
'
Adding headers to request
'
,
'
debug
'
)
for
k
,
v
in
headers
.
items
():
req
.
add_header
(
k
,
v
)
...
...
@@ -62,6 +62,38 @@ class TorrentClient:
"""
raise
NotImplementedError
def
_make_request
(
self
,
req
,
genmsg
=
''
,
fail_msg
=
''
,
abort_on_fail
=
None
):
"""
Make the web request. Doing it here avoids a lot of duplicate exception handling
:param gen_msg: Message we can print to console or logs so we know about the request
:param fail_msg: Message we can print to console or logs on failure
:param abort_on_fail: Exit on failed request
:return: Response
"""
if
genmsg
:
self
.
send_log
(
genmsg
,
'
info
'
)
try
:
res
=
urlopen
(
req
)
except
URLError
as
e
:
if
fail_msg
:
msg
=
fail_msg
else
:
msg
=
'
Failed to make request
'
if
abort_on_fail
:
self
.
send_log
(
msg
,
'
critical
'
)
self
.
send_log
(
'
Aborting
'
,
'
critical
'
)
sys
.
exit
(
1
)
else
:
self
.
send_log
(
msg
,
'
error
'
)
return
None
return
res
def
_process_response
(
self
,
res
):
# TODO May only be needed for deluge. Remove from parent if that's the case
raise
NotImplementedError
...
...
This diff is collapsed.
Click to expand it.
clients/utorrent.py
+
28
−
22
View file @
9a5e22a1
...
...
@@ -96,14 +96,13 @@ class UTorrentClient(TorrentClient):
req
=
self
.
_create_request
(
params
=
'
action=getprops&hash={}
'
.
format
(
hash
))
try
:
res
=
urlopen
(
req
).
read
().
decode
(
'
utf-8
'
)
except
URLError
as
e
:
msg
=
'
Failed to get trackers from URL for hash {}
'
.
format
(
hash
)
self
.
send_log
(
msg
,
'
error
'
)
fail_msg
=
msg
=
'
Failed to get trackers from URL for hash {}
'
.
format
(
hash
)
res
=
self
.
_make_request
(
req
,
fail_msg
=
fail_msg
)
if
not
res
:
return
'
N/A
'
res_json
=
json
.
loads
(
res
)
res_json
=
self
.
_process_response
(
res
)
tracker
=
res_json
[
'
props
'
][
0
][
'
trackers
'
].
split
()[
0
]
...
...
@@ -116,6 +115,13 @@ class UTorrentClient(TorrentClient):
return
tracker_url
def
_process_response
(
self
,
res
):
raw_output
=
res
.
read
().
decode
(
'
utf-8
'
)
json_output
=
json
.
loads
(
raw_output
)
return
json_output
def
_get_file_count
(
self
,
hash
):
"""
Method for uTorrent to get total file since it requires another API call
...
...
@@ -128,16 +134,17 @@ class UTorrentClient(TorrentClient):
req
=
self
.
_create_request
(
params
=
'
action=getfiles&hash={}
'
.
format
(
hash
))
try
:
res
=
urlopen
(
req
).
read
().
decode
(
'
utf-8
'
)
except
URLError
as
e
:
msg
=
'
Failed to get file list from URL for hash {}
'
.
format
(
hash
)
self
.
send_log
(
msg
,
'
error
'
)
res
=
self
.
_make_request
(
req
,
fail_msg
=
'
Failed to get file list for hash
'
.
format
(
hash
))
if
not
res
:
return
'
N/A
'
res_json
=
json
.
loads
(
res
)
output
=
self
.
_process_response
(
res
)
return
len
(
res_json
[
'
files
'
][
1
])
if
'
files
'
in
output
:
return
len
(
output
[
'
files
'
][
1
])
else
:
return
'
N/A
'
def
get_all_torrents
(
self
):
"""
...
...
@@ -150,16 +157,15 @@ class UTorrentClient(TorrentClient):
req
=
self
.
_create_request
(
params
=
'
list=1
'
)
try
:
res
=
urlopen
(
req
)
final
=
res
.
read
().
decode
(
'
utf-8
'
)
except
URLError
as
e
:
msg
=
'
Failed to get list of all torrents
'
print
(
e
)
self
.
send_log
(
msg
,
'
error
'
)
res
=
self
.
_make_request
(
req
,
fail_msg
=
'
Failed to get list of all torrents
'
)
if
not
res
:
self
.
torrent_list
=
{}
return
final_json
=
json
.
loads
(
final
)
output
=
self
.
_process_response
(
res
)
self
.
_build_torrent_list
(
final_json
[
'
torrents
'
])
\ No newline at end of file
if
'
torrents
'
in
output
:
self
.
_build_torrent_list
(
output
[
'
torrents
'
])
else
:
self
.
torrent_list
=
{}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment