-
Notifications
You must be signed in to change notification settings - Fork 4
/
thread_post.py
84 lines (73 loc) · 3.22 KB
/
thread_post.py
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#!/usr/bin/env python
# -*- coding: utf-8 -*-
__version__ = '0.01'
__author__ = 'Andrey Derevyagin'
__copyright__ = 'Copyright © 2013-2014'
import time
import pytz
from datetime import datetime
import calendar
class Post(object):
"""docstring for Post"""
def __init__(self, post={}, timezone_string=None):
super(Post, self).__init__()
self.title = post.get('title', '')
self.poster = post.get('poster', '')
self.time = post.get('time', 0) # unixtime
self.post = post.get('post', None)
self.thumb = post.get('thumb', None)
self.img = post.get('image', None)
self.id = post.get('pid', None)
self.deleted = post.get('deleted', False)
self.lxml_data = None
self.timezone_string = timezone_string if timezone_string else 'Europe/Kiev'
if post.get('image_attr'):
tmp = post.get('image_attr')
self.img_width = tmp[0]
self.img_height = tmp[1]
self.img_size = tmp[2]
else:
self.img_width = 0
self.img_height = 0
self.img_size = ''
if post.get('thumb_attr'):
tmp = post.get('thumb_attr')
self.thumb_width = tmp[0]
self.thumb_height = tmp[1]
else:
self.thumb_width = 0
self.thumb_height = 0
def __str__(self):
return '%s %s %s'%(time.strftime("%a, %d %b %Y %H:%M:%S +0000", time.localtime(self.time)),
self.title.encode('utf-8') if self.title else '', self.img)
def dump(self):
return '''
id: %d
date: %s
title: %s
poster: %s
image(%s): %s
thumb(%dx%d): %s
post(%d): %s'''%(self.id, self.date_string(), self.title, self.poster, self.img_info_str(), self.img,
self.thumb_width, self.thumb_height, self.thumb, len(self.post) if self.post else 0, self.post if self.post else '')
def date_string(self, timezone_string=None):
if timezone_string is None:
timezone_string = self.timezone_string
tm = datetime.fromtimestamp(self.time, pytz.timezone(timezone_string))
#tm = datetime.fromtimestamp(self.time, pytz.timezone('Europe/Kiev'))
#tm = datetime.fromtimestamp(self.time, pytz.utc)
#tm = time.gmtime(self.time).astimezone(pytz.timezone('Europe/Moscow'))
m = [u'января', u'февраля', u'марта', u'апреля', u'мая', u'июня', u'июля', u'августа', u'сентября', u'октября', u'ноября', u'декабря']
w = [u'Пн',u'Вт',u'Ср',u'Чт',u'Пт',u'Сб',u'Вс']
return '%s %02d %s %d %02d:%02d:%02d'%(w[tm.weekday()], tm.day, m[tm.month-1], tm.year, tm.hour, tm.minute, tm.second)
#tm = time.localtime(self.time)
#return '%s %02d %s %d %02d:%02d:%02d'%(w[tm.tm_wday], tm.tm_mday, m[tm.tm_mon-1], tm.tm_year, tm.tm_hour, tm.tm_min, tm.tm_sec)
#return time.strftime("%a, %d %b %Y %H:%M:%S", time.localtime(self.time))
def img_info_str(self):
if self.img_width and self.img_height:
return '%s, %dx%d'%(self.img_size, self.img_width, self.img_height)
return None
def img_name(self):
if self.img:
return self.img.split('/')[-1]
return None