mylist get list of your lists; give a list name to get tweets
from that list
public get latest public tweets
+ pyprompt start a Python prompt for interacting with the twitter
+ object directly
replies get latest replies to you
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:
home directory.
"""
+from __future__ import print_function
+
CONSUMER_KEY='uS6hO2sV6tDKIOeVjhnFnQ'
CONSUMER_SECRET='MEYTOS97VvlHX7K1rwHPEqVpTSqZ71HtvoK4sVuYk'
from getpass import getpass
import re
import os.path
-from configparser import SafeConfigParser
+import locale
+import string
+
+try:
+ from ConfigParser import SafeConfigParser
+except ImportError:
+ from configparser import ConfigParser as SafeConfigParser
import datetime
-from urllib.parse import quote
+try:
+ from urllib.parse import quote
+except ImportError:
+ from urllib2 import quote
import webbrowser
from .api import Twitter, TwitterError
from .oauth import OAuth, write_token_file, read_token_file
from .oauth_dance import oauth_dance
from . import ansi
+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):
- sys.stdout.buffer.write(string.encode('utf8'))
- print()
-
class StatusAction(Action):
def __call__(self, twitter, options):
statuses = self.getStatuses(twitter, options)
# We need to bypass the TwitterCall parameter encoding, so we
# don't encode the plus sign, so we have to encode it ourselves
query_string = "+".join(
- [quote(term.decode(get_term_encoding()))
+ [quote(term)
for term in options['extra_args']])
results = twitter.search(q=query_string)['results']
class SetStatusAction(Action):
def __call__(self, twitter, options):
- statusTxt = (" ".join(options['extra_args']).decode(get_term_encoding())
+ statusTxt = (" ".join(options['extra_args'])
if options['extra_args']
else str(input("message: ")))
- status = (statusTxt.encode('utf8', 'replace'))
- twitter.statuses.update(status=status)
+ 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):
else:
raise SystemExit(0)
+class PythonPromptAction(Action):
+ def __call__(self, twitter, options):
+ try:
+ while True:
+ smrt_input(globals(), locals())
+ except EOFError:
+ pass
+
class HelpAction(Action):
def __call__(self, twitter, options):
print(__doc__)
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,
'help' : HelpAction,
'leave' : LeaveAction,
'public' : PublicAction,
+ 'pyprompt' : PythonPromptAction,
'replies' : RepliesAction,
'search' : SearchAction,
'set' : SetStatusAction,
'shell' : TwitterShell,
+ 'rate' : RateLimitStatus,
}
def loadConfig(filename):