Skip to content
This repository has been archived by the owner on Jun 8, 2019. It is now read-only.

Commit

Permalink
Added command line parsing.
Browse files Browse the repository at this point in the history
  • Loading branch information
lukeman committed May 1, 2009
1 parent e4e4500 commit bfd4de2
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 14 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ This script can be used either as a standalone utility script or as a library.

To test encoding a file, simply pass the script a binary file with the `encode` flag and it will print the toot array to standard out. Go hog wild if you wish, but know that large files will generate a huge amount of output.

python tootfiles.py --encode="helloworld.gif"
python tootfiles.py -encode="helloworld.gif"

To send that file to a Twitter stream, simply add your Twitter credentials using the `user` and `password` flags. I *highly* recommend setting up a second Twitter account to publish any data to. Small files are good here--keep it under 5KB for best results.

python tootfiles.py --encode="helloworld.gif" --user="someuser" --password="somepass"
python tootfiles.py --encode="helloworld.gif" -u "someuser" -p "somepass"

#### Decoding a published tootfile

Expand Down
72 changes: 60 additions & 12 deletions tootfiles.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"""

import sys
import getopt
import os
import math
import re
Expand All @@ -14,6 +15,8 @@
import md5
import time
import urllib
from optparse import OptionParser


# External dependencies, see README.md
import simplejson as json
Expand Down Expand Up @@ -51,7 +54,6 @@ def _segment(self, data, n=140):
slices.reverse()
slices = range(self.tootcount)
slices.reverse()
print slices

return [data[i*n:(i+1)*n] for i in slices]

Expand All @@ -76,10 +78,9 @@ def publish(self, username, password):
time.sleep(1)
print "Finished."


def __unicode__(self):
for toot in self.tootlist:
print toot

def __str__(self):
return "\n".join([toot for toot in self.tootlist])


class TootDecoder(object):
Expand All @@ -105,6 +106,9 @@ def write(self, filename=None, overwrite=False):
f = open(filename,'wb')
f.write(self.rawdata)
f.close()

def __str__(self):
return self.rawdata


def _grabheader(self):
Expand Down Expand Up @@ -159,12 +163,56 @@ def _decode(self):
self.md5hash = md5.new(self.rawdata).hexdigest()




def main():
# TODO

def encode(filename, username=None, password=None):
if not os.path.exists(filename):
raise Exception("Specified input file does not exist")

t = TootEncoder(filename)

if username is not None and password is not None:
t.publish(username, password)
else:
print(t)

if __name__ == '__main__':
main()
def decode(tootid):
"""Given a Twitter status ID, attempt to decode"""
t = TootDecoder(tootid)
t.retrieve()
print t

def main(argv=None):
usage = "usage: %prog [-d tootidtodecode] or [-e 'somefile' -u 'username' -p 'password']"

parser = OptionParser(usage=usage)
parser.add_option("-e", dest="filename", metavar="filename", default=None,
help="The name of the file you wish to encode")
parser.add_option("-u", dest="username", metavar="username", default=None,
help="A valid Twitter username")
parser.add_option("-p", dest="password", metavar="password", default=None,
help="The password to your twitter account")
parser.add_option("-d", dest="tootfileid", metavar="id", default=None,
help="Twitter ID of header to decode from")
(options, args) = parser.parse_args()

encoding = options.filename is not None
decoding = options.tootfileid is not None
havepass = options.password is not None
haveuser = options.password is not None

# This will catch cases where both encode and decode are selected and where neither is
if encoding is decoding:
parser.error("You must choose to either encode or decode. Use --help for more info.")

if decoding:
# Decode a file from the header with Twitter ID tootfileid
# and print to stdout
decode(options.tootfileid)
elif encoding and haveuser and havepass:
# Encode a file and publish to Twitter
encode(options.filename, options.username, options.password)
elif encoding:
# Encode a file and print to stdout
encode(options.filename)

if __name__ == "__main__":
sys.exit(main())

0 comments on commit bfd4de2

Please sign in to comment.