def action(): """ Posts marketing blurbs to Twitter. Cron: Depends on offering period. """ import random from webapp.libs.twitterbot import tweet_status # get bot settings bot = TwitterBot.get() # get the time epoch = int(time.time()) if not bot: print "The marketing bot is disabled." return action if bot.announce == 0: print "The marketing bot is disabled." return action # if updated + announce > current time, do an update! if epoch > (bot.updated + (bot.announce * 3600)): # make up some stuff blurbs = [ "Get your hot-n-fresh #openstack instances! ", "Instances for nothing and #bitcoins for free. ", "Now serving #42. ", "Pssst. Hey buddy, want some #openstack? ", "Sorry #openstack, we're all out of IPv4 addresses. ", "Any significantly advanced technology is indistinguishable from magic. ", "It's #bitcoin magic! ", "I'm hungry. Spare some #bitcoin? " ] hashtags = [ "trust", "cryptocurrency", "transparency", "globalcloud", "federation", "virtualization", "monkeys" ] blurb = random.choice(blurbs) hashtag = random.choice(hashtags) # say it tweet_status("%s '@%s !status' for more info. #%s" % (blurb, bot.screen_name, hashtag)) # update bot.updated = int(time.time()) bot.update()
def task(bot): from webapp.libs.twitterbot import tweet_status, run_status, reserve_instance, check_instance, cleanup_reservations # get unhandled commands commands = db.session.query(TweetCommands).filter_by(state=1).all() for command in commands: # someone typed '!instance' if command.command == "instance": # reserve instance response = reserve_instance(command, bot) if response['response'] == "error": print response['result']['message'] command.delete(command) # someone typed '!status' elif command.command.lower() == "status": # send status info response = run_status(command, bot) if response['response'] == "error": print response['result'] # don't hold onto status commands command.delete(command) elif command.command.lower() == "help": response = tweet_status( "'@%s !instance ^http://pastebin⋅com/raw.php?i=zX5fD6HY' & pay. Edit pastebin to suit! Also, '@%s !status'." % (bot.screen_name, bot.screen_name), command.user) if response['response'] == "error": print response['result'] command.delete(command) else: # some other command or errant tweet command.delete(command) # update status of commands carrying an instance_id and update commands = db.session.query(TweetCommands).filter_by().all() for command in commands: # run an instance check check_instance(command, bot) # get pending reserved instance commands commands = db.session.query(TweetCommands).filter_by(state=10).all() for command in commands: cleanup_reservations(command, bot) return
def task(bot): from webapp.libs.twitterbot import tweet_status, run_status, reserve_instance, check_instance, cleanup_reservations # get unhandled commands commands = db.session.query(TweetCommands).filter_by(state=1).all() for command in commands: # someone typed '!instance' if command.command == "instance": # reserve instance response = reserve_instance(command, bot) if response['response'] == "error": print response['result']['message'] command.delete(command) # someone typed '!status' elif command.command.lower() == "status": # send status info response = run_status(command, bot) if response['response'] == "error": print response['result'] # don't hold onto status commands command.delete(command) elif command.command.lower() == "help": response = tweet_status("'@%s !instance ^http://pastebin⋅com/raw.php?i=zX5fD6HY' & pay. Edit pastebin to suit! Also, '@%s !status'." % (bot.screen_name, bot.screen_name), command.user) if response['response'] == "error": print response['result'] command.delete(command) else: # some other command or errant tweet command.delete(command) # update status of commands carrying an instance_id and update commands = db.session.query(TweetCommands).filter_by().all() for command in commands: # run an instance check check_instance(command, bot) # get pending reserved instance commands commands = db.session.query(TweetCommands).filter_by(state=10).all() for command in commands: cleanup_reservations(command, bot) return
def configure_twitter_tweet(): # build response response = { "response": "success", "result": { "message": "Message goes here." } } # what are we suppose to do? (TODO convert to JSON POST, that's what) action = request.form.getlist('action')[0] # bot settings bot = TwitterBot.get() # tweet that shit if action == "tweet": ask = "%0.6f" % (float(bot.flavor.ask) / 1000000) response = tweet_status( u"Up to (%s) %s instances are now on sale for %s μBTC/hour via '@%s !status'." % (bot.max_instances, bot.flavor.name, ask, bot.screen_name)) # disconnect twitter creds elif action == "disconnect": # this MUST say 'settings' for stream restart tweet_status("Appliance !settings dropping bot credentials.") bot.delete(bot) response['result'][ 'message'] = "Twitter credentials have been removed." # enable/disable bot elif action == "enabled": # this MUST say 'settings' for stream restart tweet_status("Appliance !settings enabling instance bot.") bot.enabled = True bot.update() response['result']['message'] = "Twitter bot has been enabled." elif action == "disabled": # this MUST say 'settings' for stream restart tweet_status( "Appliance !settings disabling bot temporarily. I'll be back.") bot.enabled = False bot.update() response['result']['message'] = "Twitter bot has been disabled." return jsonify(response)
def configure_twitter_tweet(): # build response response = {"response": "success", "result": {"message": "Message goes here."}} # what are we suppose to do? (TODO convert to JSON POST, that's what) action = request.form.getlist('action')[0] # bot settings bot = TwitterBot.get() # tweet that shit if action == "tweet": ask = "%0.6f" % (float(bot.flavor.ask)/1000000) response = tweet_status( u"Up to (%s) %s instances are now on sale for %s μBTC/hour via '@%s !status'." % ( bot.max_instances, bot.flavor.name, ask, bot.screen_name ) ) # disconnect twitter creds elif action == "disconnect": # this MUST say 'settings' for stream restart tweet_status("Appliance !settings dropping bot credentials.") bot.delete(bot) response['result']['message'] = "Twitter credentials have been removed." # enable/disable bot elif action == "enabled": # this MUST say 'settings' for stream restart tweet_status("Appliance !settings enabling instance bot.") bot.enabled = True bot.update() response['result']['message'] = "Twitter bot has been enabled." elif action == "disabled": # this MUST say 'settings' for stream restart tweet_status("Appliance !settings disabling bot temporarily. I'll be back.") bot.enabled = False bot.update() response['result']['message'] = "Twitter bot has been disabled." return jsonify(response)
def configure_twitter(): # check configuration settings = Status().check_settings() # get the forms for the page form = TwitterForm(request.form) mrof = BotForm(request.form) # blow the flavors into a list flavor_list = [] flavors = Flavors.get_all() for flavor in flavors: flavor_list.append((flavor.id, flavor.description)) mrof.flavor.choices = flavor_list # twitter bot credentials bot = TwitterBot.get() # initialize the bot if it's not if not bot: bot = oauth_initialize() else: if bot.complete == 0: bot = oauth_initialize() if bot.complete == 1 and request.method == "GET": # ensure we don't use stale creds bot = oauth_initialize() elif bot.complete == 1 and request.method == "POST": if form.validate_on_submit(): pin = request.form["pin"] bot = oauth_complete(pin) if bot: flash("Authentication with Twitter complete.", "success") else: flash("Authentication with Twitter failed.", "error") bot = TwitterBot.get() bot.delete(bot) return redirect(url_for(".configure_twitter")) else: # form was not valid, so show errors flash("There were form errors. Please check your entries and try again.", "error") elif request.method == "POST": if mrof.validate_on_submit(): bot.flavor_id = mrof.flavor.data bot.announce = mrof.announce.data bot.max_instances = mrof.max_instances.data bot.updated = int(time.time()) bot.update() if bot.announce > 0: # announce (requires 'settings' in comment to reload stream bot) tweet_status( "Appliance settings updated. Now serving up to (%s) %s instances via '@%s !status'" % (bot.max_instances, bot.flavor.name, bot.screen_name) ) flash("Bot settings updated.", "success") else: # form was not valid, so show errors flash("There were form errors. Please check your entries and try again.", "error") # no bot not hot if not bot: flash("Twitterbot failed to contact Twitter or get credentials.", "error") bot = None else: # set default form values mrof.flavor.data = bot.flavor_id mrof.announce.data = bot.announce return render_template("configure/twitter.html", bot=bot, settings=settings, form=form, mrof=mrof)
def configure_twitter(): # check configuration settings = Status().check_settings() # get the forms for the page form = TwitterForm(request.form) mrof = BotForm(request.form) # blow the flavors into a list flavor_list = [] flavors = Flavors.get_all() for flavor in flavors: flavor_list.append((flavor.id, flavor.description)) mrof.flavor.choices = flavor_list # twitter bot credentials bot = TwitterBot.get() # initialize the bot if it's not if not bot: bot = oauth_initialize() else: if bot.complete == 0: bot = oauth_initialize() if bot.complete == 1 and request.method == 'GET': # ensure we don't use stale creds bot = oauth_initialize() elif bot.complete == 1 and request.method == 'POST': if form.validate_on_submit(): pin = request.form['pin'] bot = oauth_complete(pin) if bot: flash("Authentication with Twitter complete.", "success") else: flash("Authentication with Twitter failed.", "error") bot = TwitterBot.get() bot.delete(bot) return redirect(url_for(".configure_twitter")) else: # form was not valid, so show errors flash( "There were form errors. Please check your entries and try again.", "error") elif request.method == 'POST': if mrof.validate_on_submit(): bot.flavor_id = mrof.flavor.data bot.announce = mrof.announce.data bot.max_instances = mrof.max_instances.data bot.updated = int(time.time()) bot.update() if bot.announce > 0: # announce (requires 'settings' in comment to reload stream bot) tweet_status( "Appliance settings updated. Now serving up to (%s) %s instances via '@%s !status'" % (bot.max_instances, bot.flavor.name, bot.screen_name)) flash("Bot settings updated.", "success") else: # form was not valid, so show errors flash( "There were form errors. Please check your entries and try again.", "error") # no bot not hot if not bot: flash("Twitterbot failed to contact Twitter or get credentials.", "error") bot = None else: # set default form values mrof.flavor.data = bot.flavor_id mrof.announce.data = bot.announce return render_template('configure/twitter.html', bot=bot, settings=settings, form=form, mrof=mrof)