跳转到内容

用户: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页面,有时候会访问失败。