root/trunk/ActionStreams/plugins/ActionStreams/lib/ActionStreams/Fix.pm @ 1562

Revision 1562, 4.5 kB (checked in by mpaschal, 5 months ago)

Don't look for a thumbnail URL where there isn't one
(prevents UUV in Kongregate achievements stream)

Line 
1
2package ActionStreams::Fix;
3
4use strict;
5use warnings;
6
7use ActionStreams::Scraper;
8
9sub _twitter_add_tags_to_item {
10    my ($item) = @_;
11    if (my @tags = $item->{title} =~ m{
12        (?: \A | \s )  # BOT or whitespace
13        \#             # hash
14        (\w\S*\w)      # tag
15        (?<! 's )      # but don't end with 's
16    }xmsg) {
17        $item->{tags} = \@tags;
18    }
19}
20
21sub twitter_tweet_name {
22    my ($cb, $app, $item, $event, $author, $profile) = @_;
23    # Remove the Twitter username from the front of the tweet.
24    my $ident = $profile->{ident};
25    $item->{title} =~ s{ \A \s* \Q$ident\E : \s* }{}xmsi;
26    _twitter_add_tags_to_item($item);
27}
28
29sub twitter_favorite_author {
30    my ($cb, $app, $item, $event, $author, $profile) = @_;
31    # Remove the Twitter username from the front of the tweet.
32    if ($item->{title} =~ s{ \A \s* ([^\s:]+) : \s* }{}xms) {
33        $item->{tweet_author} = $1;
34    }
35    _twitter_add_tags_to_item($item);
36}
37
38sub flickr_photo_thumbnail {
39    my ($cb, $app, $item, $event, $author, $profile) = @_;
40    # Extract just the URL, and use the _t size thumbnail, not the _m size image.
41    my $thumb = delete $item->{thumbnail};
42    if ($thumb =~ m{ (http://farm[^\.]+\.static\.flickr\.com .*? _m.jpg) }xms) {
43        $thumb = $1;
44        $thumb =~ s{ _m.jpg \z }{_t.jpg}xms;
45        $item->{thumbnail} = $thumb;
46    }
47}
48
49sub iminta_link_title {
50    my ($cb, $app, $item, $event, $author, $profile) = @_;
51    # Remove the username for when we add it back in later.
52    $item->{title} =~ s{ (?: \s* :: [^:]+ ){2} \z }{}xms;
53}
54
55sub instructables_favorites_thumbnails {
56    my ($cb, $app, $item, $event, $author, $profile) = @_;
57    $item->{thumbnail} = URI->new_abs($item->{thumbnail}, 'http://www.instructables.com/')
58        if $item->{thumbnail};
59}
60
61sub iusethis_event_title {
62    my ($cb, $app, $item, $event, $author, $profile) = @_;
63    # Remove the username for when we add it back in later.
64    $item->{title} =~ s{ \A \w+ \s* }{}xms;
65}
66
67sub metafilter_favorites_titles {
68    my ($cb, $app, $item, $event, $author, $profile) = @_;
69    $item->{title} =~ s{ \A [^:]+ : \s* }{}xms;
70}
71
72sub netflix_recent_prefix_thumb {
73    my ($cb, $app, $item, $event, $author, $profile) = @_;
74    # Remove the 'Shipped:' or 'Received:' prefix.
75    $item->{title} =~ s{ \A [^:]*: \s* }{}xms;
76
77    # Extract thumbnail from description.
78    my $thumb = delete $item->{thumbnail};
79    if ($thumb =~ m/ <img src="([^"]+)"} /xms) {
80        $item->{thumbnail} = $1;
81    }
82}
83
84sub netflix_queue_prefix_thumb {
85    my ($cb, $app, $item, $event, $author, $profile) = @_;
86    # Remove the item number.
87    $item->{title} =~ s{ \A \d+ [\W\S] \s* }{}xms;
88
89    # Extract thumbnail from description.
90    my $thumb = delete $item->{thumbnail};
91    if ($thumb =~ m/ <img src="([^"]+)"} /xms) {
92        $item->{thumbnail} = $1;
93    }
94}
95
96sub nytimes_links_titles {
97    my ($cb, $app, $item, $event, $author, $profile) = @_;
98    return $item->{title} =~ s{ \A [^:]* recommended [^:]* : \s* }{}xms ? 1 : 0;
99}
100
101sub p0pulist_stuff_urls {
102    my ($cb, $app, $item, $event, $author, $profile) = @_;
103    $item->{url} =~ s{ \A / }{http://p0pulist.com/}xms;
104}
105
106sub kongregate_achievement_title_thumb {
107    my ($cb, $app, $item, $event, $author, $profile) = @_;
108    # Remove the parenthetical from the end of the title.
109    $item->{title} =~ s{ \( [^)]* \) \z }{}xms;
110
111    # Pick the actual achievement badge out of the inline CSS.
112    my $thumb = delete $item->{thumbnail} || q{};
113    if ($thumb =~ m{ background-image: \s* url\( ([^)]+) }xms) {
114        $item->{thumbnail} = $1;
115    }
116}
117
118sub wists_thumb {
119    my ($cb, $app, $item, $event, $author, $profile) = @_;
120    # Grab the wists thumbnail out.
121    my $thumb = delete $item->{thumbnail};
122    if ($thumb =~ m{ (http://cache.wists.com/thumbnails/ [^"]+ ) }xms) {
123        $item->{thumbnail} = $1;
124    }
125}
126
127sub gametap_score_stuff {
128    my ($cb, $app, $item, $event, $author, $profile) = @_;
129    $item->{score} =~ s{ \D }{}xmsg;
130    $item->{url} = q{} . $item->{url};
131    $item->{url} =~ s{ \A / }{http://www.gametap.com/}xms;
132}
133
134sub typepad_comment_titles {
135    my ($cb, $app, $item, $event, $author, $profile) = @_;
136    $item->{title} =~ s{ \A .*? ' }{}xms;
137    $item->{title} =~ s{ ' \z }{}xms;
138}
139
140sub magnolia_link_notes {
141    my ($cb, $app, $item, $event, $author, $profile) = @_;
142    my $scraper = scraper {
143        process '//p[position()=2]', note => 'TEXT';
144    };
145
146    my $result = $scraper->scrape(\$item->{note});
147
148    if ($result->{note}) {
149        $item->{note} = $result->{note};
150    }
151    else {
152        delete $item->{note};
153    }
154}
155
1561;
157
Note: See TracBrowser for help on using the browser.