-
Notifications
You must be signed in to change notification settings - Fork 0
/
mpd-tweet.py
executable file
·54 lines (44 loc) · 1.72 KB
/
mpd-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
#! /usr/bin/env python
App_Name = "MPD-Tweet"
Consumer_key = "n9gj3P5Ug3bhnNNF4p3ow"
Consumer_secret = "7FIAaTZQAFcNboWXVUUlTffxzbDVEY7iwXw67fyd7Q"
Config_file="config"
MPDHost = 'localhost'
MPDPort = '6600'
from lib.oauth_dance import oauth_dance
from lib.oauthtwitter import OAuthApi
from lib.oauth import read_token_file
from mpd import MPDClient, CommandError, MPDError
from socket import error as SocketError
class Tweet:
def __init__(self):
self.generate_config()
api = self.get_twitter_access()
mpdclient = self.connect_to_mpd()
currentSong = mpdclient.currentsong()
self.tweet_now_playing(currentSong, api)
def generate_config(self):
try:
f = open(Config_file)
except IOError:
oauth_dance(App_Name, Consumer_key, Consumer_secret, Config_file)
def get_twitter_access(self):
token = read_token_file(Config_file)
api = OAuthApi(Consumer_key, Consumer_secret, token[0], token[1])
return api
def connect_to_mpd(self):
client=MPDClient()
try:
client.connect(MPDHost, MPDPort)
return client
except SocketError:
print "Unable to connect to MPD on %s on port %s" % (MPDHost, MPDPort)
def tweet_now_playing(self, currentSong, api):
try:
status = ("Listening to \"%s\" by \"%s\" from the album \"%s\" #nowplaying" ) % (currentSong['title'], currentSong['artist'], currentSong['album'])
except KeyError:
status = ("Listening to %s #nowplaying" ) % (currentSong['title'] )
print status
api.UpdateStatus(status)
if __name__ == "__main__":
Tweet = Tweet()