Example #1
0
def start():
    # Verify credentials exist. If not, give some suggestions.
    if not os.path.isfile(rootLoc + "creds"):
        print "Could not locate credentials file."
        print "Did you try running romspam with the auth option?"
        print "See help for more info ('romspam help')."
        sys.exit()

    # Get credentials
    print "Getting credentials. Please help me decrypt!"
    creds = romcreds.readcreds(rootLoc + "creds")

    # Authenticate with twitter
    api = romtwitter.authenticate(creds)

    # Request name of user to send tweet to
    user = raw_input("Thanks!\nEnter bae's name: ").strip()
    if len(user) == 0 or len(user) > 15:
        print "Invalid name entered."
        sys.exit()
    if user[0] == "@":
        user = user[1:]

    # Everything is set up to run. Now we just send a quote every 15 minutes.
    try:
        while True:
            # Get a quote to send. We want it to be unique, so we must check against
            # the sent quotes file. If this file doesn't exist it's obviously unique.
            if os.path.isfile(rootLoc + "sent"):
                with open(rootLoc + "sent", "r") as f:
                    sentquotes = f.read()
                    if len(sentquotes) == 0:
                        sent = set({})
                    else:
                        sent = set(json.loads(sentquotes))
            else:
                sent = set({})
            quote = romquote.getquote()
            while quote in sent or not isinstance(quote, str):
                quote = romquote.getquote()

            # If we have a short tweet, we want to add a picture, if one exists.
            if len(quote) + len(user) + 2 <= 140:
                image = romimage.getimage(rootLoc + "images")
            else:
                image = None

            # Send tweet
            print "Sending..."
            try:
                romtwitter.sendtweet(api, user, quote, image)
            except Exception as e:
                print e.message
                print "Image=" + image
                continue
            print quote
            print "Done at " + time.strftime("%I:%M%p") + "! Waiting..."

            # Add quote to the sent set and write out
            sent.add(quote)
            with open(rootLoc + "sent", "w+") as f:
                f.write(json.dumps(sent, cls=setencoder.SetEncoder))

            # Add image to sent directory, if it exists
            if image is not None:
                romimage.moveimagesent(image)

            # Wait 15 minutes
            time.sleep(60 * 15)
    except KeyboardInterrupt:
        print "\nHappy Valentine's Day!"
        pass
Example #2
0
def quote():
    print romquote.getquote()