├── .gitignore ├── LICENSE ├── groompbot.py ├── readme.md ├── requirements.txt └── settings.json.default /.gitignore: -------------------------------------------------------------------------------- 1 | # Python 2 | *.py[cod] 3 | *.so 4 | *.egg 5 | *.egg-info 6 | dist 7 | build 8 | eggs 9 | parts 10 | bin 11 | var 12 | sdist 13 | develop-eggs 14 | .installed.cfg 15 | lib 16 | lib64 17 | 18 | # Project specific 19 | settings.json 20 | lastupload.txt 21 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2012 AndrewNeo 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 4 | 5 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 6 | 7 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 8 | -------------------------------------------------------------------------------- /groompbot.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # Posts YouTube videos from a channel to a subreddit 3 | 4 | # Imports 5 | import sys 6 | import logging 7 | import json 8 | import praw 9 | import gdata.youtube.service 10 | 11 | # YouTube functions 12 | def getUserUploads(username): 13 | """Get YouTube uploads by username.""" 14 | yt_service = gdata.youtube.service.YouTubeService() 15 | uri = "http://gdata.youtube.com/feeds/api/users/%s/uploads" % username 16 | return yt_service.GetYouTubeVideoFeed(uri) 17 | 18 | def getVideoIdFromEntry(entry): 19 | """Get video ID from a YouTube entry.""" 20 | return entry.id.text.split("/")[-1] 21 | 22 | # Reddit functions 23 | def getReddit(settings): 24 | """Get a reference to Reddit.""" 25 | r = praw.Reddit(user_agent=settings["reddit_ua"]) 26 | try: 27 | r.login(settings["reddit_username"], settings["reddit_password"]) 28 | except: 29 | logging.exception("Error logging into Reddit.") 30 | exitApp() 31 | return r 32 | 33 | def getSubreddit(settings, reddit): 34 | """Get the subreddit.""" 35 | return reddit.get_subreddit(settings["reddit_subreddit"]) 36 | 37 | def submitContent(subreddit, title, link): 38 | """Submit a link to a subreddit.""" 39 | logging.info("Submitting %s (%s)", (title, link)) 40 | try: 41 | subreddit.submit(title, url=link) 42 | except praw.errors.APIException: 43 | logging.exception("Error on link submission.") 44 | 45 | def getPastVideos(subreddit): 46 | """Get all YouTube videos posted in the past hour.""" 47 | hour = subreddit.get_new_by_date() 48 | for video in hour: 49 | if ("youtube" in video.url): 50 | yield video.url 51 | 52 | # Main functions 53 | def takeAndSubmit(settings, subreddit, feed): 54 | """Iterate through each YouTube feed entry and submit to Reddit.""" 55 | pastVideos = [] 56 | if (settings["repost_protection"]): 57 | pastVideos = list(getPastVideos(subreddit)) 58 | 59 | for entry in feed: 60 | title = unicode(entry.title.text, "utf-8") 61 | url = entry.media.player.url 62 | videoid = getVideoIdFromEntry(entry) 63 | logging.debug("Video: %s (%s)", (title, url)) 64 | 65 | # Check if someone else already uploaded it 66 | for post in pastVideos: 67 | if (videoid in post): 68 | logging.debug("Video found in past video list.") 69 | break 70 | else: 71 | submitContent(subreddit, title, url) 72 | 73 | def loadSettings(): 74 | """Load settings from file.""" 75 | try: 76 | settingsFile = open("settings.json", "r") 77 | except IOError: 78 | logging.exception("Error opening settings.json.") 79 | exitApp() 80 | 81 | settingStr = settingsFile.read() 82 | settingsFile.close() 83 | 84 | try: 85 | settings = json.loads(settingStr) 86 | except ValueError: 87 | logging.exception("Error parsing settings.json.") 88 | exitApp() 89 | 90 | # Check integrity 91 | if (len(settings["reddit_username"]) == 0): 92 | logging.critical("Reddit username not set.") 93 | exitApp() 94 | 95 | if (len(settings["reddit_password"]) == 0): 96 | logging.critical("Reddit password not set.") 97 | exitApp() 98 | 99 | if (len(settings["reddit_subreddit"]) == 0): 100 | logging.critical("Subreddit not set.") 101 | exitApp() 102 | 103 | if (len(settings["reddit_ua"]) == 0): 104 | logging.critical("Reddit bot user agent not set.") 105 | exitApp() 106 | 107 | if (len(settings["youtube_account"]) == 0): 108 | logging.critical("YouTube account not set.") 109 | exitApp() 110 | 111 | settings["repost_protection"] = bool(settings["repost_protection"]) 112 | 113 | # Get last upload position 114 | try: 115 | lastUploadFile = open("lastupload.txt", "r") 116 | lastUpload = lastUploadFile.read() 117 | lastUploadFile.close() 118 | 119 | settings["youtube_lastupload"] = lastUpload 120 | except IOError: 121 | logging.info("No last uploaded video found.") 122 | settings["youtube_lastupload"] = None 123 | 124 | return settings 125 | 126 | def savePosition(position): 127 | """Write last position to file.""" 128 | lastUploadFile = open("lastupload.txt", "w") 129 | lastUploadFile.write(position) 130 | lastUploadFile.close() 131 | 132 | def exitApp(): 133 | sys.exit(1) 134 | 135 | def runBot(): 136 | """Start a run of the bot.""" 137 | logging.info("Starting bot.") 138 | settings = loadSettings() 139 | 140 | logging.info("Getting YouTube videos.") 141 | 142 | # Download video list 143 | uploads = getUserUploads(settings["youtube_account"]).entry 144 | newestUpload = uploads[0] 145 | 146 | # Reverse from new to old, to old to new 147 | uploads.reverse() 148 | 149 | # Only get new uploads 150 | try: 151 | videoIdList = map(getVideoIdFromEntry, uploads) 152 | indexOfLastUpload = videoIdList.index(settings["youtube_lastupload"]) 153 | uploads = uploads[indexOfLastUpload + 1:] 154 | if (len(uploads) == 0): 155 | logging.info("No new uploads since last run.") 156 | exitApp() 157 | except ValueError: 158 | # Ignore a failure if lastupload value isn't in list 159 | pass 160 | 161 | # Get reddit stuff 162 | logging.info("Logging into Reddit.") 163 | reddit = getReddit(settings) 164 | sr = getSubreddit(settings, reddit) 165 | 166 | # Submit entries 167 | logging.info("Submitting to Reddit.") 168 | takeAndSubmit(settings, sr, uploads) 169 | 170 | # Save newest position 171 | logging.info("Saving position.") 172 | videoid = getVideoIdFromEntry(newestUpload) 173 | savePosition(videoid) 174 | 175 | logging.info("Done!") 176 | 177 | if __name__ == "__main__": 178 | logging.basicConfig() 179 | 180 | try: 181 | runBot() 182 | except SystemExit: 183 | logging.info("Exit called.") 184 | except: 185 | logging.exception("Uncaught exception.") 186 | 187 | logging.shutdown() 188 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | Groompbot 2 | ========= 3 | 4 | [Groompbot](https://github.com/AndrewNeo/groompbot): A bot that posts new videos from YouTube to a subreddit 5 | 6 | Built by [AndrewNeo](http://www.reddit.com/u/AndrewNeo) to post [Game Grumps](http://www.youtube.com/gamegrumps) videos to the [/r/gamegrumps](http://www.reddit.com/r/gamegrumps) subreddit. It should work fine for other YouTube channels and subreddits. 7 | 8 | Usage 9 | ----- 10 | 11 | It's set up to be run by a cronfile, probably every minute or so at most. 12 | 13 | Configuration 14 | ------------- 15 | 16 | Configuration for the bot is set up in the settings.json file. The configuration file (which should be copied from settings.json.default) should contain the reddit bot username, password, the subreddit to post to, an approperate useragent, and the YouTube account to read from. 17 | 18 | Dependencies 19 | ------------ 20 | 21 | Groompbot depends on the following external libraries: 22 | 23 | * [praw](https://github.com/praw-dev/praw/) - Reddit library 24 | * [gdata](http://code.google.com/p/gdata-python-client/) - Google Data API 25 | 26 | License 27 | ------- 28 | 29 | Groompbot is free to use under the MIT License. 30 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | gdata==2.0.17 2 | praw==1.0.12 3 | -------------------------------------------------------------------------------- /settings.json.default: -------------------------------------------------------------------------------- 1 | { 2 | "reddit_username": "", 3 | "reddit_password": "", 4 | "reddit_subreddit": "", 5 | "reddit_ua": "", 6 | "youtube_account": "", 7 | "repost_protection": true 8 | } 9 | --------------------------------------------------------------------------------