|
- #!/usr/bin/env ruby
- require 'cross-post'
- require 'mastodon'
- require 'oauth2'
- require 'launchy'
- require 'uri'
-
- config = CrossPost::Config.new
- settings = config[:settings]
-
- url = settings['mastodon.url']
- unless url
- print 'Mastodon URL ? '
- url = gets.chomp
- url = "https://#{url}" if URI.parse(url).class == URI::Generic
- settings['mastodon.url'] = url
- end
-
- user = settings['mastodon.user']
- unless user
- print 'Mastodon username ? '
- user = gets.chomp
- settings['mastodon.user'] = user
- end
-
- client_id, client_secret = unless settings['mastodon.consumer']
- puts 'Creating new app'
- token = SecureRandom.hex 64
- redirect_url = 'urn:ietf:wg:oauth:2.0:oob'
-
- client = Mastodon::REST::Client.new base_url: url, bearer_token: token
- app = client.create_app 'CrossPost', redirect_url,
- 'read write', 'https://git.imirhil.fr/aeris/cross-post/'
- settings['mastodon.consumer.key'] = app.client_id
- settings['mastodon.consumer.secret'] = app.client_secret
- [app.client_id, app.client_secret]
- else
- [settings['mastodon.consumer.key'], settings['mastodon.consumer.secret']]
- end
-
- client = OAuth2::Client.new client_id, client_secret, site: url
- url = client.auth_code.authorize_url redirect_uri: redirect_url
- puts url
- begin
- Launchy.open url
- rescue
- end
-
- print 'Token ? '
- token = gets.chomp
- settings['mastodon.token'] = token
-
- settings.save
|