Michael Benayoun commited on
Commit
c2c6fc6
1 Parent(s): fb24c91

Add answer to the poll

Browse files
Files changed (1) hide show
  1. app.py +15 -20
app.py CHANGED
@@ -2,7 +2,6 @@ import os
2
  import string
3
  import random
4
 
5
- # import tweepy
6
  import time
7
  import json
8
  import requests
@@ -17,14 +16,6 @@ CONSUMER_SECRET = os.environ.get("CONSUMER_SECRET")
17
  ACCESS_TOKEN = os.environ.get("ACCESS_TOKEN")
18
  ACCESS_TOKEN_SECRET = os.environ.get("ACCESS_TOKEN_SECRET")
19
 
20
- # client = tweepy.Client(
21
- # bearer_token=BEARER_TOKEN,
22
- # consumer_key=CONSUMER_KEY,
23
- # consumer_secret=CONSUMER_SECRET,
24
- # access_token=ACCESS_TOKEN,
25
- # access_token_secret=ACCESS_TOKEN_SECRET,
26
- # )
27
-
28
 
29
  # Real tweets
30
 
@@ -46,10 +37,6 @@ def load_fake_tweet():
46
  index = random.randint(0, len(fake_tweets))
47
  return fake_tweets[index]
48
 
49
- # response = client.create_tweet(
50
- # text=load_real_tweet()
51
- # )
52
-
53
 
54
  def connect_to_endpoint(url, params=None, data=None, type='GET'):
55
  resp = None
@@ -65,28 +52,36 @@ def connect_to_endpoint(url, params=None, data=None, type='GET'):
65
  return resp.json()
66
 
67
 
 
 
 
 
 
 
 
68
 
69
  def create_poll():
70
- ''' Creates a Twitter poll using the given question and options '''
71
- url = 'https://api.twitter.com/2/tweets'
72
  real_or_fake = random.randint(0, 2)
73
  function = load_real_tweet if real_or_fake > 0 else load_fake_tweet
74
  tweet = function()
75
  data = {
76
- 'text': tweet,
77
- 'poll': {
78
- 'options': [
79
  "Elon",
80
  "Not",
81
  ],
82
- 'duration_minutes': 24 * 60
83
  }
84
  }
85
  connect_to_endpoint(url, data=data, type='POST')
 
86
 
87
 
88
 
89
  if __name__ == "__main__":
90
  while True:
91
- create_poll()
92
  time.sleep(24 * 60 * 60)
 
 
2
  import string
3
  import random
4
 
 
5
  import time
6
  import json
7
  import requests
 
16
  ACCESS_TOKEN = os.environ.get("ACCESS_TOKEN")
17
  ACCESS_TOKEN_SECRET = os.environ.get("ACCESS_TOKEN_SECRET")
18
 
 
 
 
 
 
 
 
 
19
 
20
  # Real tweets
21
 
 
37
  index = random.randint(0, len(fake_tweets))
38
  return fake_tweets[index]
39
 
 
 
 
 
40
 
41
  def connect_to_endpoint(url, params=None, data=None, type='GET'):
42
  resp = None
 
52
  return resp.json()
53
 
54
 
55
+ def tweet(content):
56
+ url = "https://api.twitter.com/2/tweets"
57
+ data = {
58
+ "text": content,
59
+ }
60
+ connect_to_endpoint(url, data=data, type='POST')
61
+
62
 
63
  def create_poll():
64
+ url = "https://api.twitter.com/2/tweets"
 
65
  real_or_fake = random.randint(0, 2)
66
  function = load_real_tweet if real_or_fake > 0 else load_fake_tweet
67
  tweet = function()
68
  data = {
69
+ "text": tweet,
70
+ "poll": {
71
+ "options": [
72
  "Elon",
73
  "Not",
74
  ],
75
+ "duration_minutes": 24 * 60
76
  }
77
  }
78
  connect_to_endpoint(url, data=data, type='POST')
79
+ return "Elon" if real_or_fake > 0 else "Not"
80
 
81
 
82
 
83
  if __name__ == "__main__":
84
  while True:
85
+ answer = create_poll()
86
  time.sleep(24 * 60 * 60)
87
+ tweet(f"The answer is: {answer}, contgratulations if you guessed it right!")