Utilisateur:Ir4ubot/Journaux/2010052700
Apparence
package test;
import java.net.MalformedURLException;
import java.util.Iterator;
import orph.Orphan;
import net.sourceforge.jwbf.core.actions.util.ActionException;
import net.sourceforge.jwbf.core.actions.util.ProcessException;
import net.sourceforge.jwbf.mediawiki.actions.MediaWiki;
import net.sourceforge.jwbf.mediawiki.actions.queries.CategoryMembersSimple;
import net.sourceforge.jwbf.mediawiki.bots.MediaWikiBot;
public class Basic {
public static void main(String[] args){
MediaWikiBot b;
try {
b = new MediaWikiBot("http://fr.wikipedia.org/w/");
try {
b.login("MyBot", "***");
CategoryMembersSimple cmsSubcategories;
try {
cmsSubcategories = new CategoryMembersSimple(b, "Article orphelin", MediaWiki.NS_CATEGORY);
Iterator<String> iteratorArticles=cmsSubcategories.iterator();
while(iteratorArticles.hasNext())
{
CategoryMembersSimple cat= new CategoryMembersSimple(b, iteratorArticles.next().replaceAll("Catégorie:", ""), MediaWiki.NS_MAIN);
Iterator<String> itCat = cat.iterator();
while(itCat.hasNext())
{
Orphan o = new Orphan(b, itCat.next());
if(!o.isOrphan(b))
{
o.removeOrphanTag(b);
}
}
}
} catch (ProcessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
catch (ActionException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/////////////////////////////////////
package orph;
import java.io.IOException;
import org.apache.commons.httpclient.HttpException;
import winterwell.jtwitter.Twitter;
import net.sourceforge.jwbf.core.actions.util.ActionException;
import net.sourceforge.jwbf.core.actions.util.ProcessException;
import net.sourceforge.jwbf.core.contentRep.Article;
import net.sourceforge.jwbf.mediawiki.actions.MediaWiki;
import net.sourceforge.jwbf.mediawiki.actions.queries.BacklinkTitles;
import net.sourceforge.jwbf.mediawiki.actions.util.RedirectFilter;
import net.sourceforge.jwbf.mediawiki.actions.util.VersionException;
import net.sourceforge.jwbf.mediawiki.bots.MediaWikiBot;
public class Orphan extends Article {
public Orphan(MediaWikiBot bot, String sa) {
super(bot, sa);
}
public boolean isOrphan(MediaWikiBot _bot)
{
int _iCountBacklink=0;
_iCountBacklink=getCountBacklinkTitles(_bot, this.getTitle());
if (_iCountBacklink>=3)
return false;
else
return true;
}
public int getCountBacklinkTitles(MediaWikiBot _bot, String s)
{
BacklinkTitles bl;
int iBacklinks = 0;
try {
bl = new BacklinkTitles(_bot, s, RedirectFilter.nonredirects, MediaWiki.NS_MAIN);
while(bl.hasNext())
{
iBacklinks++;
System.out.println(bl.next()+"→ "+iBacklinks);
}
} catch (VersionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return iBacklinks;
}
public void removeOrphanTag(MediaWikiBot b)
{
// TODO Auto-generated method stub
String strText=this.getText();
strText=strText.replaceAll("\\{\\{([Aa]rticle orphelin|[oO]rphelin|[Oo]rphan)\\|date=(janvier|février|mars|avril|mai|juin|juillet|août|septembre|octobre|novembre|décembre|) (2007|2008|2009|2010|)\\}\\}", "");
this.setEditSummary(this.getTitle()+" n'est plus orphelin");
this.setText(strText);
this.sendTweet(strText);
try {
this.save();
} catch (ActionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ProcessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void sendTweet(String _strText)
{
// Make a Twitter object
Twitter twitter = new Twitter("MyBot","***");
_strText=_strText.replaceAll("(\\[\\[\\w\\]\\])", "\\\\#$1");
_strText=_strText.replaceAll("\\]\\]", "");
_strText=_strText.replaceAll("\\[\\[", "");
_strText=_strText.replaceAll("'''", "");
_strText=_strText.replaceAll("''", "");
_strText=_strText.substring(0, 110);
_strText=_strText+"…";
String tinyUrl;
try {
tinyUrl = TinyURLUtils.getTinyUrl("http://fr.wikipedia.org/wiki/"+this.getTitle());
System.out.println(tinyUrl); // --> http://tinyurl.com/5cporq
twitter.setStatus(_strText+" "+tinyUrl);
} catch (HttpException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
////////////////////////////////////////////////////////
package orph;
import java.io.IOException;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.GetMethod;
public abstract class TinyURLUtils {
public static String getTinyUrl(String fullUrl) throws HttpException, IOException {
HttpClient httpclient = new HttpClient();
// Prepare a request object
HttpMethod method = new GetMethod("http://tinyurl.com/api-create.php");
method.setQueryString(new NameValuePair[]{new NameValuePair("url",fullUrl)});
httpclient.executeMethod(method);
String tinyUrl = method.getResponseBodyAsString();
method.releaseConnection();
return tinyUrl;
}
}
/////////////////////////////////////////////////