Advertisement
naista2002

Untitled

Mar 8th, 2015
323
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.72 KB | None | 0 0
  1. package ru.nickthered37.jarvisthebot;
  2.  
  3. import java.util.ArrayDeque;
  4. import java.util.Comparator;
  5. import java.util.NoSuchElementException;
  6. import java.util.PriorityQueue;
  7. import java.util.Queue;
  8. import java.util.concurrent.TimeUnit;
  9.  
  10. //import com.google.common.collect.ImmutableMultimap;
  11. //import com.google.common.collect.UnmodifiableIterator;
  12.  
  13. import net.sourceforge.jwbf.core.actions.HttpActionClient;
  14. import net.sourceforge.jwbf.core.contentRep.Article;
  15. import net.sourceforge.jwbf.mediawiki.bots.MediaWikiBot;
  16. //import net.sourceforge.jwbf.core.actions.RequestBuilder;
  17. //import net.sourceforge.jwbf.core.contentRep.Article;
  18. //import net.sourceforge.jwbf.mediawiki.ApiRequestBuilder;
  19. //import net.sourceforge.jwbf.mediawiki.bots.MediaWikiBot;
  20.  
  21. public class Main {
  22.  
  23.     public static class UploadThread extends Thread {
  24.        
  25.  
  26.         public UploadThread() {
  27.             super();
  28.             // TODO Auto-generated constructor stub
  29.         }
  30.  
  31.         public UploadThread(Runnable target, String name) {
  32.             super(target, name);
  33.             // TODO Auto-generated constructor stub
  34.         }
  35.  
  36.         public UploadThread(Runnable target) {
  37.             super(target);
  38.             // TODO Auto-generated constructor stub
  39.         }
  40.  
  41.         public UploadThread(String name) {
  42.             super(name);
  43.             // TODO Auto-generated constructor stub
  44.         }
  45.  
  46.         public UploadThread(ThreadGroup group, Runnable target, String name,
  47.                 long stackSize) {
  48.             super(group, target, name, stackSize);
  49.             // TODO Auto-generated constructor stub
  50.         }
  51.  
  52.         public UploadThread(ThreadGroup group, Runnable target, String name) {
  53.             super(group, target, name);
  54.             // TODO Auto-generated constructor stub
  55.         }
  56.  
  57.         public UploadThread(ThreadGroup group, Runnable target) {
  58.             super(group, target);
  59.             // TODO Auto-generated constructor stub
  60.         }
  61.  
  62.         public UploadThread(ThreadGroup group, String name) {
  63.             super(group, name);
  64.             // TODO Auto-generated constructor stub
  65.         }
  66.        
  67.         private boolean active = false;
  68.        
  69.         public boolean isActive() {
  70.             return active;
  71.         }
  72.        
  73.         public void wakeUp() {
  74.             active = true;
  75.         }
  76.  
  77.         public void run() {
  78.             active = true;
  79.             while (true) {
  80.                 try {
  81.                     synchronized (this) {
  82.                         if (!active)
  83.                             wait();
  84.                     }
  85.                    
  86.                     while (active) {
  87.                         // Retrieve the page from queue
  88.                         try {
  89.                             Article page = pageQueue.remove();
  90.                             // upload
  91.                             System.out.print("Saving “" + page.getTitle() + "”...");
  92.                             page.save();
  93.                             System.out.println("done");
  94.                         } catch (NoSuchElementException e) {
  95.                             // turn the thread off
  96.                             System.out.println("Stopping upload thread");
  97.                             active = false;
  98.                         }
  99.                     }
  100.                 } catch (InterruptedException exc) {
  101.                     // ...
  102.                 }
  103.             }
  104.         }
  105.        
  106.         private Queue<Article> pageQueue = new ArrayDeque<Article>(16);
  107.        
  108.         public boolean postPage(Article page) {
  109.             return pageQueue.offer(page);
  110.         }
  111.     }
  112.    
  113.     public static HttpActionClient createClient(String site) {
  114.         return HttpActionClient.builder()
  115.                 .withUrl(site)
  116.                 .withUserAgent("JARVISTheBot/naista2002.recover@gmail.com/JWBF-3.0.0-SNAPSHOT")
  117.                 .withRequestsPerUnit(10, TimeUnit.MINUTES)
  118.                 .build();
  119.     }
  120.  
  121.     public void run(MediaWikiBot wikiBot, UploadThread uploadThread) {
  122.         Article page1 = new Article(wikiBot, "Участник:JARVISTheBot/Песочница");
  123.         Article page2 = new Article(wikiBot, "Участник:JARVISTheBot/Песочница2");
  124.         Article page3 = new Article(wikiBot, "Участник:NickTheRed37/Песочница");
  125.        
  126.         page1.setText(page1.getText().replace("Test", "Тест"));
  127.         page1.setEditSummary("Бот: Тест");
  128.         page2.setText(page2.getText().replace("Тест", "Test"));
  129.         page2.setEditSummary("Бот: Тест");
  130.         page3.setText(page3.getText().replace("ЙЦУКЕН", "QWERTY"));
  131.         page3.setEditSummary("Бот: Тест");
  132.        
  133.         uploadThread.setDaemon(true);
  134.         uploadThread.start();
  135.         postPage(page1, uploadThread);
  136.        
  137.         while (uploadThread.isActive()) {}
  138.        
  139.         postPage(page2, uploadThread);
  140.         postPage(page3, uploadThread);
  141.        
  142.         while (uploadThread.isActive()) {}
  143.     }
  144.    
  145.     public static void main(String[] args) {
  146.  
  147.         MediaWikiBot wikiBot = new MediaWikiBot(createClient("http://minecraft-ru.gamepedia.com/"));
  148.         wikiBot.login(ConstData.data.username.get("gamepedia"), ConstData.data.password.get("gamepedia"));
  149.        
  150.         Main bot = new Main();
  151.        
  152.         UploadThread uploadThread = new UploadThread("UploadThread");
  153.         bot.run(wikiBot, uploadThread);
  154.     }
  155.    
  156.     public boolean postPage (Article page, UploadThread thread) {
  157.         System.out.println("Adding page “" + page.getTitle() + "” to the queue");
  158.         boolean val = thread.postPage(page);
  159.        
  160.         if (!thread.isActive()) {
  161.             System.out.println("Restarting upload thread");
  162.             thread.wakeUp();
  163.             notify();
  164.         }
  165.        
  166.         return val;
  167.     }
  168. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement