]>
jfr.im git - z_archive/twitter.git/blob - twitter/util.py
2 Internal utility functions.
4 `htmlentitydecode` came from here:
5 http://wiki.python.org/moin/EscapingHtml
8 from __future__
import print_function
18 from html
.entities
import name2codepoint
21 from htmlentitydefs
import name2codepoint
23 def htmlentitydecode(s
):
25 '&(%s);' % '|'.join(name2codepoint
),
26 lambda m
: unichr(name2codepoint
[m
.group(1)]), s
)
28 def smrt_input(globals_
, locals_
, ps1
=">>> ", ps2
="... "):
35 inputs
.append(input(prompt
))
37 ret
= eval('\n'.join(inputs
), globals_
, locals_
)
44 def printNicely(string
):
45 if hasattr(sys
.stdout
, 'buffer'):
46 sys
.stdout
.buffer.write(string
.encode('utf8'))
49 print(string
.encode('utf8'))
51 __all__
= ["htmlentitydecode", "smrt_input"]
54 print(msg
, file=sys
.stderr
)
57 """A class to count fails during a repetitive task.
60 maximum: An integer for the maximum of fails to allow.
61 exit: An integer for the exit code when maximum of fail is reached.
64 count: Count a fail, exit when maximum of fails is reached.
65 wait: Same as count but also sleep for a given time in seconds.
67 def __init__(self
, maximum
=10, exit
=1):
74 err("Too many consecutive fails, exiting.")
75 raise SystemExit(self
.exit
)
77 def wait(self
, delay
=0):
84 """Find all links in the given line. The function returns a sprintf style
85 format string (with %s placeholders for the links) and a list of urls."""
86 l
= line
.replace(u
"%", u
"%%")
87 regex
= "(https?://[^ )]+)"
89 re
.sub(regex
, "%s", l
),
90 [m
.group(1) for m
in re
.finditer(regex
, l
)])
92 def follow_redirects(link
, sites
= None):
93 """Follow directs for the link as long as the redirects are on the given
94 sites and return the resolved link."""
96 return sites
== None or urlparse
.urlparse(url
).hostname
in sites
98 class RedirectHandler(urllib2
.HTTPRedirectHandler
):
101 def redirect_request(self
, req
, fp
, code
, msg
, hdrs
, newurl
):
102 self
.last_url
= newurl
103 if not follow(newurl
):
105 r
= urllib2
.HTTPRedirectHandler
.redirect_request(
106 self
, req
, fp
, code
, msg
, hdrs
, newurl
)
107 r
.get_method
= lambda : 'HEAD'
112 redirect_handler
= RedirectHandler()
113 opener
= urllib2
.build_opener(redirect_handler
)
114 req
= urllib2
.Request(link
)
115 req
.get_method
= lambda : 'HEAD'
117 with contextlib
.closing(opener
.open(req
)) as site
:
119 except (urllib2
.HTTPError
, urllib2
.URLError
):
120 return redirect_handler
.last_url
if redirect_handler
.last_url
else link
122 def expand_line(line
, sites
):
123 """Expand the links in the line for the given sites."""
125 msg_format
, links
= find_links(l
)
126 args
= tuple(follow_redirects(l
, sites
) for l
in links
)
127 return msg_format
% args
129 def parse_host_list(list_of_hosts
):
130 """Parse the comma separated list of hosts."""
132 m
.group(1) for m
in re
.finditer("\s*([^,\s]+)\s*,?\s*", list_of_hosts
))