Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ru.nickthered37.jarvisthebot;
- import java.util.ArrayDeque;
- import java.util.Comparator;
- import java.util.NoSuchElementException;
- import java.util.PriorityQueue;
- import java.util.Queue;
- import java.util.concurrent.TimeUnit;
- //import com.google.common.collect.ImmutableMultimap;
- //import com.google.common.collect.UnmodifiableIterator;
- import net.sourceforge.jwbf.core.actions.HttpActionClient;
- import net.sourceforge.jwbf.core.contentRep.Article;
- import net.sourceforge.jwbf.mediawiki.bots.MediaWikiBot;
- //import net.sourceforge.jwbf.core.actions.RequestBuilder;
- //import net.sourceforge.jwbf.core.contentRep.Article;
- //import net.sourceforge.jwbf.mediawiki.ApiRequestBuilder;
- //import net.sourceforge.jwbf.mediawiki.bots.MediaWikiBot;
- public class Main {
- public static class UploadThread extends Thread {
- public UploadThread() {
- super();
- // TODO Auto-generated constructor stub
- }
- public UploadThread(Runnable target, String name) {
- super(target, name);
- // TODO Auto-generated constructor stub
- }
- public UploadThread(Runnable target) {
- super(target);
- // TODO Auto-generated constructor stub
- }
- public UploadThread(String name) {
- super(name);
- // TODO Auto-generated constructor stub
- }
- public UploadThread(ThreadGroup group, Runnable target, String name,
- long stackSize) {
- super(group, target, name, stackSize);
- // TODO Auto-generated constructor stub
- }
- public UploadThread(ThreadGroup group, Runnable target, String name) {
- super(group, target, name);
- // TODO Auto-generated constructor stub
- }
- public UploadThread(ThreadGroup group, Runnable target) {
- super(group, target);
- // TODO Auto-generated constructor stub
- }
- public UploadThread(ThreadGroup group, String name) {
- super(group, name);
- // TODO Auto-generated constructor stub
- }
- private boolean active = false;
- public boolean isActive() {
- return active;
- }
- public void wakeUp() {
- active = true;
- }
- public void run() {
- active = true;
- while (true) {
- try {
- synchronized (this) {
- if (!active)
- wait();
- }
- while (active) {
- // Retrieve the page from queue
- try {
- Article page = pageQueue.remove();
- // upload
- System.out.print("Saving “" + page.getTitle() + "”...");
- page.save();
- System.out.println("done");
- } catch (NoSuchElementException e) {
- // turn the thread off
- System.out.println("Stopping upload thread");
- active = false;
- }
- }
- } catch (InterruptedException exc) {
- // ...
- }
- }
- }
- private Queue<Article> pageQueue = new ArrayDeque<Article>(16);
- public boolean postPage(Article page) {
- return pageQueue.offer(page);
- }
- }
- public static HttpActionClient createClient(String site) {
- return HttpActionClient.builder()
- .withUrl(site)
- .withUserAgent("JARVISTheBot/naista2002.recover@gmail.com/JWBF-3.0.0-SNAPSHOT")
- .withRequestsPerUnit(10, TimeUnit.MINUTES)
- .build();
- }
- public void run(MediaWikiBot wikiBot, UploadThread uploadThread) {
- Article page1 = new Article(wikiBot, "Участник:JARVISTheBot/Песочница");
- Article page2 = new Article(wikiBot, "Участник:JARVISTheBot/Песочница2");
- Article page3 = new Article(wikiBot, "Участник:NickTheRed37/Песочница");
- page1.setText(page1.getText().replace("Test", "Тест"));
- page1.setEditSummary("Бот: Тест");
- page2.setText(page2.getText().replace("Тест", "Test"));
- page2.setEditSummary("Бот: Тест");
- page3.setText(page3.getText().replace("ЙЦУКЕН", "QWERTY"));
- page3.setEditSummary("Бот: Тест");
- uploadThread.setDaemon(true);
- uploadThread.start();
- postPage(page1, uploadThread);
- while (uploadThread.isActive()) {}
- postPage(page2, uploadThread);
- postPage(page3, uploadThread);
- while (uploadThread.isActive()) {}
- }
- public static void main(String[] args) {
- MediaWikiBot wikiBot = new MediaWikiBot(createClient("http://minecraft-ru.gamepedia.com/"));
- wikiBot.login(ConstData.data.username.get("gamepedia"), ConstData.data.password.get("gamepedia"));
- Main bot = new Main();
- UploadThread uploadThread = new UploadThread("UploadThread");
- bot.run(wikiBot, uploadThread);
- }
- public boolean postPage (Article page, UploadThread thread) {
- System.out.println("Adding page “" + page.getTitle() + "” to the queue");
- boolean val = thread.postPage(page);
- if (!thread.isActive()) {
- System.out.println("Restarting upload thread");
- thread.wakeUp();
- notify();
- }
- return val;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement