forked from glind/radical
-
Notifications
You must be signed in to change notification settings - Fork 1
/
joke_tweet.py
107 lines (91 loc) · 2.88 KB
/
joke_tweet.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# Import necessary libraries
import csv
import random
import requests
import tweepy
import schedule
import time
import json
# Set up Twitter API credentials
consumer_key = 'your_consumer_key'
consumer_secret = 'your_consumer_secret'
access_token = 'your_access_token'
access_token_secret = 'your_access_token_secret'
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
# Define API endpoints
joke_api_url = 'https://v2.jokeapi.dev/joke/Programming?blacklistFlags=nsfw'
geek_jokes_api_url = 'https://geek-jokes.sameerkumar.website/api'
official_joke_api_url = 'https://official-joke-api.appspot.com/jokes/programming/random'
# Define CSV file name
csv_file = 'jokes.csv'
# Function to add jokes to CSV file
def add_jokes_to_csv(jokes):
with open(csv_file, 'a', newline='') as f:
writer = csv.writer(f)
for joke in jokes:
writer.writerow([joke])
# Function to get jokes from JokeAPI
def get_jokes_from_jokeapi():
response = requests.get(joke_api_url)
data = response.json()
print(data)
jokes = []
if data['type'] == 'twopart':
joke = data['setup'] + ' ' + data['delivery']
else:
joke = data['joke']
if len(joke) <= 255:
jokes.append(joke)
add_jokes_to_csv(jokes)
# Function to get jokes from Geek Jokes API
def get_jokes_from_geek_jokes_api():
response = requests.get(geek_jokes_api_url)
joke = response.json()
print(joke)
if len(joke) <= 255:
jokes = [joke]
add_jokes_to_csv(jokes)
# Function to get jokes from Official Joke API
def get_jokes_from_official_joke_api():
response = requests.get(official_joke_api_url)
data = response.json()[0]
joke = data['setup'] + ' ' + data['punchline']
if len(joke) <= 255:
jokes = [joke]
add_jokes_to_csv(jokes)
"""
# Schedule a function to add new jokes to the CSV file every day at 12:00 PM
schedule.every().day.at("12:00").do(get_jokes_from_jokeapi)
schedule.every().day.at("12:00").do(get_jokes_from_geek_jokes_api)
schedule.every().day.at("12:00").do(get_jokes_from_official_joke_api)
# Function to tweet a random joke from the CSV file
def tweet_joke():
jokes = []
with open(csv_file, 'r') as f:
reader = csv.reader(f)
for row in reader:
jokes.append(row[0])
joke = random.choice(jokes)
if len(joke) <= 255:
api.update_status(joke)
else:
tweet_joke()
# Schedule a tweet to be sent every day at 12:00 PM
schedule.every().day.at("12:00").do(tweet_joke)
# Run the scheduler continuously
while True:
schedule.run_pending()
time.sleep(1)
"""
def main():
print("Jokes!")
get_jokes_from_jokeapi()
get_jokes_from_geek_jokes_api()
get_jokes_from_official_joke_api()
return print("Jokes!")
# Using the special variable
# __name__
if __name__=="__main__":
main()