-
Notifications
You must be signed in to change notification settings - Fork 0
/
twplaylist.py
135 lines (105 loc) · 3.94 KB
/
twplaylist.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#! /usr/bin/python -O
# Twitter Playlists
# Scans a user's latest tweets for a string + youtube video
# Adds any matching videos to a youtube playlist
from subprocess import check_call
import subprocess
import string
import random
import glob
from base64 import b64encode
import requests
from pprint import pprint
import time
import re
import threading
import sys
import ConfigParser
import httplib2
import tweepy
import argparse
from apiclient.discovery import build
from apiclient.http import BatchHttpRequest
from oauth2client.file import Storage
from oauth2client.client import AccessTokenRefreshError
from oauth2client.client import OAuth2WebServerFlow
from oauth2client.tools import run
from oauth2client import tools
# Set up config so we can get basic data
config = ConfigParser.ConfigParser()
config.read("twplaylist.cfg")
# Constants
## General
sleep = config.get("General", "sleep_time")
match = config.get("General", "match")
## Twitter
t_count = config.get("Twitter", "count")
t_user = config.get("Twitter", "user")
t_id = config.get("Twitter", "client_id")
t_secret = config.get("Twitter", "client_secret")
twitter = ''
## YouTube
ytcomReg = r"youtube.com\/watch\?v=(.{11})"
ytbeReg = r"youtu.be\/(.{11})"
yt_id = config.get("YouTube", "client_id")
yt_secret = config.get("YouTube", "client_secret")
playlist = config.get("YouTube", "playlist")
scope = 'https://www.googleapis.com/auth/youtube'
youtube = ''
def twplaylist():
flow = OAuth2WebServerFlow(yt_id, yt_secret, scope)
storage = Storage('credentials.dat')
parser = argparse.ArgumentParser(parents=[tools.argparser])
flags = parser.parse_args()
credentials = tools.run_flow(flow, storage, flags)
if credentials is None or credentials.invalid:
credentials = run(flow, storage)
global http
http = httplib2.Http()
http = credentials.authorize(http)
global youtube
youtube = build('youtube', 'v3', http=http)
global twitter
twitter = tweepy.API(tweepy.OAuthHandler(t_id, t_secret))
user = twitter.get_user(t_user)
last_id = ''
while True:
try:
ytlinks = []
if last_id:
print 'Grabbing ' + str(t_count) + ' latest tweets from ' + user.screen_name + ' since ' + str(last_id)
statuses = twitter.user_timeline(id=t_user, since_id=int(last_id), count=t_count)
else:
print 'Grabbing ' + str(t_count) + ' latest tweets from ' + user.screen_name
statuses = twitter.user_timeline(id=t_user, count=t_count)
if len(statuses) > 0:
last_id = statuses[0].id
for status in statuses:
if (match in status.text.lower()) and (status.entities.has_key('urls')):
url = status.entities.get('urls')[0].get('expanded_url')
ytcomlink = re.findall(ytcomReg, url)
ytbelink = re.findall(ytbeReg, url)
if ytbelink != []: ytlinks += ytbelink
if ytcomlink != []: ytlinks += ytcomlink
print 'Adding ' + str(len(ytlinks)) + ' video(s)'
for video in ytlinks:
insert = youtube.playlistItems().insert(
part="snippet",
body=dict(
snippet=dict(
playlistId=playlist,
resourceId=dict(
kind="youtube#video",
videoId=video
)
))
)
response = insert.execute()
else: print 'No new tweets since last attempt'
print 'Waiting ' + str(sleep) + ' seconds'
time.sleep(int(sleep))
except Exception, e:
print e
exit()
main_thread = threading.Thread(target=twplaylist)
main_thread.start()