Last active
October 17, 2016 15:20
-
-
Save bembu/688c342ac5b3798e463f43ffa5570b91 to your computer and use it in GitHub Desktop.
MB #3 -palvelinskripti
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Palvelinskripti. | |
Laheta pyynto osoitteeseen localhost:5000/photo | |
(esim. avaamalla selaimen). Kamera ottaa kuvan | |
ja lahettaa sen Telegram-kanavalle. | |
Koodista on vaihdettava <api-token> ja <group-id>, | |
jotta se toimii. | |
>> python selfie_cam.py | |
@author Peter Kronstrom | |
""" | |
from flask import Flask | |
import requests | |
import picamera | |
app = Flask(__name__) | |
@app.route('/photo', methods=['GET','POST']) | |
def handle_photo(): | |
camera_capture() | |
send_to_telegram() | |
return '{"success":"true"}' | |
def camera_capture(): | |
with picamera.PiCamera() as camera: | |
with open('image.jpg', 'wb') as capture_file: | |
camera.capture(capture_file) | |
def send_to_telegram(): | |
with open('image.jpg', 'rb') as capture_file: | |
url = 'https://api.telegram.org/bot<api-token>/sendPhoto' | |
f = {"photo": capture_file} | |
p = {'chat_id': <group-id>} | |
res = requests.post(url, files=f, params=p) | |
app.run(host="0.0.0.0") |
Replace api-token (String) and group-id (Int) before running
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Tested to be working on Python 3 and Python 2.7.
Installation:
sudo pip install Flask requests picamera
Run:
python selfie_cam.py