-
Notifications
You must be signed in to change notification settings - Fork 0
/
postprinter.py
151 lines (136 loc) · 4.47 KB
/
postprinter.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#!/usr/bin/env python2
# coding:utf-8
from textwrap import dedent
from colors import prettify
from colors import bold, underlined
from postimage import PostImage
import imgterm
import os
"""
pretty prints posts.
"""
def textpost(post):
return dedent(
"""\
==={}===
{}
""").format(
prettify(post['title'].encode('utf-8') if post['title'] else '',
'green', None, 'bold', 'underlined'),
bold(post['body'].encode('utf-8'))
)
def photopost(post):
photostr = dedent(
"""\
{}
\t{}
""").format(
bold(post['caption'].encode('utf-8')),
'\n\t'.join(
('* {}\n\t{}\n\t\t({} : {}x{})'.format(
bold(photo['caption'].encode('utf-8')),
'\n\t'.join(
imgterm.show_img(
PostImage.id_and_num_to_file(post['id'],
photo['alt_sizes'][0]['url'],
i),
method='mean' # better at letters.
)
),
prettify(photo['alt_sizes'][0]['url'],'dark gray',None),
prettify(photo['alt_sizes'][0]['width'],'dark gray',None),
prettify(photo['alt_sizes'][0]['height'],'dark gray',None)
)
for (i,photo) in enumerate(post['photos']))
)
)
return photostr
def quotepost(post):
quotestr = dedent(
"""\
{}
\t---{}
""").format(
post['text'].encode('utf-8'),
prettify(post['source'].encode('utf-8'),
'light gray',None),
)
return quotestr
def linkpost(post):
linkstr = "{} ({})\n".format(
post['title'].encode('utf-8'),
prettify(post['url'].encode('utf-8'),
'blue',None))
linkstr += '\n'.join(
('\t'+line.encode('utf-8'))
for line in post['description'].split('\n'))
return linkstr + '\n'
def chatpost(post):
dialog = post['dialogue']
title = post['title']
if not title: title=''
diastr = '\n\n'.join(
('{}\n\t{}'.format(
log['label'].encode('utf-8'),
'\n\t'.join(log['phrase'].encode('utf-8').split('\n')))
for log in dialog)
)
return bold(title.encode('utf-8')) + diastr
def audiopost(post):
return(post['caption'].encode("utf-8"))
def videopost(post):
return(post['caption'].encode("utf-8"))
def answerpost(post):
return dedent('''\
{} Asked {} ({}):
\t {}
--{}'''.format(
post['asking_name'].encode('utf-8'),
post['blog_name'], post['asking_url'],
post['question'].encode('utf-8'),
bold(post['answer'].encode('utf-8'))
))
class PostPrinter(object):
def __init__(self):
pass
printer = {
'text' : textpost,
'photo' : photopost,
'quote' : quotepost,
'link' : linkpost,
'chat' : chatpost,
'audio' : audiopost,
'video' : videopost,
'answer' : answerpost,
}
@classmethod
def show(cls, post):
rows, columns = map(int,os.popen('stty size', 'r').read().split())
printstr = dedent(
"""\
{}
{}
{} at {} from {}, id is {}
{} : [{}]
""").format(
prettify(' '*columns,'dark gray',None,'underlined'),
prettify(' '*columns,None,None),
prettify(post['type'], 'white', 'blue'),
bold(post['date']),
prettify(post['blog_name'],None, None,'bold'),
post['id'],
prettify('tagged','green',None,'bold'),
','.join(
list(map(lambda t: underlined(t.encode('utf-8')),
post['tags']))
)
)
if 'source_url' in post:
printstr += "source : {} ({})\n".format(
post['source_title'].encode('utf-8'),
post['source_url'].encode('utf-8'))
printstr += cls.printer[post['type']](post)
printstr += prettify('({})'.format(post['post_url']).rjust(columns),
'dark gray', None)
printstr += prettify(' '*columns+'\n', 'dark gray',None,'underlined')
return printstr