Incorporating into a Python Script

58 views
Skip to first unread message

George Johnson

unread,
Aug 28, 2013, 7:22:20 AM8/28/13
to supertweet-...@googlegroups.com


Hi

I have a basic Python script - as below

http://pastebin.com/mFZq1rLr

and want to send SuperTweet messages after the print statements in my script

I have Supertweet all setup and am able to Tweet from the command line ... but not sure
where to start with incorporating SuperTweet into my Python script

Any help would be much appreciated


George Johnson

unread,
Aug 30, 2013, 8:55:53 AM8/30/13
to supertweet-...@googlegroups.com


ok - it would appear the way forward is to execute a bash script via my python script - I have created a solenoid_closed.sh tweet script but getting the following SyntaxError



solenoid_closed.sh script below

file being executed in Python via - execfile('solenoid_closed.sh')

any help appreciated


Mr Blog

unread,
Aug 30, 2013, 11:04:21 PM8/30/13
to supertweet-...@googlegroups.com
You can definitely make REST API calls directly from Python. Not sure if you have Python 2.x or 3.x but ih 2.x you can use httplib as in the attached example (also copied below) that shows tweeting a message:

import httplib
import base64
import string
import urllib
 
url = "/1.1/statuses/update.json"
username = 'user'
password = 'pass'
message = 'Playing with Python and the Supertweet API'
 
# base64 encode the username and password
auth = base64.encodestring('%s:%s' % (username, password)).replace('\n', '')
 
body = urllib.urlencode({'status': message})

webservice = httplib.HTTP(host)
# write your headers
webservice.putrequest("POST", url)
webservice.putheader("Host", host)
webservice.putheader("User-Agent", "Python http auth")
webservice.putheader("Content-type", "application/x-www-form-urlencoded")
webservice.putheader("Content-length", "%d" % len(body))
# write the Authorization header like: 'Basic base64encode(username + ':' + password)
webservice.putheader("Authorization", "Basic %s" % auth)
 
webservice.endheaders()
webservice.send(body)
# get the response
statuscode, statusmessage, header = webservice.getreply()
print "Response: ", statuscode, statusmessage
print "Headers: ", header
res = webservice.getfile().read()
print 'Content: ', res
example.py

George Johnson

unread,
Aug 31, 2013, 7:47:41 AM8/31/13
to supertweet-...@googlegroups.com
Mr Blog - thanks for your reply

I am using Python 2.7.3.

Calling your script from within my Python script I am able to post to Twitter but have a couple of questions

Sometimes when the script is executed I get a 410 Gone Error ... and am told that the Tweet failed. If I look on Twitter it did actually go through ?

Having tested this a few times I am now getting 403 Forbidden Errors which I am guessing is down to Twitter's API detecting duplicate tweets ?

Is there any way around the duplicate tweets 403 error ? .... is there a time period before you can start sending the same message again without getting the 403 error ? .... or is there any way of adapting your script to send random messages (of a similar vein) ?

Mr Blog

unread,
Aug 31, 2013, 1:36:43 PM8/31/13
to supertweet-...@googlegroups.com
I thought by providing plain simple Python code you would be able to take it from there, embedding the code into your script as needed.

Of course you can modify the code to add randomness or anything you want. We assumed since you asked about Python and are writing your app in Python, you would know what you wanted to do from here using Python.

All of those error responses relate to rate-limiting or other API abuse, so go easy with what you do to circumvent them. Slow down the rate. Make the messages unique and informing.

Robbie Wiggins

unread,
Sep 4, 2013, 6:04:59 AM9/4/13
to supertweet-...@googlegroups.com
why don't you make a python script that accepts command line arguments first i.e

tweet.py this message

then in your print statements do something like a os.system call and have it do "python tweet.py Tank 2 H/L Float Has Activated - Solenoid Valve Closed - Filling Has Stopped"


On Wednesday, 28 August 2013 12:22:20 UTC+1, George Johnson wrote:
Reply all
Reply to author
Forward
0 new messages