Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
John Donnal
/
comms_demo
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
8205d69a
authored
Nov 02, 2020
by
source_reader
Browse files
Options
_('Browse Files')
Download
Email Patches
Plain Diff
removed keys
parent
92d4bc41
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
60 additions
and
0 deletions
marvel_client.py
marvel_client.py
0 → 100644
View file @
8205d69a
import
hashlib
import
time
import
requests
import
io
import
pygame
class
MarvelClient
:
def
__init__
(
self
,
public_key
,
private_key
):
self
.
public_key
=
public_key
self
.
private_key
=
private_key
self
.
api_url
=
"http://gateway.marvel.com:80/v1/public"
def
get_characters
(
self
):
params
=
self
.
__generate_auth_params
()
resp
=
requests
.
get
(
self
.
api_url
+
"/characters"
,
params
=
params
)
json
=
resp
.
json
()
characters
=
[]
for
item
in
json
[
'data'
][
'results'
]:
# turn thumbnail into image url
thumb
=
item
[
'thumbnail'
]
image_url
=
thumb
[
'path'
]
+
"/portrait_xlarge."
+
thumb
[
'extension'
]
characters
.
append
(
Character
(
item
[
'name'
],
item
[
'description'
],
image_url
,
item
[
'comics'
],
item
[
'series'
]))
return
characters
def
__generate_auth_params
(
self
):
now
=
str
(
time
.
time
())
msg
=
(
now
+
self
.
private_key
+
self
.
public_key
)
.
encode
(
'ascii'
)
hash
=
hashlib
.
md5
(
msg
)
.
hexdigest
()
return
{
"ts"
:
now
,
"hash"
:
hash
,
"apikey"
:
self
.
public_key
}
class
Character
:
def
__init__
(
self
,
name
,
description
,
image_url
,
comics
,
series
):
self
.
name
=
name
self
.
description
=
description
,
self
.
image_url
=
image_url
self
.
comics
=
comics
,
self
.
series
=
series
def
get_image
(
self
):
resp
=
requests
.
get
(
self
.
image_url
)
img_bytes
=
io
.
BytesIO
(
resp
.
content
)
img
=
pygame
.
image
.
load
(
img_bytes
)
return
img
# test library code
if
__name__
==
"__main__"
:
my_client
=
MarvelClient
(
"7e583905d15a48c849ef155bc9d6af29"
,
"f8307f988ea51a41040df0c273cd5ab36606d58d"
)
my_client
.
get_characters
()
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment