Components and supplies
PIR Motion Sensor (generic)
Arduino Yun Shield
USB WebCam
Project description
Code
Auth Script
python
Script for authentication
1import urlparse 2import oauth2 as oauth 3 4consumer_key = '' 5consumer_secret = '' 6 7request_token_url = 'http://www.tumblr.com/oauth/request_token' 8access_token_url = 'http://www.tumblr.com/oauth/access_token' 9authorize_url = 'http://www.tumblr.com/oauth/authorize' 10 11consumer = oauth.Consumer(consumer_key, consumer_secret) 12client = oauth.Client(consumer) 13 14# Step 1: Get a request token. This is a temporary token that is used for 15# having the user authorize an access token and to sign the request to obtain 16# said access token. 17 18resp, content = client.request(request_token_url, "GET") 19if resp['status'] != '200': 20 raise Exception("Invalid response %s." % resp['status']) 21 22request_token = dict(urlparse.parse_qsl(content)) 23 24print "Request Token:" 25print " - oauth_token = %s" % request_token['oauth_token'] 26print " - oauth_token_secret = %s" % request_token['oauth_token_secret'] 27print 28 29# Step 2: Redirect to the provider. Since this is a CLI script we do not 30# redirect. In a web application you would redirect the user to the URL 31# below. 32 33print "Go to the following link in your browser:" 34print "%s?oauth_token=%s" % (authorize_url, request_token['oauth_token']) 35print 36 37# After the user has granted access to you, the consumer, the provider will 38# redirect you to whatever URL you have told them to redirect to. You can 39# usually define this in the oauth_callback argument as well. 40accepted = 'n' 41while accepted.lower() == 'n': 42 accepted = raw_input('Have you authorized me? (y/n) ') 43oauth_verifier = raw_input('What is the PIN? ') 44 45# Step 3: Once the consumer has redirected the user back to the oauth_callback 46# URL you can request the access token the user has approved. You use the 47# request token to sign this request. After this is done you throw away the 48# request token and use the access token returned. You should store this 49# access token somewhere safe, like a database, for future use. 50token = oauth.Token(request_token['oauth_token'], 51 request_token['oauth_token_secret']) 52token.set_verifier(oauth_verifier) 53client = oauth.Client(consumer, token) 54 55resp, content = client.request(access_token_url, "POST") 56access_token = dict(urlparse.parse_qsl(content)) 57 58print "Access Token:" 59print " - oauth_token = %s" % access_token['oauth_token'] 60print " - oauth_token_secret = %s" % access_token['oauth_token_secret'] 61print 62print "You may now access protected resources using the access tokens above." 63print 64
Python Script
python
Upload photos to tumblr
1import glob 2import json 3import os 4import time 5import urllib2 6import 7 urlparse 8import oauth2 9from poster.encode import multipart_encode 10from 11 poster.streaminghttp import register_openers 12 13class APIError(StandardError): 14 15 def __init__(self, msg, response=None): 16 StandardError.__init__(self, 17 msg) 18 19class TumblrAPIv2: 20 def __init__(self, consumer_key, consumer_secret, 21 oauth_token, oauth_token_secret): 22 self.consumer = oauth2.Consumer(consumer_key, 23 consumer_secret) 24 self.token = oauth2.Token(oauth_token, oauth_token_secret) 25 26 self.url = "http://api.tumblr.com" 27 28 def parse_response(self, 29 result): 30 content = json.loads(result) 31 if 400 <= int(content["meta"]["status"]) 32 <= 600: 33 raise APIError(content["meta"]["msg"], result) 34 return 35 content["response"] 36 37 def createPhotoPost(self, id, post): 38 url 39 = self.url + "/v2/blog/%s/post" %id 40 41 img_file = post['data'] 42 43 del(post['data']) 44 req = oauth2.Request.from_consumer_and_token(self.consumer, 45 46 token=self.token, 47 http_method="POST", 48 49 http_url=url, 50 parameters=post) 51 52 req.sign_request(oauth2.SignatureMethod_HMAC_SHA1(), self.consumer, self.token) 53 54 compiled_postdata = req.to_postdata() 55 all_upload_params = urlparse.parse_qs(compiled_postdata, 56 keep_blank_values=True) 57 58 for key, val in all_upload_params.iteritems(): 59 60 all_upload_params[key] = val[0] 61 62 all_upload_params['data'] 63 = open(img_file, 'rb') 64 datagen, headers = multipart_encode(all_upload_params) 65 66 request = urllib2.Request(url, datagen, headers) 67 68 try: 69 70 respdata = urllib2.urlopen(request).read() 71 except urllib2.HTTPError, 72 ex: 73 return 'Received error code: ', ex.code 74 75 return 76 self.parse_response(respdata) 77 78register_openers() 79 80# Fill with your 81 credentials 82 83CONSUMER_KEY = '****' 84CONSUMER_SECRET = '****' 85OAUTH_TOKEN 86 = '****' 87OAUTH_TOKEN_SECRET = '****' 88 89 90DIR = '/root/photos/' 91FILE_MASK 92 = '*.jpg' 93BLOG = '****' # put here the name of your blog i.e. arduino.tumblr.com 94 95 96api 97 = TumblrAPIv2(CONSUMER_KEY, CONSUMER_SECRET, OAUTH_TOKEN, OAUTH_TOKEN_SECRET) 98 99for 100 img in glob.glob( os.path.join(DIR, FILE_MASK) ): 101 102 date = time.gmtime(os.path.getmtime(img)) 103 104 post = { 105 'type' : 'photo', 106 'date' : time.strftime ("%Y-%m-%d 107 %H:%M:%S", date), 108 'data' : img, 109 'tags' : "I am a tag", 110 111 'caption' : "I am a caption" 112 } 113 114 try: 115 response 116 = api.createPhotoPost(BLOG,post) 117 if 'id' in response: 118 print 119 response['id'] 120 else: 121 print response 122 break 123 124 125 except APIError: 126 print "Error" 127 break 128 129print "Done!" 130
Python Script
python
Upload photos to tumblr
1import glob 2import json 3import os 4import time 5import urllib2 6import urlparse 7import oauth2 8from poster.encode import multipart_encode 9from poster.streaminghttp import register_openers 10 11class APIError(StandardError): 12 def __init__(self, msg, response=None): 13 StandardError.__init__(self, msg) 14 15class TumblrAPIv2: 16 def __init__(self, consumer_key, consumer_secret, oauth_token, oauth_token_secret): 17 self.consumer = oauth2.Consumer(consumer_key, consumer_secret) 18 self.token = oauth2.Token(oauth_token, oauth_token_secret) 19 self.url = "http://api.tumblr.com" 20 21 def parse_response(self, result): 22 content = json.loads(result) 23 if 400 <= int(content["meta"]["status"]) <= 600: 24 raise APIError(content["meta"]["msg"], result) 25 return content["response"] 26 27 def createPhotoPost(self, id, post): 28 url = self.url + "/v2/blog/%s/post" %id 29 30 img_file = post['data'] 31 del(post['data']) 32 req = oauth2.Request.from_consumer_and_token(self.consumer, 33 token=self.token, 34 http_method="POST", 35 http_url=url, 36 parameters=post) 37 req.sign_request(oauth2.SignatureMethod_HMAC_SHA1(), self.consumer, self.token) 38 compiled_postdata = req.to_postdata() 39 all_upload_params = urlparse.parse_qs(compiled_postdata, keep_blank_values=True) 40 41 for key, val in all_upload_params.iteritems(): 42 all_upload_params[key] = val[0] 43 44 all_upload_params['data'] = open(img_file, 'rb') 45 datagen, headers = multipart_encode(all_upload_params) 46 request = urllib2.Request(url, datagen, headers) 47 48 try: 49 respdata = urllib2.urlopen(request).read() 50 except urllib2.HTTPError, ex: 51 return 'Received error code: ', ex.code 52 53 return self.parse_response(respdata) 54 55register_openers() 56 57# Fill with your credentials 58 59CONSUMER_KEY = '****' 60CONSUMER_SECRET = '****' 61OAUTH_TOKEN = '****' 62OAUTH_TOKEN_SECRET = '****' 63 64 65DIR = '/root/photos/' 66FILE_MASK = '*.jpg' 67BLOG = '****' # put here the name of your blog i.e. arduino.tumblr.com 68 69 70api = TumblrAPIv2(CONSUMER_KEY, CONSUMER_SECRET, OAUTH_TOKEN, OAUTH_TOKEN_SECRET) 71 72for img in glob.glob( os.path.join(DIR, FILE_MASK) ): 73 74 date = time.gmtime(os.path.getmtime(img)) 75 post = { 76 'type' : 'photo', 77 'date' : time.strftime ("%Y-%m-%d %H:%M:%S", date), 78 'data' : img, 79 'tags' : "I am a tag", 80 'caption' : "I am a caption" 81 } 82 83 try: 84 response = api.createPhotoPost(BLOG,post) 85 if 'id' in response: 86 print response['id'] 87 else: 88 print response 89 break 90 91 except APIError: 92 print "Error" 93 break 94 95print "Done!" 96
Auth Script
python
Script for authentication
1import urlparse 2import oauth2 as oauth 3 4consumer_key = '' 5consumer_secret = '' 6 7request_token_url = 'http://www.tumblr.com/oauth/request_token' 8access_token_url = 'http://www.tumblr.com/oauth/access_token' 9authorize_url = 'http://www.tumblr.com/oauth/authorize' 10 11consumer = oauth.Consumer(consumer_key, consumer_secret) 12client = oauth.Client(consumer) 13 14# Step 1: Get a request token. This is a temporary token that is used for 15# having the user authorize an access token and to sign the request to obtain 16# said access token. 17 18resp, content = client.request(request_token_url, "GET") 19if resp['status'] != '200': 20 raise Exception("Invalid response %s." % resp['status']) 21 22request_token = dict(urlparse.parse_qsl(content)) 23 24print "Request Token:" 25print " - oauth_token = %s" % request_token['oauth_token'] 26print " - oauth_token_secret = %s" % request_token['oauth_token_secret'] 27print 28 29# Step 2: Redirect to the provider. Since this is a CLI script we do not 30# redirect. In a web application you would redirect the user to the URL 31# below. 32 33print "Go to the following link in your browser:" 34print "%s?oauth_token=%s" % (authorize_url, request_token['oauth_token']) 35print 36 37# After the user has granted access to you, the consumer, the provider will 38# redirect you to whatever URL you have told them to redirect to. You can 39# usually define this in the oauth_callback argument as well. 40accepted = 'n' 41while accepted.lower() == 'n': 42 accepted = raw_input('Have you authorized me? (y/n) ') 43oauth_verifier = raw_input('What is the PIN? ') 44 45# Step 3: Once the consumer has redirected the user back to the oauth_callback 46# URL you can request the access token the user has approved. You use the 47# request token to sign this request. After this is done you throw away the 48# request token and use the access token returned. You should store this 49# access token somewhere safe, like a database, for future use. 50token = oauth.Token(request_token['oauth_token'], 51 request_token['oauth_token_secret']) 52token.set_verifier(oauth_verifier) 53client = oauth.Client(consumer, token) 54 55resp, content = client.request(access_token_url, "POST") 56access_token = dict(urlparse.parse_qsl(content)) 57 58print "Access Token:" 59print " - oauth_token = %s" % access_token['oauth_token'] 60print " - oauth_token_secret = %s" % access_token['oauth_token_secret'] 61print 62print "You may now access protected resources using the access tokens above." 63print 64
Downloadable files
Pir Schematic
Pir Schematic
Comments
Only logged in users can leave comments
Arduino_Genuino
60 Followers
•83 Projects
0