Skip to content

Commit 0e784b7

Browse files
author
DrillCoder
committed
v1.1 Первая версия пакетика
1 parent 0347860 commit 0e784b7

File tree

13 files changed

+199
-0
lines changed

13 files changed

+199
-0
lines changed

Diff for: __init__.py

Whitespace-only changes.

Diff for: python_pik_api_wrapper/__init__.py

Whitespace-only changes.

Diff for: python_pik_api_wrapper/entity/__init__.py

Whitespace-only changes.

Diff for: python_pik_api_wrapper/entity/block.py

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from python_pik_api_wrapper.entity.entity import Entity
2+
3+
4+
class Block(Entity):
5+
def __init__(self, id: str, name: str, sort: int):
6+
super().__init__(id)
7+
if type(name) is not str:
8+
raise ValueError
9+
self.name = name
10+
if type(sort) is not int:
11+
raise ValueError
12+
self.sort = sort

Diff for: python_pik_api_wrapper/entity/bulk.py

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from python_pik_api_wrapper.entity.entity import Entity
2+
3+
4+
class Bulk(Entity):
5+
def __init__(self, id: str, name: str, sort: int, type_id: int):
6+
super().__init__(id)
7+
if type(name) is not str:
8+
raise ValueError
9+
self.name = name
10+
if type(sort) is not int:
11+
raise ValueError
12+
self.sort = sort
13+
if type(type_id) is not int:
14+
raise ValueError
15+
self.type_id = type_id

Diff for: python_pik_api_wrapper/entity/entity.py

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Entity:
2+
def __init__(self, id: str):
3+
if type(id) is not str:
4+
raise ValueError
5+
self.id = id
6+
7+
def __repr__(self):
8+
str_list = []
9+
for item in self.__dict__:
10+
if type(getattr(self, item)) is list:
11+
elements = []
12+
for element in getattr(self, item):
13+
elements.append(f'{element.__repr__()}')
14+
str_list.append(f'{item}:\n' + "\n".join(elements))
15+
continue
16+
str_list.append(f'{item}: {getattr(self, item)}')
17+
return ', '.join(str_list)

Diff for: python_pik_api_wrapper/entity/item.py

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
from typing import List
2+
3+
from python_pik_api_wrapper.entity.entity import Entity
4+
from python_pik_api_wrapper.value_object.photo import Photo
5+
6+
7+
class Item(Entity):
8+
def __init__(self, id: str, public_date: str, date: str, photos: List[Photo]):
9+
super().__init__(id)
10+
if type(public_date) is not str:
11+
raise ValueError
12+
self.public_date = public_date
13+
if type(date) is not str:
14+
raise ValueError
15+
self.date = date
16+
if type(photos) is not list:
17+
raise ValueError
18+
self.photos = photos

Diff for: python_pik_api_wrapper/entity/location.py

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from python_pik_api_wrapper.entity.entity import Entity
2+
3+
4+
class Location(Entity):
5+
def __init__(self, id: str, name: str, sort: int):
6+
super().__init__(id)
7+
if type(name) is not str:
8+
raise ValueError
9+
self.name = name
10+
if type(sort) is not int:
11+
raise ValueError
12+
self.sort = sort

Diff for: python_pik_api_wrapper/pik_wrapper.py

+95
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
from typing import List
2+
3+
import requests
4+
5+
from python_pik_api_wrapper.entity.block import Block
6+
from python_pik_api_wrapper.entity.bulk import Bulk
7+
from python_pik_api_wrapper.entity.item import Item
8+
from python_pik_api_wrapper.entity.location import Location
9+
from python_pik_api_wrapper.value_object.photo import Photo
10+
11+
12+
class PikWrapper:
13+
def __init__(self):
14+
self.base_url = 'https://api.pik.ru/v1/'
15+
self.headers = {
16+
'accept': '*/*',
17+
'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_1) AppleWebKit/605.1.15 (KHTML, like Gecko) '
18+
'Version/13.0.3 Safari/605.1.15',
19+
'wrapper': 'https://github.com/drillcoder/python_pik_api_wrapper'
20+
}
21+
22+
def send_request(self, url:str) -> List:
23+
if type(url) is not str:
24+
raise ValueError
25+
response = requests.get(self.base_url + url, headers=self.headers)
26+
if response.status_code != requests.codes.ok:
27+
raise ValueError
28+
if response.status_code == requests.codes.unprocessable:
29+
return []
30+
content = response.json()
31+
if type(content) is dict:
32+
if content.get('message') == 'ERR_NEWS_NOT_FOUND':
33+
raise []
34+
if type(content.get('items')) is not None:
35+
content = content.get('items')
36+
if type(content) is not list:
37+
raise ValueError
38+
return content
39+
40+
def get_locations(self) -> List[Location]:
41+
locations = self.send_request('location')
42+
locations_list = []
43+
count = 0
44+
for location in locations:
45+
if location['id'] == 2:
46+
locations_list.append(Location('2', 'Москва', count))
47+
count += 1
48+
locations_list.append(Location('3', 'Московская область', count))
49+
continue
50+
locations_list.append(Location(str(location['id']), location['name'], count))
51+
count += 1
52+
return locations_list
53+
54+
def get_blocks(self, location: Location) -> List[Block]:
55+
if type(location) is not Location:
56+
raise ValueError
57+
blocks = self.send_request(f"block?metadata=1&types=1,2&locations={location.id}")
58+
blocks_list = []
59+
for block in blocks:
60+
blocks_list.append(Block(str(block['id']), block['name'], block['sort']))
61+
return blocks_list
62+
63+
def get_bulks(self, block: Block) -> List[Bulk]:
64+
if type(block) is not Block:
65+
raise ValueError
66+
bulks = self.send_request(f"bulk?block_id={block.id}")
67+
bulks_list = []
68+
count = 0
69+
for bulk in bulks:
70+
if bulk['type_id'] not in {1, 2, 103} or len(bulk['name']) == 0:
71+
continue
72+
bulks_list.append(Bulk(str(bulk['id']), bulk['name'], count, bulk['type_id']))
73+
count += 1
74+
return bulks_list
75+
76+
def get_items(self, bulk: Bulk) -> List[Item]:
77+
if type(bulk) is not Bulk:
78+
raise ValueError
79+
items = self.send_request(f"news?limit=all&is_content=1&is_progress=1&bulk_id={bulk.id}")
80+
items_list = []
81+
for item in items:
82+
url = 'https:' + item['preview']
83+
images_path = [item['preview']]
84+
photos = [Photo(url, 0)]
85+
sort = 1
86+
if type(item.get('gallery')) is list:
87+
for photo in item['gallery']:
88+
if photo['file_path'] in images_path:
89+
continue
90+
images_path.append(photo['file_path'])
91+
url = 'https:' + photo['file_path']
92+
photos.append(Photo(url, sort))
93+
sort += 1
94+
items_list.append(Item(item['id'], item['public_date'], item['date'], photos))
95+
return items_list

Diff for: python_pik_api_wrapper/value_object/__init__.py

Whitespace-only changes.

Diff for: python_pik_api_wrapper/value_object/photo.py

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from python_pik_api_wrapper.value_object.value_object import ValueObject
2+
3+
4+
class Photo(ValueObject):
5+
def __init__(self, url: str, sort: int):
6+
if type(url) is not str:
7+
raise ValueError
8+
self.url = url
9+
if type(sort) is not int:
10+
raise ValueError
11+
self.sort = sort

Diff for: python_pik_api_wrapper/value_object/value_object.py

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class ValueObject:
2+
def __repr__(self):
3+
return ', '.join(f'{item}: {getattr(self, item)}' for item in self.__dict__)

Diff for: setup.py

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from setuptools import setup, find_packages
2+
3+
setup(name='python_pik_api_wrapper',
4+
version='1.1',
5+
description='Wrapper for the PIK Group website API',
6+
classifiers=[
7+
'Programming Language :: Python :: 3.6',
8+
],
9+
keywords='pik wrapper python',
10+
url='https://github.com/drillcoder/python_pik_api_wrapper',
11+
author='DrillCoder',
12+
author_email='[email protected]',
13+
packages=find_packages(),
14+
install_requires=[
15+
'requests',
16+
])

0 commit comments

Comments
 (0)