2

聞いたことがあるかもしれませんが、現在、いくつかのサブレディット、特にr/atheismがチャリティードライブを行っています。資金調達を支援/奨励するために、私はこれらの寄付に関するリアルタイムの情報を提供するための小さなWebユーティリティを書き始めました(基本的に、RedditからのデータをFirstGivingからのデータとマッシュアップします)-あなたは私がこれまでに持っているものを見ることができますここでは、各subredditの合計と平均値を示しているだけであり、非常に予備的なものです(これもきれいではありません)。

私が追加したい機能は、FirstGivingが提供していないように見える機能、特定の寄付を検索またはリンクする機能です。先週、人々が寄付のマッチングなどを提供しようとした投稿がたくさんありましたが、偽の/トロールの投稿もたくさんあり、誰かが「配信」しているかどうかを確認する良い方法はありません(スクリーンショットは簡単です偽物。)FirstGivingからデータをキャッシュして、誰かがリンクできるようにする予定です。

FirstGivingページを調べたところ、文書化されていないJSON API呼び出し(ページの一番下までスクロールしてさらに寄付を表示するときに使用)があり、寄付金額、メッセージ、ニックネームのリストがHTMLテーブルとして返されるようです。Opera Dragonflyによると、ブラウザ(Opera)でアクセスすると次のようになります。

URL:    http://www.firstgiving.com/ProfileWebApi/Donations
Method: POST
Status: 200 OK
Duration:   1220 ms

詳細をリクエストする

POST /ProfileWebApi/Donations HTTP/1.1 
User-Agent: Opera/9.80 (Windows NT 6.1; U; Edition United Kingdom Local; en) Presto/2.10.229 Version/11.60
Host: www.firstgiving.com
Accept-Language: en-GB,en;q=0.9
Accept-Encoding: gzip, deflate
Referer: http://www.firstgiving.com/fundraiser/r-atheism/ratheism
Cookie: ASP.NET_SessionId=rmsl4b45jdxwykanpoqkb255
Connection: Keep-Alive
Content-Length: 111
Content-Type: application/json;
Accept: application/json, text/javascript, */*; q=0.01
X-Requested-With: XMLHttpRequest
Content-Transfer-Encoding: binary
Request body
{"EventGivingGroupId":1476950,"TotalRaised":"190776.020000","PageIsExpired":false,"PageNumber":4,"PageSize":50}
Response details
HTTP/1.1 200 OK 
Cache-Control: private
Content-Length: 62979
Content-Type: application/json; charset=utf-8
Server: Microsoft-IIS/7.5
X-AspNetMvc-Version: 2.0
X-AspNet-Version: 2.0.50727
X-Powered-By: ASP.NET
Date: Tue, 13 Dec 2011 19:13:28 GMT

{"Data":"\u0009\u000d\u000a\u0009\u0009\u0009\u0009\u000d\u000a                         <table class=\"donationTable collapsed\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\" style='height:0px; overflow:hidden;' >\u000d\u000a                            <thead class=\"visuallyhidden\">\u000d\u000a\u0009\u0009                        <tr>\u000d\u000a                                    <th scope=\"col\">Comment<\/th>\u000d\u000a                                    <th scope=\"col\" class=\"amount\">Donation<\/th>\u000d\u000a                                <\/tr>\u000d\u000a                            <\/thead>\u000d\u000a\u0009\u0009\u0009            \u000d\u000a                            <tr>                              \u000d\u000a                                  <td class=\"comment\">\u000d\u000a                                            \u000d\u000a                                                    <strong>Dear Regan Layman<\/strong>\u000d\u000a                                                Happy holidays :)<br \/>\u000d\u000a                                            \u000d\u000a                                                <time datetime=\"2011-12-10T21:55:35.0000000\">\u000d\u000a                                                    12\/10\/2011\u000d\u000a                                                <\/time>\u000d\u000a                                            \u000d\u000a                                   <\/td>\u000d\u000a                               \u000d\u000a                              <td class=\"amount\">\u000d\u000a                                $20.00<sup style=\"font-size:10px;\" title=\"Offline donation\"><\/sup> \u000d\u000a                                \u000d\u000a                              <\/td>\u000d\u000a                        <\/tr>\u000d\u000a\u0009                \u000d\u000a                            <tr>                              \u000d\u000a                                  <td class=\"comment\">\u000d\u000a                                            \u000d\u000a                                                    <strong>Frodo Baggins<\/strong>\u000d\u000a                                                Due to the fact that doctors heal people, not God!<br \/>\u000d\u000a                                            \u000d\u000a                                                <time datetime=\"2011-12-10T21:52:11.0000000\">\u000d\u000a                                                    12\/10\/2011\u000d\u000a                                                <\/time>\u000d\u000a                                            \u000d\u000a                                   <\/td>\u000d\u000a                               \u000d\u000a                              <td class=\"amount\">\u000d\u000a                                $4.64<sup style=\"font-size:10px;\" title=\"Offline donation\"><\/sup> \u000d\u000a                                \u000d\u000a                              <\/td>\u000d\u000a                        <\/tr>\u000d\u000a\u0009                \u000d\u000a                            

(応答本文の残りの部分を削除しました。また、通常はより多くのCookieがありますが、aspsession idを除くすべてを手動で削除しました。正常に機能したため、分析など以外には関連していないようです)

ただし、 perlスクリプトから同じことを実行しようとすると、この有用な出力が得られません。これが私のスクリプトです:

#!/usr/bin/perl -w

use LWP::Simple;
use JSON;

use HTTP::Cookies;
use LWP::UserAgent;

use Data::Dumper;

my $cookie_jar = HTTP::Cookies->new;
my $ua = LWP::UserAgent->new(cookie_jar => $cookie_jar);
#push @{ $ua->requests_redirectable }, 'POST';
$ua->get('http://www.firstgiving.com/fundraiser/r-atheism/ratheism');

print Dumper $cookie_jar;

my $req = HTTP::Request->new(
    'POST',
    'http://www.firstgiving.com/ProfileWebApi/Donations');
$req->header('Accept-Encoding' => 'gzip, deflate');
$req->header('Referer' => 'http://www.firstgiving.com/fundraiser/r-atheism/ratheism');
$req->header('X-Requested-With' => 'XMLHttpRequest');
$req->header('Content-Transfer-Encoding' => 'binary');
$req->header('Content-type:' => 'application/json');
$req->header('User-Agent' => 'Opera/9.80 (Windows NT 6.1; U; Edition United Kingdom Local; en) Presto/2.10.229 Version/11.60');
$req->content('{"EventGivingGroupId":1476950,"TotalRaised":"190776.020000","PageIsExpired":true,"PageNumber":2,"PageSize":50}');
#$req->content('{"EventGivingGroupId":1476950,"PageNumber":1,"PageSize":50}');

my $post_request = $ua->request($req);
print Dumper( ($post_request) );

そしてここに出力があります:

$VAR1 = bless( {
                 'COOKIES' => {
                                'www.firstgiving.com' => {
                                                           '/' => {
                                                                    'ASP.NET_SessionId' => [
                                                                                             0,
                                                                                             'yynhqi2udtz4y055fakdvjiu',
                                                                                             undef,
                                                                                             1,
                                                                                             undef,
                                                                                             undef,
                                                                                             1,
                                                                                             {
                                                                                               'HttpOnly' => undef
                                                                                             }
                                                                                           ]
                                                                  }
                                                         }
                              }
               }, 'HTTP::Cookies' );
$VAR1 = bless( {
                 '_protocol' => 'HTTP/1.1',
                 '_content' => '<html><head><title>Object moved</title></head><body>
<h2>Object moved to <a href="%2ferror%2f404">here</a>.</h2>
</body></html>
',
                 '_rc' => '302',
                 '_headers' => bless( {
                                        'x-powered-by' => 'ASP.NET',
                                        'client-response-num' => 1,
                                        'location' => '/error/404',
                                        'cache-control' => 'private',
                                        'date' => 'Tue, 13 Dec 2011 19:43:56 GMT',
                                        'client-peer' => '204.12.127.197:80',
                                        'x-aspnet-version' => '2.0.50727',
                                        'client-date' => 'Tue, 13 Dec 2011 19:36:45 GMT',
                                        'x-aspnetmvc-version' => '2.0',
                                        'content-type' => 'text/html; charset=utf-8',
                                        'title' => 'Object moved',
                                        'client-transfer-encoding' => [
                                                                        'chunked'
                                                                      ],
                                        'server' => 'Microsoft-IIS/7.5'
                                      }, 'HTTP::Headers' ),
                 '_msg' => 'Found',
                 '_request' => bless( {
                                        '_content' => '{"EventGivingGroupId":1476950,"TotalRaised":"190776.020000","PageIsExpired":true,"PageNumber":2,"PageSize":50}',
                                        '_uri' => bless( do{\(my $o = 'http://www.firstgiving.com/ProfileWebApi/Donations')}, 'URI::http' ),
                                        '_headers' => bless( {
                                                               'cookie2' => '$Version="1"',
                                                               'user-agent' => 'Opera/9.80 (Windows NT 6.1; U; Edition United Kingdom Local; en) Presto/2.10.229 Version/11.60',
                                                               'cookie' => 'ASP.NET_SessionId=yynhqi2udtz4y055fakdvjiu',
                                                               'x-requested-with' => 'XMLHttpRequest',
                                                               'accept-encoding' => 'gzip, deflate',
                                                               'content-transfer-encoding' => 'binary',
                                                               'content-type:' => 'application/json',
                                                               'referer' => 'http://www.firstgiving.com/fundraiser/r-atheism/ratheism'
                                                             }, 'HTTP::Headers' ),
                                        '_method' => 'POST',
                                        '_uri_canonical' => $VAR1->{'_request'}{'_uri'}
                                      }, 'HTTP::Request' )
               }, 'HTTP::Response' );

回線を有効にするとpush @{ $ua->requests_redirectable }, 'POST';(つまり、POSTのリダイレクトを許可すると)、404エラーページにリダイレクトされます

これが人間以外のクライアントを締め出すためのFirstGivingによる意図的な試みである場合、私はもちろんあきらめますが、彼らのrobots.txtは私がしていることを禁止していないようです。

4

1 に答える 1

2

Accept: application/json, text/javascript, */*; q=0.01ヘッダーを追加します。通常は重要だと思うヘッダーではありませんが、この場合は重要なようです。

を使用して簡単なテストを行いcurlました。これはうまくいきました:

curl -vv -H 'Content-Type: application/json' \
  -H 'Referer: http://www.firstgiving.com/fundraiser/r-atheism/ratheism' \
  -H 'Cookie: ASP.NET_SessionId=svqlde45h0cvrv55hqvhwv55;' \
  -H 'X-Requested-With: XMLHttpRequest' \
  -H 'Accept: application/json, text/javascript, */*; q=0.01' \
  -d '{"EventGivingGroupId":1476950,"TotalRaised":"191532.480000","PageIsExpired":false,"PageNumber":2,"PageSize":50}' \
  'http://www.firstgiving.com/ProfileWebApi/Donations'

これは私にリダイレクトを与えました:

curl -vv -H 'Content-Type: application/json' \
  -H 'Referer: http://www.firstgiving.com/fundraiser/r-atheism/ratheism' \
  -H 'Cookie: ASP.NET_SessionId=svqlde45h0cvrv55hqvhwv55;' \
  -H 'X-Requested-With: XMLHttpRequest' \
  -d '{"EventGivingGroupId":1476950,"TotalRaised":"191532.480000","PageIsExpired":false,"PageNumber":2,"PageSize":50}' \
  'http://www.firstgiving.com/ProfileWebApi/Donations'
于 2011-12-13T22:39:57.183 に答える