Advertisement
Guest User

Untitled

a guest
Jul 20th, 2019
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.45 KB | None | 0 0
  1. import org.jibble.pircbot.*;
  2. import java.net.*;
  3. import java.util.Scanner;
  4. import java.io.*;
  5. import com.google.gson.*;
  6.  
  7. public class MyBot {
  8. public static void main(String[] args) throws Exception{
  9. // Now start our bot up.
  10. MyBotClass bot = new MyBotClass();
  11.  
  12. // Enable debugging output.
  13. bot.setVerbose(true);
  14.  
  15. // Connect to the IRC server.
  16. bot.connect("chat.freenode.net");
  17.  
  18. // Join the #pircbot channel.
  19. bot.joinChannel("#pircbot");
  20. }
  21. }
  22.  
  23. class MyBotClass extends PircBot{
  24.  
  25. public MyBotClass() {
  26. this.setName("Bot1");
  27. }
  28.  
  29.  
  30. public void onMessage(String channel, String sender,
  31. String login, String hostname, String message) {
  32.  
  33. String[] arrOfString = message.split("@");
  34.  
  35.  
  36. if (arrOfString[0].equalsIgnoreCase("weather")) {
  37. try {
  38. sendMessage(channel, sender + ": " + WeatherAPI.getWeatherInfo(arrOfString[1]));
  39. }
  40. catch(IOException ie) {
  41. ie.printStackTrace();
  42. }
  43. }
  44.  
  45. if (arrOfString[0].equalsIgnoreCase("stock")) {
  46. try {
  47. sendMessage(channel, sender + ": " + StockAPI.getStockInfo(arrOfString[1]));
  48. }
  49. catch(IOException ie) {
  50. ie.printStackTrace();
  51. }
  52. }
  53.  
  54. if ((arrOfString[0] == getName()) && (arrOfString[1] == "quit")) {
  55. this.quitServer();
  56. }
  57.  
  58. }
  59. }
  60.  
  61.  
  62. class WeatherAPI {
  63. public static String getWeatherInfo(String cityName) throws IOException{
  64. String myAPIurl = "https://api.openweathermap.org/data/2.5/weather?q=";
  65. String myApiToken = "&APPID=26aa1d90a24c98fad4beaac70ddbf274";
  66.  
  67. String weatherURL = myAPIurl + cityName + myApiToken;
  68. URL url = new URL(weatherURL);
  69. HttpURLConnection conn = (HttpURLConnection) url.openConnection();
  70. conn.setRequestMethod("GET");
  71. BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
  72.  
  73. String response = new String();
  74. for (String line; (line = rd.readLine()) != null; response += line) ;
  75.  
  76.  
  77.  
  78. String output = parseJsonFunction(response);
  79. return output;
  80. }
  81. //Function that will parse your JSON
  82. public static String parseJsonFunction (String json)
  83. {
  84. JsonObject object = new JsonParser().parse(json).getAsJsonObject();
  85. //System.out.print(object.toString());
  86. String cityName = object.get("name").getAsString();
  87. JsonObject main = object.getAsJsonObject("main");
  88. double temp = main.get("temp").getAsDouble();
  89. double fahr = ((9 / 5) * (temp - 273) + 32);
  90. String output = "In the city of " + cityName + " the temperature is " + fahr + " degrees fahrenheit.";
  91.  
  92. return output;
  93. }
  94. }
  95.  
  96. class StockAPI {
  97. public static String getStockInfo(String tickerSymbl) throws IOException{
  98. String myAPIurl = "https://www.alphavantage.co/query?function=GLOBAL_QUOTE&symbol=";
  99. String myApiToken = "&apikey=UELU6AL4I27DSMI4";
  100.  
  101. String stockURL = myAPIurl + tickerSymbl + myApiToken;
  102. URL url = new URL(stockURL);
  103. HttpURLConnection conn = (HttpURLConnection) url.openConnection();
  104. conn.setRequestMethod("GET");
  105. BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
  106.  
  107. String response = new String();
  108. for (String line; (line = rd.readLine()) != null; response += line) ;
  109.  
  110. String output = parseJsonFunction(response);
  111. return output;
  112. }
  113. //Function that will parse your JSON
  114. public static String parseJsonFunction (String json)
  115. {
  116. JsonObject object = new JsonParser().parse(json).getAsJsonObject();
  117.  
  118. JsonObject main = object.getAsJsonObject("Global Quote");
  119. double open = main.get("02. open").getAsDouble();
  120. double high = main.get("03. high").getAsDouble();
  121. double low = main.get("04. low").getAsDouble();
  122. String day = main.get("07. latest trading day").getAsString();
  123. String ticker = main.get("01. symbol").getAsString();
  124.  
  125. String output = "On the day of " + day + " " + ticker +
  126. " opened with a stock price of $" + open + " USD. " + ticker +
  127. " reached a high of $" + high + " USD, and a low of $" + low + " USD.";
  128. return output;
  129. }
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement