跳至內容

用戶:Paoting/英語維基百科特色條目/腳本

維基百科,自由的百科全書

用到的API

在Wikidata中用英語維基(enwiki)條目名`DNA'進行檢索 得到中文維基(zhwiki)的結果:

"zhwiki": {
    "site": "zhwiki",
    "title": "\u8131\u6c27\u6838\u7cd6\u6838\u9178",
    "badges": [
        "Q17437796"
    ]
}

在獎章(badges)中,Q17437796代表特色條目,Q17437798代表優良條目。

代碼

#!/usr/bin/perl

use Encode;
use utf8::all;
use LWP::Simple;

my $FA_list_html = get 'https://en.wikipedia.org/w/index.php?title=Wikipedia:Featured_articles&action=edit';
my ($FA_list_wiki) = ($FA_list_html =~ m/(==Art, .*){{WikiProject cleanup/s);

for my $line (split "\n", $FA_list_wiki) {
  $line =~ s/{{FA\/BeenOnMainPage\|(.*)}}/$1/;
  if ($line =~ m/(\|[^\]]+)]]/) {
    $line =~ s/\[\[/[[:en:/g;
  } else {
    $line =~ s/\[\[([^\]]+)/[[:en:$1|$1/g;
  }
  if ($line =~ m/:en:(.*)\|/) {
    my $article_name = $1;
    my $api_url = 'https://www.wikidata.org/w/api.php?action=wbgetentities&format=json&utf8'
                  . '&sites=enwiki&titles=' . $article_name;
    my $json_text = get $api_url;
    if ($json_text =~ m/{"site":"zhwiki","title":"([^"]+)","badges":\[([^\]]*)\]}/) {
      my $zh_title = decode_utf8($1);
      if ($2) {
        if ($2 =~ m/Q17437796/) {
          $line .= "<small>({{Faico}} [[$zh_title]])</small>";
        } else {
          $line .= "<small>({{Gaico}} [[$zh_title]])</small>";
        }
      } else {
        $line .= "<small>([[$zh_title]])</small>";
      }
    } else {
      $line .= "<small>([[$article_name]])</small>"
    }
  }
  print $line, "\n";
}

測試片段

==Art, architecture, and archaeology==
{{FA/BeenOnMainPage|[[Statue of Liberty]]}}
· {{FA/BeenOnMainPage|[[Four Freedoms (Norman Rockwell)|''Four Freedoms'' (Norman Rockwell)]]}}
· [[Andean condor]]
· [[Alpine chough]]

{{WikiProject cleanup listing|Wikipedia:Featured articles}}

結果

==Art, architecture, and archaeology==
[[:en:Statue of Liberty|Statue of Liberty]]<small>({{Faico}} [[自由女神像]])</small>
· [[:en:Four Freedoms (Norman Rockwell)|''Four Freedoms'' (Norman Rockwell)]]<small>([[Four Freedoms (Norman Rockwell)]])</small>
· [[:en:Andean condor|Andean condor]]<small>({{Gaico}} [[安地斯神鷹]])</small>
· [[:en:Alpine chough|Alpine chough]]<small>([[黄嘴山鸦]])</small>

Statue of Liberty( 自由女神像) · Four Freedoms (Norman Rockwell)(Four Freedoms (Norman Rockwell)) · Andean condor( 安地斯神鷹) · Alpine chough(黃嘴山鴉)

Bug

因為需要訪問4684次WikidataAPI頁面,有時候會訪問失敗。