developing apps using perl

87
Developing apps using Perl Anatoly Sharifulin YAPC::Russia 2012

Upload: anatoly-sharifulin

Post on 12-May-2015

4.691 views

Category:

Education


0 download

DESCRIPTION

Talk from PerlMova 2012 and YAPC::Russia "May Perl"

TRANSCRIPT

Page 1: Developing apps using Perl

Developing appsusing Perl

Anatoly SharifulinYAPC::Russia 2012

Page 2: Developing apps using Perl

Hello, World!

Page 3: Developing apps using Perl

«Happy Perl developer»about me by Andy Shitov

Page 4: Developing apps using Perl

I program in Perl lessBut it doesn't mean I love it less :-)

Page 5: Developing apps using Perl

JFDI became JFTIJust F*cking Talk about It

Page 6: Developing apps using Perl

Success-storyof one app

And how Perl helped me/us with it

Page 7: Developing apps using Perl

By us I mean Applifto

Page 9: Developing apps using Perl

Our applications

Page 10: Developing apps using Perl

Audience polling

Page 12: Developing apps using Perl

«Why?» and «For what?» in details

http://www.slideshare.net/sharifulin/ss-12313103

Page 13: Developing apps using Perl

A powerful reasonfor me

I was moving from LiveJournal to Posterous, used transfer scripts and changed Twitter and Facebook

crossposting settings when started to get replies like 'Tolya! Stop it! Enough!'

Page 14: Developing apps using Perl

Current analogues are nice... not!

Slow, awkward, plain, trouble working:TwitWipe, TwitCide, Delete My Tweets, ...

Page 15: Developing apps using Perl

«Stop managering, let’s start programming»

And have a talk at #yr2012 :-)

Page 16: Developing apps using Perl

Approach and solutions

Page 17: Developing apps using Perl

Main aims for me

• App must delete tweets quickly

• Appropriate deleting

• Cross-platform app: iOS, Android, Web, ...

Page 18: Developing apps using Perl

Server API— hello, Perl!

All logic — on our server API (not Twitter API),clients is simple interface

Page 19: Developing apps using Perl

Getting to know Twitter API

Page 20: Developing apps using Perl

Little experience:

• Getting user profiles after login through Twitter on websites

• Auto-posting to Twitter with Net::Twitter(::Lite)

• AnyEvent::Twitter::Stream for real-time search (hey, @miyagawa!)

Page 21: Developing apps using Perl

Blocking wayTask can be solved with Net::Twitter(::Lite)

Page 22: Developing apps using Perl

LWP::UserAgent::POE Transport may be changed in Net::Twitter(::Lite)

Page 23: Developing apps using Perl

NO

Page 24: Developing apps using Perl

Idea — 2 separate asynchronous queues

Get timeline and delete tweets using REST Twitter API and asynchronous requests

Page 25: Developing apps using Perl

This has to work fast!

Page 26: Developing apps using Perl

Authorization and OAuth

Page 27: Developing apps using Perl

Net::OAuth::AllWe've written our OAuth module long time ago,

supports all OAuth versions (1.0, 1.0A, 2.0)

Page 28: Developing apps using Perl

Net::OAuth::Allhttps://github.com/likhatskiy/Net-OAuth-All

Page 29: Developing apps using Perl

my $oauth = Net::OAuth::All->new( consumer_secret => $conf->{consumer_secret}, consumer_key => $conf->{consumer_key },

token => $data->{access_token }, token_secret => $data->{access_token_secret},);

Page 30: Developing apps using Perl

$oauth ->via('GET') ->protected_resource_url('https://api.twiiter.com/...’) ->put_extra( include_rts => 'true', user_id => $user->{id}, count => 200, ) ->request('protected_resource');$oauth->to_header;$oauth->url; # $oauth->url_with_extra;

Page 31: Developing apps using Perl

Patched the moduleUnauthorized user:

limit of 150 requests per hour from one IP, not 350 requests per hour from one user

Page 32: Developing apps using Perl

Asynchronous requests

Page 33: Developing apps using Perl

I use MojoliciousNot a secret at all :-)

Page 34: Developing apps using Perl

Mojo::UserAgentMojo::IOLoopGood asynchronous HTTP client

Page 35: Developing apps using Perl

my $delay = Mojo::IOLoop->delay;for (@$tasks) { ... $delay->begin; $ua->get($url => {'Authorization' => $h} => sub { my ($ua, $tx) = @_; ... $delay->end( ... ); });}

say $delay->wait;

Page 36: Developing apps using Perl

Server API

Page 37: Developing apps using Perl

Server APIAPI supports GET and POST requests,

JSON transmission format,response codes 200 and 50x.

Page 38: Developing apps using Perl

Server APIStarman + Mojolicious

Mojolicious::Plugin::ApiHelpers

Page 39: Developing apps using Perl

Server APIhttp://api.dlttr.com

Page 40: Developing apps using Perl

package App::Api;use App::Base -controller, with => [ 'App::Task', 'App::User' ];

sub error { shift->api_error(code => 1) }sub any { shift->api_error(code => 5) }

...

1;

Page 41: Developing apps using Perl

my $api = $r->route('/api')->to('api#', api => 1);$api->route('/:action', action => qr/login|oauth/)->to;

my $apiu = $api->bridge->to('#auth', sign => 1);$apiu->bridge('/task/new')->to('#task_check') ->route->to('#task_new');

$api->bridge('/')->to('#auth', maybe => 1, sign => 0) ->route->to('#hello');

Page 42: Developing apps using Perl

10 methods,123 tests and documentation

Most methods require user authorization and check sign (api_id + secret)

Page 43: Developing apps using Perl

Test::MojoTest::More

Testing API methods access, input-output data, signature check and etc.

Page 44: Developing apps using Perl

Test::MojoTest::More

A real task from a test user

Page 45: Developing apps using Perl

All methods are coveredWithout Mock-objects, no check

if a correct cluster of tweets have been deleted

Page 46: Developing apps using Perl

Too laborious and needlessA real cycle cannot be tested:

posting tweets – deleting – check

Page 47: Developing apps using Perl

$t->get_ok("$url/") ->status_is(200) ->header_is('Access-Control-Allow-Origin', '*') ->json_content_is({hello => 'Hello DLTTR!'});

$t->get_ok(sign get => "$url/task/new") ->status_is(200) ->json_content_is({error => {msg => 'User authorization failed', code => 2}});

Page 48: Developing apps using Perl

Too risky to test on your own account :-)

— You don't care about you Twitter acc, do you?— Yep!— So I can delete all your tweets?— Nooooooooooooooo!!!

Page 49: Developing apps using Perl

Simple debug reguests

To check the quality of client work requests/responses with before_dispatch + after_dispatch may be used

Page 50: Developing apps using Perl

Three queues

Page 51: Developing apps using Perl

1. Queue for timelinesSearch and filters on tweets ID to delete,

paging with since_id and max_id

Page 52: Developing apps using Perl

1. Queue for timelinesMax. 200 tweets per request,

getting timeline one by one per user, Rate Limit check

Page 53: Developing apps using Perl

«Limits» by TwitterNo access to all tweets,

only the latest ~3200 or starting from any exact date, counter is not on zero and etc.

Page 54: Developing apps using Perl

2. Queue for deleting tweets

Deleting with 'clusters' of 200 tweets, no limits. «Buster» or «Cannon» :-)

Page 55: Developing apps using Perl

2. Queue for deleting tweets

Error processing — Twitter API error doesn't always mean a failure

Page 56: Developing apps using Perl

3. Queue for Push notify

On iOSNet::APNS::Persistent (also AnyEvent::APNS),

to check if tokens are valid — Net::APNS::Feedback

Page 57: Developing apps using Perl

On AndroidWWW::Google::C2DM and WWW::Google::ClientLogin

3. Queue for Push notify

Page 58: Developing apps using Perl

Database

Page 59: Developing apps using Perl

So easy — mysqlStoring and task orders,

just to relax :-)

Page 60: Developing apps using Perl

2 finer points:

Page 61: Developing apps using Perl

1. Create correct indexFor queues

Page 62: Developing apps using Perl

2. Disable request caching

select SQL_NO_CACHE * from task where ...

Page 63: Developing apps using Perl

Site and web-version

Page 66: Developing apps using Perl

Site and web-version

• Starman + Mojolicious

• Mojolicious::Plugin::I18N2— my fork of I18N plugin

• Mojolicious::Plugin::OAuth— login through Net::OAuth::All

• Mojolicious::Plugin::ShareHelpers

• Mojolicious::Plugin::UtilHelpers

Page 67: Developing apps using Perl

Admin and statistics

Page 68: Developing apps using Perl

«Big Brother»All details on users, number of tasks, feedbacks

and real-time statistics

Page 69: Developing apps using Perl

Statistics on users

Page 70: Developing apps using Perl
Page 71: Developing apps using Perl

Users wall

Page 72: Developing apps using Perl

«Ordinary» apps don't have statistics like that

Page 73: Developing apps using Perl

1M+ tweets deleted,3K+ users,

20% make purchaseFor 2 months

Page 74: Developing apps using Perl

Promotion

Page 75: Developing apps using Perl

Promotion on TwitterFollowing, favorites, replies and search —

Net::Twitter(::Lite)

Page 76: Developing apps using Perl

But they blocked me three times already :-)

Promotion on Twitter

Page 77: Developing apps using Perl

Summary

Page 78: Developing apps using Perl

Positive

• Easy solution using pure Perl(work time is about 30 hours)

• Works fast, really fast

• Right delete tweets

• Any changes, updates, new services— everything's on the server, no hard

• Real-time statistics

Page 79: Developing apps using Perl

Only one negative:Server's down — the app doesn't work,

users become mad, low rates and spell curses :-)

Page 80: Developing apps using Perl

Check out DLTTR!http://dlttr.com/app http://dlttr.com/android

www.dlttr.com

Page 81: Developing apps using Perl
Page 82: Developing apps using Perl

«Seems like a simple app but in fact it's awesome

and in Perl»

Page 83: Developing apps using Perl

P. S.

Page 84: Developing apps using Perl

At #BarCampKrrI had a talk

«Developing app from idea to release»

(Perl wasn't mentioned)

Page 85: Developing apps using Perl

But everything came down to... Perl

«What language do you use?», «But why?»,

«Is it difficult to find programmers like those?» :-)

Page 86: Developing apps using Perl

use Perl or die;

Page 87: Developing apps using Perl

Thanks!Anatoly SharifulinYAPC::Russia 2012