-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtweetifymusic.py
59 lines (48 loc) · 1.79 KB
/
tweetifymusic.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
import os
import tweepy
import spotipy
from spotipy.oauth2 import SpotifyOAuth
# Spotify Part :
client_id = os.environ["SPOTIFY_CLIENT_ID"]
client_secret = os.environ["SPOTIFY_CLIENT_SECRET"]
refresh_token = os.environ["SPOTIFY_REFRESH_TOKEN"]
sp_oauth = SpotifyOAuth(
client_id=client_id,
client_secret=client_secret,
redirect_uri="http://127.0.0.1:8080/callback",
scope="user-read-recently-played",
cache_path="token.txt", # Path to store the token
)
# Use the refresh token to get a new access token
token_info = sp_oauth.refresh_access_token(refresh_token)
access_token = token_info["access_token"]
# Create a Spotipy client with the access token
sp = spotipy.Spotify(auth=access_token)
data = sp.current_user_recently_played(limit=1)
song_url = data["items"][0]["track"]["external_urls"]["spotify"]
song_name = data["items"][0]["track"]["name"]
# Twitter Part :
twitter_api_key = os.environ["TWITTER_API_KEY"]
twitter_api_secret = os.environ["TWITTER_API_SECRET"]
twitter_bearer_token = rf'{os.environ["TWITTER_BEARER_TOKEN"]}'
twitter_access_token = os.environ["TWITTER_ACCESS_TOKEN"]
twitter_access_token_secret = os.environ["TWITTER_ACCESS_TOKEN_SECRET"]
user = tweepy.Client(
bearer_token=twitter_bearer_token,
consumer_key=twitter_api_key,
consumer_secret=twitter_api_secret,
access_token=twitter_access_token,
access_token_secret=twitter_access_token_secret,
)
auth = tweepy.OAuth1UserHandler(
consumer_key=twitter_api_key,
consumer_secret=twitter_api_secret,
access_token=twitter_access_token,
access_token_secret=twitter_access_token_secret,
)
api = tweepy.API(auth)
try:
user.create_tweet(text=f"Song : {song_name}\n\n Tweeted by TweetifyMusic. {song_url}")
print("Tweet Completed.")
except tweepy.Forbidden:
print("Skipping Duplicate Tweet.")