Просмотр исходного кода

marvel.py: no requests, no f-strings

That way all students can use it
master
Dimitri Merejkowsky 6 лет назад
Родитель
Сommit
52ae9807bf
1 измененных файлов: 18 добавлений и 7 удалений
  1. +18
    -7
      sources/marvel/marvel.py

+ 18
- 7
sources/marvel/marvel.py Просмотреть файл

@@ -1,8 +1,10 @@
import abc import abc
import hashlib import hashlib
import requests
import json
import shutil import shutil
import time import time
import urllib.parse
import urllib.request
import textwrap import textwrap
import sys import sys


@@ -41,11 +43,13 @@ class Client:
params = self.auth.generate_params() params = self.auth.generate_params()
params.update(query.params()) params.update(query.params())
url = Client.base_url + query.path() url = Client.base_url + query.path()
response = requests.get(url, params=params)
status_code = response.status_code
if status_code != 200:
sys.exit("got status: " + str(status_code))
body = response.json()
url_query = urllib.parse.urlencode(params)
full_url = url + "?" + url_query
with urllib.request.urlopen(full_url) as response:
status_code = response.getcode()
if status_code != 200:
sys.exit("got status: " + str(status_code))
body = json.loads(response.read())
attribution = body["attributionText"] attribution = body["attributionText"]
response = query.extract(body) response = query.extract(body)
return (response, attribution) return (response, attribution)
@@ -132,7 +136,12 @@ class CreatorNumberOfSeries(Query):
return first_result["series"]["available"] return first_result["series"]["available"]


def text(self, response): def text(self, response):
return f"{self.first_name} {self.last_name} worked on {response} series"
return "{} {} worked on {} series".format(
self.first_name,
self.last_name,
response
)





def main(): def main():
@@ -147,6 +156,8 @@ def main():
first_name = sys.argv[2] first_name = sys.argv[2]
last_name = sys.argv[3] last_name = sys.argv[3]
query = CreatorNumberOfSeries(first_name, last_name) query = CreatorNumberOfSeries(first_name, last_name)
else:
sys.exit("Unkwnon query type: {}".format(query_type))


client = Client(auth) client = Client(auth)
response,attribution = client.make_request(query) response,attribution = client.make_request(query)