Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

Re^6: RFC on how I'm doing when it comes to writing objects?

by Lady_Aleena (Deacon)
on Feb 06, 2013 at 19:07 UTC ( #1017487=note: print w/replies, xml ) Need Help??

in reply to Re^5: RFC on how I'm doing when it comes to writing objects?
in thread RFC on how I'm doing when it comes to writing objects?

Maybe if I showed some of the scripts where the data is used, maybe some pointers can be given on where my mind should be if I decide to try again with OO for the data.

list_links sends HTML list items <li> to the clip board so I can paste them into a Tumblr post I update every once in a while to get more people to subscribe.

#!/usr/bin/perl use strict; use warnings FATAL => qw( all ); use Lingua::EN::Inflect qw(NO); use Win32::Clipboard; use lib '../files/perl/lib'; use Twitter::ListSort qw(list_compare); use Twitter::Data; my %lists = Twitter::Data::all_lists; # This is all of the lists I kee +p across all my accounts. my $board; for my $list (sort { list_compare($a,$b) } values %lists) { # Starting + the loop. next if !grep(/owner/,@{$list->{status}}); my $targ = $list->{target}; my $link = $list->{link}; my $name = length($list->{long_name}) ? $list->{long_name} : $list-> +{name}; my $targ_text = $targ > 0 ? " (".NO('subscriber',$targ)." 'til targe +t met)" : ''; my $asterisk = $list->{user} eq "Lady_Aleena" ? '*' : ''; $board .= qq(<li><a href="$link">$name</a>$asterisk$targ_text</li>\n +); } my $clip = Win32::Clipboard(); $clip->Set($board);

list_people does three things.

  1. It tells me who I have not made members of my List subscribers list. Anyone who subscribes to one of my lists becomes a member of List subscribers. I take this group and add them to List subscribers with another script. Sometimes removed or suspended Twitter accounts remain as subscribers of a list for months or even years. I check to make sure the account exists before trying to add it (or readd it) to the List subscribers list.
  2. It tells me who is no longer subscribed to my lists who still remains on the List subscribers list. There is a way with Net::Twitter::Lite to remove people from the list programmatically, I just have not added it to the script yet.
  3. Since most of my accounts (not my main Lady_Aleena account) were created to house lists, most (if not all) of the people followed by the account is either a member or subscriber to a list. This section checks to see if the account is following someone who is neither a list member or list subscriber. The first time I ran the script with this section, I ended up creating about eight new lists.
#!/usr/bin/perl use strict; use warnings FATAL => qw( all ); use Lingua::EN::Inflect qw(WORDLIST); use Text::Table; use Win32::Clipboard; use lib '../files/perl/lib'; use Twitter::Data; my @accounts = Twitter::Data::accounts; my %lists = Twitter::Data::all_lists; my %lists_people = Twitter::Data::lists_people; my @subscribers = @{$lists{'72710881'}{members}}; local $\ = "\n"; # 1 above. print 'Subscribers not on the list'; for my $subscriber (keys %{$lists_people{subscribers}}) { my $s_lists = WORDLIST(@{$lists_people{subscribers}{$subscriber}}, { +conj=>''}); print "$subscriber - $s_lists" if !grep($_ eq $subscriber,@subscribe +rs); } print "\n"; # 2 above. print 'Non-subscribers on the list'; for my $subscriber (@subscribers) { print $subscriber if !$lists_people{subscribers}{$subscriber}; } print "\n"; # 3 above print 'People followed but not listed (by account)'; for my $account (@accounts) { next if $account =~ /^(LadyAleena_(?:eros|test))$/; my %account_people = Twitter::Data::acct_data($account,'people'); my @not_members; for my $person (values %account_people) { my $name = $person->{screen_name}; push @not_members, $name if ($person->{friends} == 1 && !$lists_pe +ople{members}{$name}); } local $\ = "\n"; if (scalar(@not_members) > 0) { print "$account"; for (sort {lc $a cmp lc $b} @not_members) { $board .= "\t$_\n"; print "\t$_"; } } }

If you see anything in there where OO would come in handy, let me know. These two scripts are probably the shortest of those I have written where I need the data.

Have a cookie and a very nice day!
Lady Aleena

Replies are listed 'Best First'.
Re^7: RFC on how I'm doing when it comes to writing objects?
by mbethke (Hermit) on Feb 07, 2013 at 06:44 UTC
    Hm, I'm not sure; not being too Twitter-savvy¹ I have a very vague understanding what a "list" and a "subscriber" is in this context. However, it looks like you updated your modules with hardcoded constants whenever you change some list. You could do something like this to read them from various sources like text files or SQLite:
    package Twitter::Data; use Modern::Perl; sub new { my $class = shift; return bless {}, $class; } sub accounts { croak("Please override accounts()"); } sub get_account_data { croak("Please override get_account_data()"); } sub all_account_data { my $self = shift; my %accounts; foreach my $acct ($self->accounts) { $accounts{$acct} = $self->get_account_data($acct); } } package Twitter::Data::Text; use Modern::Perl; use Carp; use YAML; use base 'Twitter::Data'; sub new { my $class = shift; my %args = @_; defined $args{db} or croak("`db' arg missing"); -d $args{db} or croak("`db' arg is not a directory"); my $self = $class->SUPER::new(%args); $self->{db} = $args{db}; return $self; } # Get a list of all accounts sub accounts { my $self = shift; return glob("$self->{db}/*"); } sub get_account_data { my ($self, $acct) = @_; return YAML::LoadFile("$self->{db}/$acct/accontdata.yml"); } package Twitter::Data::SQLite; use Modern::Perl; use Carp; use DBIx::Simple; use parent 'Twitter::Data'; sub new { my $class = shift; my %args = @_; defined $args{db} or croak("`db' arg missing"); my $self = $class->SUPER::new(%args); $self->{db} = DBIx::Simple->new("dbi:SQLite:dbname=$args{db}",""," +") or croak("can't open db"); return $self; } # Get a list of all accounts sub accounts { my $self = shift; $self->{sql_acctnames}->execute; return $self->{db}->select('SELECT name FROM accounts')->flat; } sub get_account_data { my ($self, $acct) = @_; return $self->{db}->select('SELECT * FROM accounts WHERE name=?')- +>hash; }
    A method like get_account_data would be shared by any underlying implementation so it would work no matter how the account data stuff is actually stored even though you call it on the object that is of a derived class.

    ¹ I follow a handful of news aggregators and never twit, or whatever the verb is for what you to on Twitter.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1017487]
[Corion]: Hi marto ;)
[Corion]: I hope you're well ;)
[marto]: running late today, get to work and people are just sitting around. "can't delete file, quota exceeded"
[Corion]: marto: Oy :-/
[Corion]: Ah, it's only "quota exceeded", not "filesystem full", so it's "merely" an administrative act to up the FS quota resp. inode quota
[Corion]: I've been on databases where I couldn't drop a table or delete rows because the DB was full. Not so much fun...
[marto]: meh, the admins are lazy, I just piped null to some old large log files, problem solved

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (10)
As of 2017-01-24 09:56 GMT
Find Nodes?
    Voting Booth?
    Do you watch meteor showers?

    Results (203 votes). Check out past polls.