<s> import atexit <EOL> import getopt <EOL> import json <EOL> import logging <EOL> import os <EOL> import sys <EOL> import time <EOL> import datetime <EOL> import praw <EOL> import praw . helpers <EOL> import signal <EOL> from imgurpython import ImgurClient <EOL> from utils import log , Color , retrieve_vine_video_url , gfycat_convert , get_id , get_gfycat_info , offsided_convert , imgur_upload , get_offsided_info , notify_mac , retrieve_vine_cdn_url , get_streamable_info , streamable_convert , get_remote_file_size <EOL> __author__ = '<STR_LIT>' <EOL> cache_file = "<STR_LIT>" <EOL> propsFile = "<STR_LIT>" <EOL> running_on_heroku = False <EOL> dry_run = False <EOL> notify = False <EOL> bot_name = "<STR_LIT>" <EOL> allowedDomains = [ <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" <EOL> ] <EOL> allowed_extensions = [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] <EOL> disabled_extensions = [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] <EOL> approved_subs = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> comment_intro = """<STR_LIT>""" <EOL> comment_info = """<STR_LIT>""" <EOL> vine_warning = """<STR_LIT>""" <EOL> class MirroredObject : <EOL> op_id = "<STR_LIT>" <EOL> original_url = "<STR_LIT>" <EOL> gfycat_url = "<STR_LIT>" <EOL> offsided_url = "<STR_LIT>" <EOL> imgur_url = "<STR_LIT>" <EOL> streamable_url = "<STR_LIT>" <EOL> def __init__ ( self , op_id , original_url , json_data = None ) : <EOL> if json_data : <EOL> self . __dict__ = json . loads ( json_data ) <EOL> else : <EOL> self . op_id = op_id <EOL> self . original_url = original_url <EOL> def comment_string ( self , domain ) : <EOL> s = "<STR_LIT:\n>" <EOL> if self . original_url : <EOL> if "<STR_LIT>" in self . original_url : <EOL> s += vine_warning <EOL> s += "<STR_LIT>" % ( domain , self . original_url ) <EOL> if self . gfycat_url : <EOL> gfy_id = get_id ( self . gfycat_url ) <EOL> urls = self . gfycat_urls ( gfy_id ) <EOL> s += "<STR_LIT>" <EOL> s += "<STR_LIT>" % ( <EOL> self . gfycat_url , urls [ <NUM_LIT:0> ] , urls [ <NUM_LIT:1> ] , urls [ <NUM_LIT:2> ] ) <EOL> if self . offsided_url : <EOL> s += "<STR_LIT>" <EOL> s += "<STR_LIT>" % self . offsided_url <EOL> for mediaType , url in self . offsided_urls ( get_id ( self . offsided_url ) ) : <EOL> s += "<STR_LIT>" % ( mediaType , url ) <EOL> s = s [ <NUM_LIT:0> : - <NUM_LIT:2> ] <EOL> if self . imgur_url : <EOL> s += "<STR_LIT>" <EOL> s += "<STR_LIT>" % self . imgur_url <EOL> for mediaType , url in self . imgur_urls ( get_id ( self . imgur_url ) ) : <EOL> s += "<STR_LIT>" % ( mediaType , url ) <EOL> s = s [ <NUM_LIT:0> : - <NUM_LIT:2> ] <EOL> if self . streamable_url : <EOL> s += "<STR_LIT>" <EOL> s += "<STR_LIT>" % self . streamable_url <EOL> for mediaType , url in self . streamable_urls ( get_id ( self . streamable_url ) ) : <EOL> s += "<STR_LIT>" % ( mediaType , url ) <EOL> s = s [ <NUM_LIT:0> : - <NUM_LIT:2> ] <EOL> s += "<STR_LIT:\n>" <EOL> return s <EOL> def to_json ( self ) : <EOL> return json . dumps ( self . __dict__ ) <EOL> @ staticmethod <EOL> def gfycat_urls ( gfy_id ) : <EOL> info = get_gfycat_info ( gfy_id ) <EOL> return info [ '<STR_LIT>' ] , info [ '<STR_LIT>' ] , info [ '<STR_LIT>' ] <EOL> @ staticmethod <EOL> def offsided_urls ( fit_id ) : <EOL> info = get_offsided_info ( fit_id ) <EOL> return [ [ x , info [ x ] ] for x in ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) if info [ x ] ] <EOL> @ staticmethod <EOL> def streamable_urls ( s_id ) : <EOL> info = get_streamable_info ( s_id ) <EOL> return [ [ x , "<STR_LIT>" + info [ "<STR_LIT>" ] [ x ] [ "<STR_LIT:url>" ] ] for x in info [ "<STR_LIT>" ] ] <EOL> @ staticmethod <EOL> def imgur_urls ( s_id ) : <EOL> info = imgur_client . get_image ( s_id ) <EOL> imgur_info = [ ] <EOL> if info . __dict__ [ "<STR_LIT>" ] : <EOL> imgur_info . append ( [ "<STR_LIT>" , info . mp4 ] ) <EOL> if info . __dict__ [ "<STR_LIT>" ] : <EOL> imgur_info . append ( [ "<STR_LIT>" , info . webm ] ) <EOL> if extension ( info . link ) == "<STR_LIT>" : <EOL> imgur_info . append ( [ "<STR_LIT>" , info . link ] ) <EOL> return imgur_info <EOL> def exit_handler ( ) : <EOL> log ( "<STR_LIT>" , Color . BOLD ) <EOL> def signal_handler ( input_signal , frame ) : <EOL> log ( '<STR_LIT>' , Color . RED ) <EOL> sys . exit ( ) <EOL> def exit_bot ( ) : <EOL> sys . exit ( ) <EOL> def retrieve_login_credentials ( ) : <EOL> if running_on_heroku : <EOL> login_info = [ os . environ [ '<STR_LIT>' ] , <EOL> os . environ [ '<STR_LIT>' ] , <EOL> os . environ [ '<STR_LIT>' ] , <EOL> os . environ [ '<STR_LIT>' ] , <EOL> os . environ [ '<STR_LIT>' ] <EOL> ] <EOL> return login_info <EOL> else : <EOL> with open ( "<STR_LIT>" , "<STR_LIT:r>" ) as loginFile : <EOL> login_info = json . loads ( loginFile . read ( ) ) <EOL> login_info [ <NUM_LIT:0> ] = login_info [ "<STR_LIT>" ] <EOL> login_info [ <NUM_LIT:1> ] = login_info [ "<STR_LIT>" ] <EOL> login_info [ <NUM_LIT:2> ] = login_info [ "<STR_LIT>" ] <EOL> login_info [ <NUM_LIT:3> ] = login_info [ "<STR_LIT>" ] <EOL> login_info [ <NUM_LIT:4> ] = login_info [ "<STR_LIT>" ] <EOL> return login_info <EOL> def extension ( url_to_split ) : <EOL> return os . path . splitext ( url_to_split ) [ <NUM_LIT:1> ] <EOL> def previously_commented ( submission ) : <EOL> flat_comments = praw . helpers . flatten_tree ( submission . comments ) <EOL> for comment in flat_comments : <EOL> try : <EOL> if comment . author . name == bot_name : <EOL> log ( "<STR_LIT>" ) <EOL> return True <EOL> except : <EOL> return False <EOL> return False <EOL> def submission_is_valid ( submission ) : <EOL> ext = extension ( submission . url ) <EOL> if ( submission . domain in allowedDomains and ext not in disabled_extensions ) or ext in allowed_extensions : <EOL> if previously_commented ( submission ) : <EOL> return False , True <EOL> else : <EOL> return True , False <EOL> return False , False <EOL> def process_submission ( submission ) : <EOL> new_mirror = MirroredObject ( submission . id , submission . url ) <EOL> already_gfycat = False <EOL> url_to_process = submission . url <EOL> if submission . domain == "<STR_LIT>" : <EOL> url_to_process = retrieve_vine_video_url ( url_to_process ) <EOL> elif submission . domain == "<STR_LIT>" : <EOL> url_to_process = retrieve_vine_cdn_url ( url_to_process ) <EOL> elif submission . domain == "<STR_LIT>" : <EOL> already_gfycat = True <EOL> new_mirror . gfycat_url = url_to_process <EOL> url_to_process = get_gfycat_info ( get_id ( url_to_process ) ) [ '<STR_LIT>' ] <EOL> elif submission . domain == "<STR_LIT>" : <EOL> new_mirror . offsided_url = url_to_process <EOL> url_to_process = get_offsided_info ( get_id ( url_to_process ) ) [ '<STR_LIT>' ] <EOL> elif submission . domain == "<STR_LIT>" : <EOL> new_mirror . streamable_url = url_to_process <EOL> url_to_process = "<STR_LIT>" % get_streamable_info ( get_id ( url_to_process ) ) [ "<STR_LIT>" ] <EOL> if not url_to_process . startswith ( '<STR_LIT>' ) : <EOL> url_to_process = '<STR_LIT>' + url_to_process <EOL> elif submission . domain == "<STR_LIT>" or submission . domain == "<STR_LIT>" : <EOL> new_mirror . imgur_url = url_to_process <EOL> imgur_data = imgur_client . get_image ( get_id ( url_to_process ) ) <EOL> if extension ( url_to_process ) == "<STR_LIT>" : <EOL> url_to_process = imgur_data . link <EOL> elif "<STR_LIT>" in imgur_data . __dict__ : <EOL> url_to_process = imgur_data . __dict__ [ "<STR_LIT>" ] <EOL> else : <EOL> return <EOL> if submission . domain == "<STR_LIT>" : <EOL> url_to_process = url_to_process . replace ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> new_mirror . gfycat_url = url_to_process <EOL> already_gfycat = True <EOL> log ( "<STR_LIT>" + url_to_process ) <EOL> if not already_gfycat : <EOL> gfy_url = gfycat_convert ( url_to_process ) <EOL> if gfy_url : <EOL> new_mirror . gfycat_url = gfy_url <EOL> log ( "<STR_LIT>" + new_mirror . gfycat_url ) <EOL> remote_size = get_remote_file_size ( url_to_process ) <EOL> if submission . domain != "<STR_LIT>" : <EOL> fitba_url = offsided_convert ( submission . title , url_to_process ) <EOL> if fitba_url : <EOL> new_mirror . offsided_url = fitba_url <EOL> log ( "<STR_LIT>" + new_mirror . offsided_url ) <EOL> if submission . domain != "<STR_LIT>" : <EOL> new_mirror . streamable_url = streamable_convert ( url_to_process , retrieve_login_credentials ( ) [ <NUM_LIT:2> ] ) <EOL> log ( "<STR_LIT>" + new_mirror . streamable_url ) <EOL> comment_string = comment_intro + new_mirror . comment_string ( submission . domain ) + comment_info <EOL> add_comment ( submission , comment_string ) <EOL> if not already_gfycat : <EOL> log ( '<STR_LIT>' , Color . CYAN ) <EOL> time . sleep ( <NUM_LIT> ) <EOL> def add_comment ( submission , comment_string ) : <EOL> log ( "<STR_LIT>" , Color . BLUE ) <EOL> if dry_run : <EOL> log ( "<STR_LIT>" , Color . BLUE ) <EOL> log ( comment_string , Color . GREEN ) <EOL> return <EOL> try : <EOL> submission . add_comment ( comment_string ) <EOL> except praw . errors . RateLimitExceeded : <EOL> log ( "<STR_LIT>" , Color . RED ) <EOL> except praw . errors . APIException : <EOL> log ( '<STR_LIT>' , Color . RED ) <EOL> logging . exception ( "<STR_LIT>" ) <EOL> def bot ( ) : <EOL> for sub in approved_subs : <EOL> log ( "<STR_LIT>" + sub , Color . BLUE ) <EOL> now = datetime . datetime . utcnow ( ) <EOL> now_minus_10 = now + datetime . timedelta ( minutes = - <NUM_LIT:10> ) <EOL> float_now_minus_10 = time . mktime ( now_minus_10 . timetuple ( ) ) <EOL> subreddit = r . get_subreddit ( sub ) <EOL> submissions = [ p for p in subreddit . get_new ( limit = <NUM_LIT:200> ) if p . created_utc > float_now_minus_10 ] <EOL> for submission in sorted ( submissions , key = lambda p : p . created_utc ) : <EOL> is_valid , has_commented = submission_is_valid ( submission ) <EOL> log ( "<STR_LIT>" + submission . title ) <EOL> if is_valid : <EOL> log ( "<STR_LIT>" + submission . subreddit . display_name + "<STR_LIT>" + submission . url , Color . GREEN ) <EOL> process_submission ( submission ) <EOL> if dry_run : <EOL> sys . exit ( "<STR_LIT>" ) <EOL> elif has_commented : <EOL> return <EOL> else : <EOL> continue <EOL> if len ( submissions ) == <NUM_LIT:0> : <EOL> log ( "<STR_LIT>" , Color . BLUE ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> try : <EOL> opts , args = getopt . getopt ( sys . argv [ <NUM_LIT:1> : ] , "<STR_LIT>" , [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] ) <EOL> except getopt . GetoptError : <EOL> print ( '<STR_LIT>' ) <EOL> sys . exit ( <NUM_LIT:2> ) <EOL> if os . environ . get ( '<STR_LIT>' , None ) : <EOL> running_on_heroku = True <EOL> if len ( opts ) != <NUM_LIT:0> : <EOL> for o , a in opts : <EOL> if o in ( "<STR_LIT>" , "<STR_LIT>" ) : <EOL> dry_run = True <EOL> elif o in ( "<STR_LIT>" , "<STR_LIT>" ) : <EOL> notify = True <EOL> else : <EOL> sys . exit ( '<STR_LIT>' ) <EOL> atexit . register ( exit_handler ) <EOL> signal . signal ( signal . SIGINT , signal_handler ) <EOL> log ( "<STR_LIT>" , Color . BOLD ) <EOL> log ( "<STR_LIT>" + sys . platform , Color . BOLD ) <EOL> log ( "<STR_LIT>" + datetime . datetime . fromtimestamp ( <EOL> time . time ( ) - <NUM_LIT> ) . strftime ( '<STR_LIT>' ) , Color . BOLD ) <EOL> args = sys . argv <EOL> loginType = "<STR_LIT>" <EOL> r = praw . Reddit ( user_agent = '<STR_LIT>' ) <EOL> log ( "<STR_LIT>" , Color . BOLD ) <EOL> loginInfo = retrieve_login_credentials ( ) <EOL> try : <EOL> r . login ( loginInfo [ <NUM_LIT:0> ] , loginInfo [ <NUM_LIT:1> ] , disable_warning = True ) <EOL> log ( "<STR_LIT>" , Color . GREEN ) <EOL> except praw . errors : <EOL> log ( "<STR_LIT>" , Color . RED ) <EOL> exit_bot ( ) <EOL> imgur_client = ImgurClient ( loginInfo [ <NUM_LIT:3> ] , loginInfo [ <NUM_LIT:4> ] ) <EOL> counter = <NUM_LIT:0> <EOL> if running_on_heroku : <EOL> log ( "<STR_LIT>" , Color . BOLD ) <EOL> bot ( ) <EOL> else : <EOL> log ( "<STR_LIT>" , Color . BOLD ) <EOL> while True : <EOL> bot ( ) <EOL> counter += <NUM_LIT:1> <EOL> log ( '<STR_LIT>' + str ( counter ) , Color . BOLD ) <EOL> if notify : <EOL> notify_mac ( "<STR_LIT>" ) <EOL> time . sleep ( <NUM_LIT> ) </s>
