Cisco 642-747 Dumps | Practice Tests | CCNP Wireless Cisco Certification


Retweeting a user

The first thing we need to do is get a list of the user’s latest tweets. We then loop through each tweet and get its creation time as a string, which is then converted to a datetime object. We then check that the tweet’s time is newer than the time the function was last called ?and if so, retweet the tweet.


Authenticating with Twitter

We’re going to create our bot as a class, where we authenticate with Twitter in the constructor. We take the tokens from the previous steps as parameters and use them to create an instance of the Twython API. We also have a variable, last_ran, which is set to the current time. This is used to check if there are new tweets later on.


The main section

The main section is straightforward. We create an instance of the bot class using our tokens, and then go into an infinite loop. In this loop, we check for any new retweets from the users we are monitoring (we could run the retweet task with different users), then update the time everything was last run, and sleep for five minutes.

Full code listing
#!/usr/bin/env python2 # A Twitter Bot for the Raspberry Pi that retweets any content from # @LinuxUserMag. Written by Liam Fraser for a Linux User & Developer article. import sys import time from datetime import datetime from twython import Twython class bot: def __init__(self, c_key, c_secret, a_token, a_token_ secret): # Create a Twython API instance self.api = Twython(c_key, c_secret, a_token, a_token_secret) # Make sure we are authenticated correctly try: self.api.verify_credentials() except: sys.exit(“Authentication Failed”) self.last_ran = @staticmethod def timestr_to_datetime(timestr): # Convert a string like Sat Nov 09 09:29:55 +0000 # 2013 to a datetime object. Get rid of the timezone # and make the year the current one timestr = “{0} {1}”.format(timestr[:19], datetime. now().year) # We now have Sat Nov 09 09:29:55 2013 return datetime.strptime(timestr, `%a %b %d %H:%M: %S %Y’) def retweet_task(self, screen_name): # Retweets any tweets we’ve not seen # from a user print “Checking for new tweets from format(screen_name) # Get a list of the users latest tweets timeline = self.api.get_user_timeline (screen_name = screen_name)

Get the code: 1RTgNSH

# Loop through each tweet and check if it was # posted since we were last called for t in timeline: tweet_time = bot.timestr_to_datetime (t[`created_at’]) if tweet_time > self.last_ran: print “Retweeting {0}”.format(t[`id’]) self.api.retweet(id = t[`id’]) if __name__ == “__main__”: # The consumer keys can be found on your application’s # Details page located at # apps(under “OAuth settings”) c_key=”” c_secret=”” # The access tokens can be found on your applications’s # Details page located at # (located under “Your access token”) a_token=”” a_token_secret=”” # Create an instance of the bot class twitter = bot(c_key, c_secret, a_token, a_token_secret) # Retweet anything new by @LinuxUserMag every 5 minutes while True: # Update the time after each retweet_task so we’re # only retweeting new stuff twitter.retweet_task(“LinuxUserMag”) twitter.last_ran = time.sleep(5 * 60)


The Arduino is better at dealing with things like servos and analog input

Program your Arduino with Raspberry Pi
Enjoy all the features and benefits of the Arduino microcontroller on your Raspberry Pi projects
What you’ll need
Arduino Uno Internet connectivity Nanpy

You might be wondering why you might want to attach an Arduino to your Raspberry Pi. While there are lots of reasons, probably the most poignant is the extra six PWM-capable pins and another six analogue pins that a standard Arduino Uno offers. You see, while the Raspberry Pi has an excellent array of pins and capabilities, it can’t do analogue and it can’t do realtime processing out of the box. With an Arduino, additions like servos, potentiometers and a whole array of analog sensors are trivially easy to trigger and control. The best part is that you don’t even have to program in Arduino’s quasi-C++ language. All you need is a standard USB connection between your Raspberry Pi and Arduino and a small Python package called Nanpy. Here’s how it’s done…


Grab an Arduino

Before you can do anything, you need an Arduino. We recommend the Uno, since it’s the default choice with the best balance of features, convenience and affordability. Since you’ll want to put it to use straight away, we recommend investing in a `starter kit’ that includes LEDs, servos and all that fun stuff.


Satisfying dependencies

We’re assuming you’re using Raspbian (recommended), so open your terminal because we need to get

Comments are closed.