<s> import sublime <EOL> import sublime_plugin <EOL> import threading <EOL> from libs . hnapi import * <EOL> always_online_url = "<STR_LIT>" <EOL> hn_url = "<STR_LIT>" <EOL> timeout = <NUM_LIT:2> <EOL> hnAPi = HackerNewsAPI ( ) <EOL> class HackerNewsReaderCommand ( sublime_plugin . WindowCommand ) : <EOL> def run ( self ) : <EOL> sublime . status_message ( '<STR_LIT>' ) <EOL> statusThread = CheckStatus ( self . onInternetThreadResult , always_online_url , hn_url , timeout ) <EOL> statusThread . start ( ) <EOL> newsThread = HNRSSNewsLoad ( self . onNewsThreadResult ) <EOL> newsThread . start ( ) <EOL> def onNewsThreadResult ( self , data ) : <EOL> self . hnData = data <EOL> sublime . set_timeout ( self . displayItems , <NUM_LIT:0> ) <EOL> def displayItems ( self ) : <EOL> self . feed_text = [ ] <EOL> for item in self . hnData : <EOL> first_line = '<STR_LIT>' % ( item . number , item . title . decode ( '<STR_LIT:utf8>' ) ) <EOL> second_line = '<STR_LIT>' % ( item . score , item . submitter , item . commentCount , item . domain ) <EOL> self . feed_text . append ( [ first_line , second_line ] ) <EOL> self . window . show_quick_panel ( self . feed_text , self . onItemSelection ) <EOL> def onItemSelection ( self , index ) : <EOL> if ( index != - <NUM_LIT:1> ) : <EOL> self . selected_item_index = index <EOL> item = self . hnData [ index ] <EOL> menu_text = [ ] <EOL> menu_text . append ( '<STR_LIT>' ) <EOL> menu_text . append ( '<STR_LIT>' ) <EOL> menu_text . append ( '<STR_LIT>' % item . submitter ) <EOL> self . window . show_quick_panel ( menu_text , self . onMenuChoiceSelection ) <EOL> def onMenuChoiceSelection ( self , index ) : <EOL> if ( index != - <NUM_LIT:1> ) : <EOL> if ( index == <NUM_LIT:1> ) : <EOL> self . openURL ( self . hnData [ self . selected_item_index ] . commentsURL ) <EOL> elif ( index == <NUM_LIT:0> ) : <EOL> self . openURL ( self . hnData [ self . selected_item_index ] . URL ) <EOL> else : <EOL> item = self . hnData [ self . selected_item_index ] <EOL> statusThread = CheckStatus ( self . onInternetThreadResult , always_online_url , hn_url , timeout ) <EOL> statusThread . start ( ) <EOL> userThread = HNRSSUserLoad ( self . onUserThreadResult , item . submitter ) <EOL> userThread . start ( ) <EOL> url = None <EOL> def onUserThreadResult ( self , data ) : <EOL> self . userData = data <EOL> sublime . set_timeout ( self . displayUser , <NUM_LIT:0> ) <EOL> def displayUser ( self ) : <EOL> menu_text = [ ] <EOL> first_line = self . userData . name <EOL> second_line = '<STR_LIT>' % ( self . userData . karma , self . userData . userPageURL ) <EOL> menu_text . append ( [ first_line , second_line ] ) <EOL> self . window . show_quick_panel ( menu_text , self . onUserMenuChoiceSelection ) <EOL> def onUserMenuChoiceSelection ( self , index ) : <EOL> if ( index != - <NUM_LIT:1> ) : <EOL> self . openURL ( self . userData . userPageURL ) <EOL> def openURL ( self , url ) : <EOL> import webbrowser <EOL> webbrowser . open ( url ) <EOL> def onInternetThreadResult ( self , status , service_status ) : <EOL> self . internetStatus = status <EOL> self . service_status = service_status <EOL> sublime . set_timeout ( self . displayError , <NUM_LIT:0> ) <EOL> def displayError ( self ) : <EOL> if ( not self . internetStatus ) : <EOL> sublime . status_message ( '<STR_LIT>' ) <EOL> elif ( not self . service_status ) : <EOL> sublime . status_message ( '<STR_LIT>' ) <EOL> class HNRSSNewsLoad ( threading . Thread ) : <EOL> def __init__ ( self , callback ) : <EOL> self . result = None <EOL> threading . Thread . __init__ ( self ) <EOL> self . callback = callback <EOL> def run ( self ) : <EOL> self . result = hnAPi . getTopStories ( ) <EOL> self . callback ( self . result ) <EOL> return <EOL> class HNRSSUserLoad ( threading . Thread ) : <EOL> def __init__ ( self , callback , username ) : <EOL> self . username = username <EOL> self . result = None <EOL> threading . Thread . __init__ ( self ) <EOL> self . callback = callback <EOL> def run ( self ) : <EOL> user = HackerNewsUser ( self . username ) <EOL> self . result = user <EOL> self . callback ( self . result ) <EOL> return <EOL> class CheckStatus ( threading . Thread ) : <EOL> def __init__ ( self , callback , check_url , service_url , timeout ) : <EOL> self . timeout = timeout <EOL> self . check_url = check_url <EOL> self . service_url = service_url <EOL> threading . Thread . __init__ ( self ) <EOL> self . callback = callback <EOL> def run ( self ) : <EOL> try : <EOL> urllib2 . urlopen ( self . check_url , timeout = self . timeout ) <EOL> try : <EOL> urllib2 . urlopen ( self . service_url , timeout = self . timeout ) <EOL> self . callback ( True , True ) <EOL> except : <EOL> self . callback ( True , False ) <EOL> except : <EOL> self . callback ( False , None ) <EOL> return </s>
