def tw_authenticate(apikey, ck, cs): """OAuth authentication on Twitter""" auth = tweepy.OAuthHandler(ck, cs) auth_url = auth.get_authorization_url() print("Please authorize this application by giving access to your Twitter account") open_url_in_browser(auth_url) pin = input('Paste PIN here: ') auth.get_access_token(pin) if auth.access_token.key and auth.access_token.secret: tw_format_string = """# Format of the tweet: # PREP is what comes before the song link, POSTP is what comes after # Leave empty either (or even both) if you want the the tinysong link to be # at the beginning/end of the tweet. # Don't use too many characters or you'll risk hitting the 140 chars limit. """ with open('tinysongconfig.py', 'w') as f: f.write("#-*- coding: utf-8 -*-\n\n") f.write("APIKEY = '%s'\n" % apikey) f.write("TW_CONSUMER = '%s'\nTW_CONSUMER_SECRET = '%s'\n" % (ck, cs)) f.write("TW_ACCESS = '%s'\nTW_ACCESS_SECRET = '%s'\n\n" % (auth.access_token.key, auth.access_token.secret)) f.write(tw_format_string) f.write("PREP = '%s'\n" % PREP.decode("utf-8")) f.write("POSTP = '%s'\n" % POSTP.decode("utf-8")) return auth.access_token.key, auth.access_token.secret else: print("Authentication unsuccessful (perhaps wrong PIN?") sys.exit(1)
def main(): options = argument_parser() joined_args = '+'.join(options.args) ts = TinySongSearcher() if options.mpd: mpdartist, mpdalbum, mpdsong = mpd_get_song() # we use what mpd gives back as tinysong arguments joined_args = '+'.join([mpdartist, mpdalbum, mpdsong]) # if we are going to tweet, we need to do at least a meta search if options.tweet: options.metasearch = True if options.cmus: c = tinysong_cmus.CmusStatusParser() cmusartist, cmusalbum, cmussong = c.parse() print(cmusartist, cmusalbum, cmussong) # form arguments for tinysong query joined_args = '+'.join([cmusartist, cmusalbum, cmussong]) # if we are going to tweet, we need to do at least a meta search if options.tweet: options.metasearch = True # remove any whitespaces from the query joined_args = '+'.join(joined_args) if options.limit: result_url, artistname, albumname, songname = ts.limit_search(joined_args, options.limit) elif options.metasearch: result_url, artistname, albumname, songname = ts.meta_search(joined_args) else: result_url = ts.basic_search(joined_args) # stop if we haven't got any result from tinysong if not result_url: ts.not_found() if options.openbrowser: open_url_in_browser(result_url) if options.tweet: try: # import... from tinysongconfig import TW_CONSUMER, TW_CONSUMER_SECRET, \ TW_ACCESS, TW_ACCESS_SECRET # ...and tweet! check_tweet(TW_CONSUMER, TW_CONSUMER_SECRET, TW_ACCESS, TW_ACCESS_SECRET, result_url, \ artistname, songname) except ImportError: # we need to authenticate then TW_ACCESS, TW_ACCESS_SECRET = tw_authenticate(APIKEY, TW_CONSUMER, TW_CONSUMER_SECRET) if TW_ACCESS and TW_ACCESS_SECRET: check_tweet(TW_CONSUMER, TW_CONSUMER_SECRET, TW_ACCESS, TW_ACCESS_SECRET, result_url, \ artistname, songname) else: print("It seems the authentication process went wrong...") sys.exit(1)