-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjoke.py
75 lines (63 loc) · 1.91 KB
/
joke.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import pyttsx3
import json
import requests
from urllib.request import urlopen
import pandas as pd
import numpy as np
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
sensor = 24
GPIO.setup(sensor, GPIO.IN, pull_up_down = GPIO.PUD_UP)
file_name = "jokes.csv"
#jokes_list = pd.read_csv("/home/pi/Documents/joker-main/" +file_name)
joke = []
f = r"https://official-joke-api.appspot.com/random_joke"
#Function executed on signal detection
def active(null):
try:
url = "https://www.google.com"
file=urlopen(url)
status = "Connected"
except:
print("No internet connection.")
file_name = '/home/pi/Documents/joker-main/jokes.csv'
jokes_list = open(file_name, 'r')
csvFileArray = []
for row in csv.reader(jokes_list, delimiter = '.'):
csvFileArray.append(row)
total_row = len(csvFileArray)
joke = csvFileArray[randint(0, total_row)]
if status == "Connected":
print("Connected to the Internet")
data = requests.get(f)
a = json.loads(data.text)
part1 = (a["setup"],)
part2 = (a["punchline"],)
joke = part1 + part2
else:
print("No internet connection.")
file_name = '/home/pi/Documents/Jokeproject/jokes.csv'
jokes_list = open(file_name, 'r')
csvFileArray = []
for row in csv.reader(jokes_list, delimiter = '.'):
csvFileArray.append(row)
total_row = len(csvFileArray)
joke = csvFileArray[randint(0, total_row)]
converter = pyttsx3.init()
# Sets speed percent
# Can be more than 100
converter.setProperty('rate', 150)
# Set volume 0-1
converter.setProperty('volume', 1)
converter.say(joke)
converter.runAndWait()
#On detecting signal (falling edge), active function will be activated.
GPIO.add_event_detect(sensor, GPIO.FALLING, callback=active, bouncetime=100)
# main program loop
try:
while True:
time.sleep(0.05)
# Scavenging work after the end of the program
except KeyboardInterrupt:
GPIO.cleanup()