-
Notifications
You must be signed in to change notification settings - Fork 1
/
tools.py
104 lines (87 loc) · 2.93 KB
/
tools.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
#!/usr/bin/env python
#-*- coding: utf-8 -*-
#
# Twittext - tools.py
# - Hirotaka Kawata <[email protected]>
# - http://www.techno-st.net/wiki/Twittext
#
# Copyright (C) 2009-2010 Hirotaka Kawata <[email protected]>
#
# This file is part of "Twittext".
#
# Twittext is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Twittext is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Twittext. If not, see <http://www.gnu.org/licenses/>.
#
import re
import htmlentitydefs
import curses
class cursesTools:
def split_text(s, w):
i = 0
ss = unicode()
sss = list()
for c in unicode(s):
i += 1 if 0x00 <= ord(c) <= 0x7f else 2
ss += c
if i >= w - 1:
if len(sss) == 0: sss = [ss]
else: sss.append(ss)
ss = unicode()
i = 0
if ss: sss.append(ss)
return sss
def replace_htmlentity(string):
amp = string.find('&')
if amp == -1:
return string
entity = re.compile("&([A-Za-z]+);")
entity_match = entity.findall(string)
for name in entity_match:
c = htmlentitydefs.name2codepoint[name]
string = string.replace("&%s;" % name, unichr(c))
return string
def delete_notprintable(string):
s = unicode()
for c in unicode(string):
if (not(0x00 <= ord(c) <= 0x7f)
or 0x20 <= ord(c) <= 0x7e):
s += c
return s
def attr_select(post, me):
user = post["user"]["screen_name"]
reply_to = post["in_reply_to_screen_name"] if (
"in_reply_to_screen_name" in post.keys()) else None
myname = me["screen_name"]
# Color Change
if user == myname:
# my status
return curses.color_pair(3)
elif reply_to == myname:
# reply to me
return (curses.color_pair(1) | curses.A_BOLD)
else:
at = post["text"].find("@%s" % myname)
if at == 0:
# reply to me, but no in_reply_to
return curses.color_pair(1)
elif at != -1:
# maybe Retweet
return curses.color_pair(2)
return 0
def dputs(*args):
fp = open("debug", 'a')
l = list()
for i in args:
l.append(unicode(i))
fp.write("%s\n" % " ".join(l).encode("utf-8"))
fp.close()