usr bin python3 coding utf-8 import calendar import requests from date

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import calendar
import requests
from datetime import datetime
from bs4 import BeautifulSoup
SITE_URL = 'http://pikabu.ru/'
PIKABU_SESS = requests.Session()
class PikaService(object):
def request(self, url):
if url is not None:
return PIKABU_SESS.get(SITE_URL + url)
else:
return False
class PikabuComments(PikaService):
def get(self, post_id):
if post_id is not None:
page = self.request('generate_xml_comm.php?id=%i' % post_id)
comment_list = []
xml = BeautifulSoup(page.text)
comments = xml.comments.findAll('comment')
for comment in comments:
comment_id = int(comment['id'])
comment_rating = int(comment['rating'])
comment_author = comment['nick']
comment_answer = int(comment['answer'])
comment_date = calendar.timegm(
datetime.strptime(comment['date'], '%Y-%m-%d %H:%M').utctimetuple())
comment_text = comment.text
comment_list.append(ObjectComments(comment_id,
comment_rating, comment_author,
comment_answer, comment_date, comment_text))
return comment_list
else:
return False
class ObjectComments():
def __init__(self, id, rating, nick, answer, date, text):
self.id = id
self.rating = rating
self.author = nick
self.answer = answer
self.date = date
self.text = text
class API:
def __init__(self):
self.comments = PikabuComments()