Posted By

ksaver on 09/12/10


Tagged

python twitter OAuth


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

ksaver


chirpyt: Simple Python script to share a link to Twitter


 / Published in: Python
 

URL: http://twitter.com/chirpyt

"Whatever you do will be insignificant, but it is very important that you do it." --Mahatma Gandhi

  1. #! /usr/bin/env python
  2. #
  3. # chirpyt-0.1.py
  4. # http://twitter.com/chirpyt
  5. # A simple python script to share a link to Twitter stream :-)
  6. # ksaver (at identi.ca), Sep 11, 2010.
  7. # Public Domain Code.
  8. # Not warranty at all.
  9. #
  10. # In order to work, some requierements must be met:
  11. # BeautifulSoup: http://www.crummy.com/software/BeautifulSoup/
  12. # tweepy: http://github.com/joshthecoder/tweepy
  13. #
  14. # Usage:
  15. # ./chirpyt.py auth -To get a pair of oauth keys.
  16. # ./chirpyt.py help -To get this help output.
  17. # ./chirpyt.py http://example.com -To send a new link.
  18. # ./chirpyt.py -Without arguments.
  19. #
  20. # Elliot, be good.
  21.  
  22. import string
  23. import sys
  24. import tweepy
  25. import urllib
  26. import urllib2
  27.  
  28. from BeautifulSoup import BeautifulSoup as cooksoup
  29.  
  30. # Edit this two lines, and add your own keys.
  31. # To get a key pair, run ./chirpyt.py auth.
  32. #------------------------------------------------
  33. ACCESS_KEY = 'PASTE_HERE_YOUR_ACCESS_KEY'
  34. ACCESS_SECRET = 'PASTE_HERE_YOUR_ACCESS_SECRET'
  35. #------------------------------------------------
  36.  
  37. CONSUMER_KEY = 'D6gfLh3XYuyLrnlP3XYdQw'
  38. CONSUMER_SECRET = '7fete8FUruFaJPAl4NFVg3jF2TZDiNUG5FkW2ob0oM'
  39.  
  40. def authorize():
  41. auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
  42. auth_url = auth.get_authorization_url()
  43. print "----------------------------"
  44. print "Chirpyt authorization module"
  45. print "----------------------------\n"
  46. print "Please login to Twitter in your browser,",
  47. print "then open this URL and authorize (by clicking 'Allow'):"
  48. print auth_url
  49. print
  50.  
  51. verifier = ''
  52. while not verifier:
  53. verifier = raw_input("Enter here the numeric PIN: ").strip()
  54.  
  55. auth.get_access_token(verifier)
  56. print "Now, copy the next pair of keys, and paste them into the script."
  57. print
  58. print "ACCESS_KEY = '%s'" % auth.access_token.key
  59. print "ACCESS_SECRET = '%s'" % auth.access_token.secret
  60.  
  61. def chirpyt(tweet):
  62. auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
  63. auth.set_access_token(ACCESS_KEY, ACCESS_SECRET)
  64. api = tweepy.API(auth)
  65. api.update_status(tweet)
  66.  
  67. def get_html(url, params):
  68. uagent = 'Opera/9.80 (X11; FreeBSD 8.1-RELEASE i386; U; en)\
  69. Presto/2.6.30 Version/10.62'
  70. headers = {'User-Agent': uagent}
  71. request = urllib2.Request(url, params, headers)
  72. return urllib2.urlopen(request).read()
  73.  
  74. def get_title(url):
  75. htmldoc = get_html(url, None)
  76. soup = cooksoup(htmldoc)
  77. return soup.title.string.strip()
  78.  
  79. def short_url(longurl):
  80. shortener = 'http://ur1.ca/'
  81. webparams = {'longurl': longurl}
  82. encparams = urllib.urlencode(webparams)
  83. htmldoc = get_html(shortener, encparams)
  84. soup = cooksoup(htmldoc)
  85. return soup.p.text[string.find(soup.p.text, ':')+1:]
  86.  
  87. def help():
  88. myname = sys.argv[0]
  89. helpstring = '''
  90. Usage:
  91. ./%s auth -To get a pair of oauth keys.
  92. ./%s help -To get this help output.
  93. ./%s http://example.com -To tweet a new link.
  94. ./%s -Without arguments.
  95.  
  96. ''' % (myname, myname, myname, myname)
  97. return helpstring
  98.  
  99. def main(argv):
  100.  
  101. TITMAXLEN = 100
  102. TWTMAXLEN = 140
  103.  
  104. if len(argv) > 1:
  105. if argv[1] == 'auth':
  106. authorize()
  107. exit()
  108. elif argv[1] == 'help':
  109. print help()
  110. exit()
  111. else:
  112. url = argv[1]
  113. else:
  114. url = raw_input("Please enter a url: ")
  115.  
  116. page_title = get_title(url)[:TITMAXLEN]
  117. tweet = "%s: %s" % (page_title, url)
  118.  
  119. if len(tweet) > TWTMAXLEN:
  120. url = short_url(url)
  121. tweet = "%s: %s" % (page_title, url)
  122.  
  123. chirpyt(tweet)
  124. print "%s [%s chars]\n" % (tweet, len(tweet))
  125.  
  126.  
  127. if __name__ == '__main__':
  128. main(sys.argv)

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: ksaver on September 13, 2010

I think is not a big deal to share so easy the CONSUMER_KEY pair, so, I must to think another way to do so, or leave the twitter app registration process to the user... :-|

You need to login to post a comment.