-
Notifications
You must be signed in to change notification settings - Fork 7
/
publish_playlist_twitter.py
executable file
·101 lines (91 loc) · 2.87 KB
/
publish_playlist_twitter.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
#! /usr/bin/python -u
"""
Publish video playlist into Twitter.
"""
import list_youtuble_playlist as ytb
import twitter
import random
import curl
import tempfile
import os
import ConfigParser
import time
PLAYLIST = "PLQYPYhKQVTvcNqNnkEfEKhaMxUf2tn_CP" # PyConSE
HOMEDIR = os.environ.get("HOME")
configuration = "%s/.twitterc" % HOMEDIR
def ReadConfig():
"""
Configuration from file ~/.twitterc
"""
global cons_key, cons_sec, acc_key, acc_sec, wth_key, wth_loc
cfg = ConfigParser.ConfigParser()
print "Reading configuration: %s" % configuration
if not os.path.exists(configuration):
print "Failed to find configuration file %s" % configuration
sys.exit(1)
cfg.read(configuration)
cons_key = cfg.get("TWITTER", "CONS_KEY")
cons_sec = cfg.get("TWITTER", "CONS_SEC")
acc_key = cfg.get("TWITTER", "ACC_KEY")
acc_sec = cfg.get("TWITTER", "ACC_SEC")
wth_key = cfg.get("FORECAST.IO", "KEY")
wth_loc = cfg.get("FORECAST.IO", "LOCATION")
def GetVideo():
y = ytb.main(PLAYLIST)
x = os.urandom(int(time.time()/1000))
random.seed(x)
talk = random.choice(y.keys())
return { talk : y[talk] }
def GetImage(url, fd):
c = curl.Curl()
c.get(url)
img_write = os.fdopen(fd, 'w')
img_write.write(c.body())
img_write.flush()
img_write.close()
def TwitterPost(video_info):
# App python-tweeter
# https://dev.twitter.com/apps/815176
tw = twitter.Api(
consumer_key = cons_key,
consumer_secret = cons_sec,
access_token_key = acc_key,
access_token_secret = acc_sec
)
title = video_info.keys()[0]
print "Title: %s" % title
image = video_info[title]['thumb']
print "Image: %s" % image
url = video_info[title]['url']
print "Video: %s" % url
fd, file_image = tempfile.mkstemp()
GetImage(image, fd)
msg = "Watch again: %s\n%s #pyconse" % (title, url)
print "Posting: %s" % msg
print "Image: %s" % file_image
try:
tw.PostMedia(status = msg,media = file_image)
os.unlink(file_image)
except twitter.TwitterError as err:
# twitter.TwitterError: [{u'message': u'Status is a duplicate.', u'code': 187}]
print "ERROR: %s" % err.args[0][0]['message']
print "Post on Twitter failed. Trying again..."
if os.path.exists(file_image):
os.unlink(file_image)
video = GetVideo()
TwitterPost(video)
return
try:
msg = "Don't forget to send you talk proposal for #pyconse (September 6th in Stockholm). key=%d " % random.randint(1000,5000) + \
"https://goo.gl/RjHpoS"
print "Posting: %s" % msg
tw.PostUpdate(msg)
except twitter.TwitterError as err:
error = err.args[0][0]
print "ERROR: %s" % error['message']
def main():
video = GetVideo()
ReadConfig()
TwitterPost(video)
if __name__ == '__main__':
main()