| 1 | package Exobrain::Twitter; | ||
| 2 | use Moose; | ||
| 3 | use Exobrain::Config; | ||
| 4 | use feature qw(say); # To make 5.10 happy | ||
| 5 | |||
| 6 | # ABSTRACT: Twitter components for exobrain | ||
| 7 | our $VERSION = '1.04'; # VERSION | ||
| 8 | |||
| 9 | |||
| 10 | with 'Exobrain::Component'; | ||
| 11 | |||
| 12 | # This is the namespace everything will be installed in by default. | ||
| 13 | # It's automatically prepended with 'exobrain' | ||
| 14 | |||
| 15 | sub component { "twitter" }; | ||
| 16 | |||
| 17 | # These are the services in that namespace. So we have | ||
| 18 | # 'exobrain.twitter.source' and 'exobrain.twitter.sink'. | ||
| 19 | |||
| 20 | sub services { | ||
| 21 | return ( | ||
| 22 | source => 'Twitter::Source', | ||
| 23 | sink => 'Twitter::Sink', | ||
| 24 | response => 'Twitter::Response', | ||
| 25 | ) | ||
| 26 | } | ||
| 27 | |||
| 28 | sub setup { | ||
| 29 | |||
| 30 | # Load our module for testing. | ||
| 31 | |||
| 32 | eval 'use Net::Twitter'; | ||
| 33 | die $@ if $@; | ||
| 34 | |||
| 35 | say "\n\nHey there, we'll need to start by getting an API key from twitter.\n"; | ||
| 36 | say "Head over to: https://apps.twitter.com/app/new\n"; | ||
| 37 | say "You need only fill in the required fields there."; | ||
| 38 | say "Alternatively, if you already have a registered app, you can use that."; | ||
| 39 | |||
| 40 | say "\n--> Your application MUST require read, write, and DM access <--\n"; | ||
| 41 | |||
| 42 | say "\nOnce done, you'll need to provide the API key and secret."; | ||
| 43 | say "These are not shared with anyone."; | ||
| 44 | |||
| 45 | print "\nAPI key: "; | ||
| 46 | chomp(my $consumer_key = <STDIN>); | ||
| 47 | |||
| 48 | print "\nAPI secret: "; | ||
| 49 | chomp(my $consumer_secret = <STDIN>); | ||
| 50 | |||
| 51 | unless ($consumer_key and $consumer_secret) { | ||
| 52 | die "I need both the key and secret to proceed. Halting.\n"; | ||
| 53 | } | ||
| 54 | |||
| 55 | say "\n\nThanks! Authing with twitter...\n"; | ||
| 56 | |||
| 57 | my $nt = Net::Twitter->new( | ||
| 58 | traits => ['API::RESTv1_1', 'OAuth'], | ||
| 59 | consumer_key => $consumer_key, | ||
| 60 | consumer_secret => $consumer_secret, | ||
| 61 | ssl => 1, | ||
| 62 | ); | ||
| 63 | |||
| 64 | say "\n----------------------------------------------\n"; | ||
| 65 | |||
| 66 | say "Great! Now, to complete the auth, visit this URL, and enter the PIN...\n"; | ||
| 67 | |||
| 68 | say $nt->get_authorization_url; | ||
| 69 | |||
| 70 | print "\nPIN: "; | ||
| 71 | |||
| 72 | my $pin = <STDIN>; # wait for input | ||
| 73 | chomp $pin; | ||
| 74 | |||
| 75 | my($access_token, $access_token_secret, $user_id, $screen_name) = $nt->request_access_token(verifier => $pin); | ||
| 76 | |||
| 77 | say "\n\nThanks \@$screen_name!"; | ||
| 78 | |||
| 79 | my $config = | ||
| 80 | "[Components]\n" . | ||
| 81 | "Twitter=$VERSION\n\n" . | ||
| 82 | |||
| 83 | "[Twitter]\n" . | ||
| 84 | "consumer_key = $consumer_key\n" . | ||
| 85 | "consumer_secret = $consumer_secret\n" . | ||
| 86 | "access_token = $access_token\n" . | ||
| 87 | "access_token_secret = $access_token_secret\n" | ||
| 88 | ; | ||
| 89 | |||
| 90 | my $filename = Exobrain::Config->write_config('Twitter.ini', $config); | ||
| 91 | |||
| 92 | say "\n\nConfig written to $filename. Have a nice day!"; | ||
| 93 | } | ||
| 94 | |||
| 95 | 1; | ||
| 96 | |||
| 97 | __END__ |