search search twitter (Beware: octothorpe, escape it)
set set your twitter status
shell login to the twitter shell
+ rate get your current rate limit status (remaining API reqs)
OPTIONS:
from getpass import getpass
import re
import os.path
+import locale
+import string
+
try:
from ConfigParser import SafeConfigParser
except ImportError:
from .oauth import OAuth, write_token_file, read_token_file
from .oauth_dance import oauth_dance
from . import ansi
-from .util import smrt_input
+from .util import smrt_input, printNicely
OPTIONS = {
'action': 'friends',
'datestamp', 'no-ssl']
short_opts = "e:p:f:h?rR:c:l:td"
opts, extra_args = getopt(args, short_opts, long_opts)
+ extra_args = [arg.decode(locale.getpreferredencoding())
+ for arg in extra_args]
for opt, arg in opts:
if opt in ('-f', '--format'):
def __call__(self, twitter, options):
raise NoSuchActionError("No such action: %s" %(options['action']))
-def printNicely(string):
- if hasattr(sys.stdout, 'buffer'):
- sys.stdout.buffer.write(string.encode('utf8'))
- print()
- else:
- print(string.encode('utf8'))
-
class StatusAction(Action):
def __call__(self, twitter, options):
statuses = self.getStatuses(twitter, options)
statusTxt = (" ".join(options['extra_args'])
if options['extra_args']
else str(input("message: ")))
- twitter.statuses.update(status=statusTxt)
+ replies = []
+ ptr = re.compile("@[\w_]+")
+ while statusTxt:
+ s = ptr.match(statusTxt)
+ if s and s.start() == 0:
+ replies.append(statusTxt[s.start():s.end()])
+ statusTxt = statusTxt[s.end()+1:]
+ else:
+ break
+ replies = " ".join(replies)
+ if len(replies) >= 140:
+ # just go back
+ statusTxt = replies
+ replies = ""
+
+ splitted = []
+ while statusTxt:
+ limit = 140 - len(replies)
+ if len(statusTxt) > limit:
+ end = string.rfind(statusTxt, ' ', 0, limit)
+ else:
+ end = limit
+ splitted.append(" ".join((replies,statusTxt[:end])))
+ statusTxt = statusTxt[end:]
+
+ for status in splitted:
+ twitter.statuses.update(status=status)
class TwitterShell(Action):
def __call__(self, twitter, options):
pass
+class RateLimitStatus(Action):
+ def __call__(self, twitter, options):
+ rate = twitter.account.rate_limit_status()
+ print("Remaining API requests: %s / %s (hourly limit)" % (rate['remaining_hits'], rate['hourly_limit']))
+ print("Next reset in %ss (%s)" % (int(rate['reset_time_in_seconds']-time.time()),
+ time.asctime(time.localtime(rate['reset_time_in_seconds']))))
+
actions = {
'authorize' : DoNothingAction,
'follow' : FollowAction,
'search' : SearchAction,
'set' : SetStatusAction,
'shell' : TwitterShell,
+ 'rate' : RateLimitStatus,
}
def loadConfig(filename):