I have looked up some things about Tweepy but am unsure of exactly how it is used. Essentially I am using this code to generate plots and messages for a game. I would like to then use Tweepy to send the outputted Pie charts and texts via a Twitter dm. Any idea how I would do this?
import numpy as np
import random
from matplotlib import pyplot as plt
class Wheelspin:
def __init__(self):
self.odds = float(input("Enter the odds (is win:loss = 1:x) enter x:\n")) #gets initial odds from player
self.W_L_messages = ["You did not win.", "You have won!"] #predetermined win/loss messages
self.plot_colors = ["r", "b"] #determines colors for pie charts
self.plot_labels = ["Lose", "Win"] #determines labels for pie charts
self.several_wins = False #determines is player can spin again after winning
self.spin() #initiate game
def odds_adjustment(self, result): #determines how odds should be adjusted
if result == 1:
self.odds += 15
if result == 0:
self.odds -= 1
def spin(self):
print("The current odds are shown in the Pie chart:")
def pct_func(pct, allvalues):
absolute = int(pct / 100.*np.sum(allvalues))
return "{:.1f}%".format(pct, absolute)
plt.pie([self.odds, 1], labels = self.plot_labels, colors = self.plot_colors, autopct = lambda pct: pct_func(pct, [1, self.odds]))
plt.show()
spin_again = str(input("Would you like to spin: Y or N?\n")) #ask player if they'd like to spin again
if spin_again == "Y":
result = random.choices([1, 0], weights = [1, self.odds], k = 1)[0] #0 indicates a loss and 1 a win (odds are 1:x for win:lose)
print(self.W_L_messages[result]) #deliver win/loss message
plt.pie([1, 0], labels = [self.plot_labels[result], ""], colors = [self.plot_colors[result], "g"])
plt.show()
self.odds_adjustment(result = result) #adjust odds
if result == 0 or (self.several_wins and result == 1): #conditions for spinning again
self.spin()
else:
print(self.odds)
if spin_again == "N":
print("Spins are over. Current odds are 1:{}".format(int(self.odds)))
game = Wheelspin()
I don't have any experience using Tweepy however I did come accross this post: How do I tweet media using Tweepy and get requests in Python? I am wondering how I can use a similar idea to upload the plots and messages via a dm each time the game is played but I'm not sure where to start. The difference here is the plot is not a saved image on my device but rather just an output of the code, I'd like to be able to send it without first saving it. Thanks.