Advertisement
Guest User

Untitled

a guest
Jan 6th, 2018
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.47 KB | None | 0 0
  1. #!/usr/bin/python
  2.  
  3. import praw
  4. import time
  5. import httplib2
  6. import os
  7. import sys
  8.  
  9. from apiclient.discovery import build
  10. from apiclient.errors import HttpError
  11. from oauth2client.client import flow_from_clientsecrets
  12. from oauth2client.file import Storage
  13. from oauth2client.tools import argparser, run_flow
  14.  
  15. CLIENT_SECRETS_FILE = "client_secrets.json"
  16.  
  17. # This variable defines a message to display if the CLIENT_SECRETS_FILE is
  18. # missing.
  19. MISSING_CLIENT_SECRETS_MESSAGE = """
  20. WARNING: Please configure OAuth 2.0
  21.  
  22. To make this sample run you will need to populate the client_secrets.json file
  23. found at:
  24.  
  25. %s
  26.  
  27. with information from the {{ Cloud Console }}
  28. {{ https://cloud.google.com/console }}
  29.  
  30. For more information about the client_secrets.json file format, please visit:
  31. https://developers.google.com/api-client-library/python/guide/aaa_client_secrets
  32. """ % os.path.abspath(os.path.join(os.path.dirname(__file__),
  33. CLIENT_SECRETS_FILE))
  34.  
  35. # This OAuth 2.0 access scope allows for full read/write access to the
  36. # authenticated user's account.
  37. YOUTUBE_READ_WRITE_SCOPE = "https://www.googleapis.com/auth/youtube"
  38. YOUTUBE_API_SERVICE_NAME = "youtube"
  39. YOUTUBE_API_VERSION = "v3"
  40.  
  41. flow = flow_from_clientsecrets(
  42. CLIENT_SECRETS_FILE,
  43. message=MISSING_CLIENT_SECRETS_MESSAGE,
  44. scope=YOUTUBE_READ_WRITE_SCOPE
  45. )
  46.  
  47. storage = Storage("%s-oauth2.json" % sys.argv[0])
  48. credentials = storage.get()
  49.  
  50. if credentials is None or credentials.invalid:
  51. flags = argparser.parse_args()
  52. credentials = run_flow(flow, storage, flags)
  53.  
  54. youtube = build(YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION,
  55. http=credentials.authorize(httplib2.Http()))
  56. print("Connected to YouTube!")
  57.  
  58. reddit = praw.Reddit(client_id='ojxVhSfPheoQyQ',
  59. client_secret='secret',
  60. user_agent='u/balloon_wanted bot 1.0',
  61. password='password',
  62. username='balloon_wanted')
  63. print("Connected to Reddit!")
  64.  
  65. SeenVideos = []
  66.  
  67. channelResult = youtube.channels().list(
  68. part='contentDetails',
  69. id='channelid'
  70. ).execute()
  71.  
  72. playlistId = channelResult['items'][0]['contentDetails']['relatedPlaylists']['uploads']
  73.  
  74. while True:
  75. playlistResult = youtube.playlistItems().list(
  76. part='snippet',
  77. maxResults=2,
  78. playlistId=playlistId
  79. ).execute()
  80.  
  81. print('Playlist has {} visible videos'.format(playlistResult['pageInfo']['totalResults']))
  82.  
  83. print('Got {} uploads for channel id: {}'.format(len(playlistResult['items']), playlistId))
  84.  
  85. if len(SeenVideos) == 0:
  86. for video in playlistResult['items']:
  87. videoLink ='https://www.youtube.com/watch?v=' + video['snippet']['resourceId']['videoId']
  88. SeenVideos.append(videoLink)
  89. print('Initialized seen videos with {} videos'.format(len(SeenVideos)))
  90. else:
  91. if len(playlistResult['items']) == 2:
  92. for video in playlistResult['items']:
  93. videoTitle = video['snippet']['title']
  94. videoLink = 'https://www.youtube.com/watch?v=' + video['snippet']['resourceId']['videoId']
  95. if videoLink not in SeenVideos:
  96. print('Found new video: \'{}\' link: {}'.format(videoTitle, videoLink))
  97. reddit.subreddit('balloon_wanted').submit(videoTitle, url=videoLink, resubmit = True)
  98. print('Posted to reddit')
  99. SeenVideos.append(videoLink)
  100. exit() # EXIT
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement