public static void stringToFile(String filepath, String s) {
    /* creating empty file */
    File file = null;
    try {
      /* create new file */
      file = new File(filepath);
      file.createNewFile();
      boolean goodFile = file.canWrite();
      if (!goodFile) {
        System.out.println("can't make new file...");
        return;
      }
    } catch (Exception e) {
      e.printStackTrace();
      return;
    }

    /* prepare for writing to the file */
    PrintWriter writer;
    try {
      writer = new PrintWriter(filepath, "UTF-8");
    } catch (Exception e) {
      e.printStackTrace();
      return;
    }
    writer.write(s);
    // String string = JSON.stringify(mixnet); /* javascript :( */
    writer.close();
    /* in JAVA we can't close file. or open it. */
  }
Esempio n. 2
0
 public StockQuote getStockQuote(String ticker) {
   StockQuote quote = new StockQuote();
   if (!ticker.isEmpty()) {
     System.out.println("Getting data for: " + ticker);
     try {
       String queryUrl = BASE_URL + YQL + "'" + ticker + "'" + FORMAT;
       URL url = new URL(queryUrl);
       System.out.println(queryUrl);
       URLConnection yc = url.openConnection();
       BufferedReader in = new BufferedReader(new InputStreamReader(yc.getInputStream()));
       String inputLine = "";
       String buffer = "";
       while ((inputLine = in.readLine()) != null) {
         buffer += inputLine;
       }
       try {
         System.out.println("Called at initialization: " + inputLine);
         JSONObject result = new JSONObject(buffer);
         System.out.println("Received Data: " + result);
       } catch (Exception e) {
         e.printStackTrace();
       }
       in.close();
     } catch (Exception e) {
       e.printStackTrace();
     }
   }
   return quote;
 }
Esempio n. 3
0
  // Lee la configuracion que se encuentra en conf/RegistryConf
  private void readConfXml() {
    try {
      File inputFile = new File("src/java/Conf/RegistryConf.xml");
      DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
      DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
      Document doc = dBuilder.parse(inputFile);
      doc.getDocumentElement().normalize();
      NodeList nList = doc.getElementsByTagName("RegistryConf");
      for (int i = 0; i < nList.getLength(); i++) {
        Node nNode = nList.item(i);
        if (nNode.getNodeType() == Node.ELEMENT_NODE) {
          Element eElement = (Element) nNode;
          RegistryConf reg = new RegistryConf();
          String aux;
          int idSensor;
          int value;
          aux = eElement.getElementsByTagName("idSensor").item(0).getTextContent();
          idSensor = Integer.parseInt(aux);
          reg.setIdSensor(idSensor);

          aux = eElement.getElementsByTagName("saveType").item(0).getTextContent();
          reg.setSaveTypeString(aux);

          aux = eElement.getElementsByTagName("value").item(0).getTextContent();
          value = Integer.parseInt(aux);
          reg.setValue(value);

          registryConf.add(reg);
          lastRead.put(idSensor, 0);
        }
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
 public void markLastRead() {
   try {
     if (mLastReadUrl != null) {
       NetworkUtils.get(Constants.BASE_URL + mLastReadUrl);
     }
   } catch (Exception e) {
     e.printStackTrace();
   }
 }
Esempio n. 5
0
 private boolean is200(String url) {
   if (DEBUG) log("is200() :: url=%s", url);
   HttpURLConnection conn = null;
   try {
     conn = (HttpURLConnection) new URL(url).openConnection();
     return conn.getResponseCode() == 200;
   } catch (Exception ex) {
     if (DEBUG) ex.printStackTrace();
     return false;
   } finally {
     if (conn != null)
       try {
         conn.disconnect();
       } catch (Exception ex) {
         if (DEBUG) ex.printStackTrace();
       }
   }
 }
Esempio n. 6
0
 /**
  * Returns the configuration object found at the requested path.
  *
  * @param path The search path
  * @return The config value
  * @throws ConfigException thrown if nothing is found at the path
  */
 public Object get(String path) throws ConfigException {
   try {
     Object obj = Utils.findPath(configObject, context + path);
     if (obj == null) throw new ConfigException("");
     return obj;
   } catch (Exception e) {
     e.printStackTrace();
     throw new ConfigException("No config object found at " + path);
   }
 }
Esempio n. 7
0
  public void onReceive(ByteBuffer b) {
    if (b.remaining() == 4) {
      Common.logError("invalid packet:");
      byte[] data = new byte[b.remaining()];
      b.get(data);
      Common.print_dump(data);
      //	retry( TLMessage.keyAt(  TLMessage.size() - 1) );
      return;
    }

    try {
      if (b.getLong() == 0) { // auth_key_id
        b.getLong(); // message_id
        b.getInt(); // message length
        process(TL.deserialize(b));
      } else {
        byte[] msg_key = new byte[16];
        b.get(msg_key);
        byte[] data = new byte[b.remaining()];
        b.get(data);

        synchronized (aes) {
          aes.prepare(false, msg_key, auth_key);
          data = aes.IGE(data, false);
        }

        ByteBuffer btmp = ByteBuffer.wrap(data);
        btmp.order(ByteOrder.LITTLE_ENDIAN);
        server_salt = btmp.getLong();
        btmp.getLong(); // session_id
        cur_message_id = btmp.getLong(); // message_id
        cur_msg_seq = btmp.getInt(); // seq_no
        // if (cur_msg_seq > seqno)
        //	seqno = cur_msg_seq + cur_msg_seq % 2;
        int bsize = btmp.getInt();
        if (bsize < 0 || bsize > 1024 * 1024) {
          Common.print_dump(btmp.array());
          Common.logError(new String(btmp.array()));
          Common.logError("FFFUUUUUUUUUUU!!!");
        }

        b = BufferAlloc(bsize);
        btmp.get(b.array());
        b.getInt();
        ack_message(cur_msg_seq, cur_message_id);
        b.position(0);

        process(TL.deserialize(b));
        send_accept();
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
Esempio n. 8
0
 // Guarda registro en la BD
 public boolean saveRegistry(int idSensor, int value) {
   try {
     Registry r = new Registry();
     r.setIdsensor(idSensor);
     r.setValue(value);
     r.setDate(new Date());
     Session session = HibernateUtil.getSessionFactory().openSession();
     Transaction tx = session.beginTransaction();
     session.save(r);
     tx.commit();
     session.close();
     return true;
   } catch (Exception e) {
     e.printStackTrace();
     return false;
   }
 }
Esempio n. 9
0
  // El string debe estar em formato json
  public boolean updateRegistry(String dataJson) {
    try {
      // Crea objeto json con string por parametro
      JSONObject json = new JSONObject(dataJson);
      // Obtiene el array de Registos
      JSONArray arr = json.getJSONArray("Registry");
      // Recorre el array
      for (int i = 0; i < arr.length(); i++) {
        // Obtiene los datos idSensor y value
        int idSensor = arr.getJSONObject(i).getInt("idSensor");

        int value = arr.getJSONObject(i).getInt("value");
        // Recorre la configuracion de registro
        for (RegistryConf reg : registryConf) {

          // Se fija si el registro corresponde a esta configuracion
          if (reg.getIdSensor() == idSensor) {

            // Checkea el criterio para guardar, o no en la BD
            // Checkea tambien si el valor es igual al anterior
            if (reg.getSaveTypeString() == "ONCHANGE" && lastRead.get(idSensor) != value) {
              // Actualizo la ultima lectura y guardo en la BD
              lastRead.put(idSensor, value);
              saveRegistry(idSensor, value);

            } else if (reg.getSaveTypeString() == "ONTIME") {
              // Variables auxiliares, para checkear tiempo
              Long auxLong = System.currentTimeMillis() / 1000;
              int now = auxLong.intValue();
              int timeToSave = lastRead.get(idSensor) + reg.getValue();
              // Checkea si ya es tiempo para guerdar un nuevo registro
              if (now >= timeToSave) {
                // Actualizo el ultimo guardado
                lastRead.put(idSensor, now);
                saveRegistry(idSensor, value);
              }
            }
          }
        }
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
    return false;
  }
Esempio n. 10
0
  public void invoke(String name, TL.Object obj) {
    Method m;
    try {
      m = MTProto.class.getDeclaredMethod("TL_" + name.replace(".", "_"), TL.Object.class);
      m.setAccessible(true);
    } catch (Exception e) {
      Common.logWarning("not handled: Main::" + name);
      return;
    }

    try {
      Common.logInfo("invoke: Main::" + name);
      m.invoke(this, obj);
    } catch (Exception e) {
      Common.logError("invoke error in Main");
      e.printStackTrace();
    }
  }
Esempio n. 11
0
  private void process(TL.Object obj) {
    //		if (obj instanceof TL.Vector)
    //			return;
    Method m;
    try {
      m = MTProto.class.getDeclaredMethod("TL_" + obj.type.replace(".", "_"), TL.Object.class);
      m.setAccessible(true);
    } catch (Exception e) {
      Common.logWarning("not handled: " + obj.type + " (" + obj.name + ")");
      return;
    }

    try {
      Common.logInfo("invoke: " + obj.type + " (" + obj.name + ")");
      m.invoke(this, obj);
    } catch (Exception e) {
      Common.logError("process error");
      e.printStackTrace();
    }
  }
  public static void main(String args[]) {
    if (args.length < 1) {
      System.err.println(
          "Usage: java org.globusonline.transfer.ErrorTest "
              + "username [cafile certfile keyfile [baseurl]]]");
      System.exit(1);
    }
    String username = args[0];

    String cafile = null;
    if (args.length > 1 && args[1].length() > 0) cafile = args[1];

    String certfile = null;
    if (args.length > 2 && args[2].length() > 0) certfile = args[2];

    String keyfile = null;
    if (args.length > 3 && args[3].length() > 0) keyfile = args[3];

    String baseUrl = null;
    if (args.length > 4 && args[4].length() > 0) baseUrl = args[4];

    try {
      JSONTransferAPIClient c =
          new JSONTransferAPIClient(username, cafile, certfile, keyfile, baseUrl);
      System.out.println("base url: " + c.getBaseUrl());
      ErrorTest et = new ErrorTest(c);
      et.run();

      // test auth error
      System.out.println("=== 400 Auth Failed ===");
      c = new JSONTransferAPIClient(username, cafile, null, null, baseUrl);
      try {
        c.getResult("/tasksummary");
      } catch (APIError e) {
        e.printStackTrace();
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
Esempio n. 13
0
  protected void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {

    System.out.println("HI From data supply manager");

    String app = (String) request.getParameter("app");
    String sdate = (String) request.getParameter("sdate");
    String edate = (String) request.getParameter("edate");
    String typeOfChart = (String) request.getParameter("typeOfChart");
    String typeofReport = (String) request.getParameter("typeOfReport");

    System.out.println(
        "In data supply : App name : "
            + app
            + "  sdate  : "
            + sdate
            + " edate : "
            + edate
            + " type of chart  : "
            + typeOfChart
            + " type of report :"
            + typeofReport);

    Main mainthread = new Main();

    JSONArray htags = new JSONArray();

    try {
      htags.put(mainthread.getTickets(typeOfChart, app, typeofReport, sdate, edate));
    } catch (Exception e) {

      e.printStackTrace();
    }
    response.setContentType("application/json");
    response.setCharacterEncoding("utf-8");
    PrintWriter out = response.getWriter();
    out.print(htags);
  }
Esempio n. 14
0
  private void notifyUser(Object message) {
    try {
      if (message instanceof JSONObject) {
        final JSONObject obj = (JSONObject) message;
        this.runOnUiThread(
            new Runnable() {
              public void run() {
                Toast.makeText(getApplicationContext(), obj.toString(), Toast.LENGTH_LONG).show();

                Log.i("Received msg : ", String.valueOf(obj));
              }
            });

      } else if (message instanceof String) {
        final String obj = (String) message;
        this.runOnUiThread(
            new Runnable() {
              public void run() {
                Toast.makeText(getApplicationContext(), obj, Toast.LENGTH_LONG).show();
                Log.i("Received msg : ", obj.toString());
              }
            });

      } else if (message instanceof JSONArray) {
        final JSONArray obj = (JSONArray) message;
        this.runOnUiThread(
            new Runnable() {
              public void run() {
                Toast.makeText(getApplicationContext(), obj.toString(), Toast.LENGTH_LONG).show();
                Log.i("Received msg : ", obj.toString());
              }
            });
      }

    } catch (Exception e) {
      e.printStackTrace();
    }
  }
Esempio n. 15
0
  private boolean saveAndVerify(ZipInputStream data) throws IOException {
    try {
      ZipEntry ze;
      while ((ze = data.getNextEntry()) != null) {
        // Filename + reference to file.
        String filename = ze.getName();
        File output = new File(local_path + filename);

        if (filename.endsWith("/")) {
          output.mkdirs();
        } else {
          if (output.exists()) {
            // Delete the file if it already exists.
            if (!output.delete()) {
              return false;
            }
          }
          if (output.createNewFile()) {
            FileOutputStream out = new FileOutputStream(output);
            byte[] buffer = new byte[1024];
            int count;
            while ((count = data.read(buffer)) != -1) {
              out.write(buffer, 0, count);
            }
          } else {
            return false;
          }
        }
      }
    } catch (Exception e) {
      e.printStackTrace();
      return false;
    } finally {
      data.close();
    }
    return true;
  }
  private static TagNode convertVideos(TagNode contentNode) {
    TagNode[] videoNodes = contentNode.getElementsByAttValue("class", "bbcode_video", true, true);
    TagNode[] youtubeNodes =
        contentNode.getElementsByAttValue("class", "youtube-player", true, true);
    for (TagNode youTube : youtubeNodes) {
      String src = youTube.getAttributeByName("src");
      int height = Integer.parseInt(youTube.getAttributeByName("height"));
      int width = Integer.parseInt(youTube.getAttributeByName("width"));
      Matcher youtube = youtubeHDId_regex.matcher(src);
      if (youtube.find()) {
        String videoId = youtube.group(1);
        String link = "http://www.youtube.com/watch?v=" + videoId;
        String image = "http://img.youtube.com/vi/" + videoId + "/0.jpg";
        youTube.setName("a");
        youTube.setAttribute("href", link);
        youTube.removeAttribute("type");
        youTube.removeAttribute("frameborder");
        youTube.removeAttribute("src");
        youTube.removeAttribute("height");
        youTube.removeAttribute("width");
        youTube.setAttribute(
            "style",
            "background-image:url("
                + image
                + ");background-size:cover;background-repeat:no-repeat;background-position:center; position:relative;display:block;text-align:center; width:"
                + width
                + "; height:"
                + height);
        TagNode img = new TagNode("img");
        img.setAttribute("class", "nolink videoPlayButton");
        img.setAttribute("src", "file:///android_res/drawable/ic_menu_video.png");
        img.setAttribute(
            "style", "position:absolute;top:50%;left:50%;margin-top:-16px;margin-left:-16px;");
        youTube.addChild(img);
      }
    }
    for (TagNode node : videoNodes) {
      try {
        String src = null;
        int height = 0;
        int width = 0;
        TagNode[] object = node.getElementsByName("object", false);
        if (object.length > 0) {
          height = Integer.parseInt(object[0].getAttributeByName("height"));
          width = Integer.parseInt(object[0].getAttributeByName("width"));
          TagNode[] emb = object[0].getElementsByName("embed", true);
          if (emb.length > 0) {
            src = emb[0].getAttributeByName("src");
          }
        }
        if (src != null && height != 0 && width != 0) {
          String link = null, image = null;
          Matcher youtube = youtubeId_regex.matcher(src);
          Matcher vimeo = vimeoId_regex.matcher(src);
          if (youtube
              .find()) { // we'll leave in the old youtube code in case something gets reverted
            String videoId = youtube.group(1);
            link = "http://www.youtube.com/watch?v=" + videoId;
            image = "http://img.youtube.com/vi/" + videoId + "/0.jpg";
          } else if (vimeo.find()) {
            String videoId = vimeo.group(1);
            TagNode vimeoXML;
            try {
              vimeoXML = NetworkUtils.get("http://vimeo.com/api/v2/video/" + videoId + ".xml");
            } catch (Exception e) {
              e.printStackTrace();
              continue;
            }
            if (vimeoXML.findElementByName("mobile_url", true) != null) {
              link = vimeoXML.findElementByName("mobile_url", true).getText().toString();
            } else {
              link = vimeoXML.findElementByName("url", true).getText().toString();
            }
            image = vimeoXML.findElementByName("thumbnail_large", true).getText().toString();
          } else {
            node.removeAllChildren();
            TagNode ln = new TagNode("a");
            ln.setAttribute("href", src);
            ln.addChild(new ContentNode(src));
            node.addChild(ln);
            continue;
          }
          node.removeAllChildren();
          node.setAttribute(
              "style",
              "background-image:url("
                  + image
                  + ");background-size:cover;background-repeat:no-repeat;background-position:center; position:relative;text-align:center; width:"
                  + width
                  + "; height:"
                  + height);
          node.setAttribute("onclick", "location.href=\"" + link + "\"");
          TagNode img = new TagNode("img");
          img.setAttribute("class", "nolink videoPlayButton");
          img.setAttribute("src", "file:///android_res/drawable/ic_menu_video.png");
          img.setAttribute(
              "style", "position:absolute;top:50%;left:50%;margin-top:-23px;margin-left:-32px;");
          node.addChild(img);
        }
      } catch (Exception e) {
        continue; // if we fail to convert the video tag, we can still display the rest.
      }
    }

    return contentNode;
  }
  public void doProcess(HttpServletRequest req, HttpServletResponse res, boolean isPost) {
    StringBuffer bodyContent = null;
    OutputStream out = null;
    PrintWriter writer = null;
    String serviceKey = null;

    try {
      BufferedReader in = req.getReader();
      String line = null;
      while ((line = in.readLine()) != null) {
        if (bodyContent == null) bodyContent = new StringBuffer();
        bodyContent.append(line);
      }
    } catch (Exception e) {
    }
    try {
      if (requireSession) {
        // check to see if there was a session created for this request
        // if not assume it was from another domain and blow up
        // Wrap this to prevent Portlet exeptions
        HttpSession session = req.getSession(false);
        if (session == null) {
          res.setStatus(HttpServletResponse.SC_FORBIDDEN);
          return;
        }
      }
      serviceKey = req.getParameter("id");
      // only to preven regressions - Remove before 1.0
      if (serviceKey == null) serviceKey = req.getParameter("key");
      // check if the services have been loaded or if they need to be reloaded
      if (services == null || configUpdated()) {
        getServices(res);
      }
      String urlString = null;
      String xslURLString = null;
      String userName = null;
      String password = null;
      String format = "json";
      String callback = req.getParameter("callback");
      String urlParams = req.getParameter("urlparams");
      String countString = req.getParameter("count");
      // encode the url to prevent spaces from being passed along
      if (urlParams != null) {
        urlParams = urlParams.replace(' ', '+');
      }

      try {
        if (services.has(serviceKey)) {
          JSONObject service = services.getJSONObject(serviceKey);
          // default to the service default if no url parameters are specified
          if (urlParams == null && service.has("defaultURLParams")) {
            urlParams = service.getString("defaultURLParams");
          }
          String serviceURL = service.getString("url");
          // build the URL
          if (urlParams != null && serviceURL.indexOf("?") == -1) {
            serviceURL += "?";
          } else if (urlParams != null) {
            serviceURL += "&";
          }
          String apikey = "";
          if (service.has("username")) userName = service.getString("username");
          if (service.has("password")) password = service.getString("password");
          if (service.has("apikey")) apikey = service.getString("apikey");
          urlString = serviceURL + apikey;
          if (urlParams != null) urlString += "&" + urlParams;
          if (service.has("xslStyleSheet")) {
            xslURLString = service.getString("xslStyleSheet");
          }
        }
        // code for passing the url directly through instead of using configuration file
        else if (req.getParameter("url") != null) {
          String serviceURL = req.getParameter("url");
          // build the URL
          if (urlParams != null && serviceURL.indexOf("?") == -1) {
            serviceURL += "?";
          } else if (urlParams != null) {
            serviceURL += "&";
          }
          urlString = serviceURL;
          if (urlParams != null) urlString += urlParams;
        } else {
          writer = res.getWriter();
          if (serviceKey == null)
            writer.write("XmlHttpProxyServlet Error: id parameter specifying serivce required.");
          else
            writer.write(
                "XmlHttpProxyServlet Error : service for id '" + serviceKey + "' not  found.");
          writer.flush();
          return;
        }
      } catch (Exception ex) {
        getLogger().severe("XmlHttpProxyServlet Error loading service: " + ex);
      }

      Map paramsMap = new HashMap();
      paramsMap.put("format", format);
      // do not allow for xdomain unless the context level setting is enabled.
      if (callback != null && allowXDomain) {
        paramsMap.put("callback", callback);
      }
      if (countString != null) {
        paramsMap.put("count", countString);
      }

      InputStream xslInputStream = null;

      if (urlString == null) {
        writer = res.getWriter();
        writer.write(
            "XmlHttpProxyServlet parameters:  id[Required] urlparams[Optional] format[Optional] callback[Optional]");
        writer.flush();
        return;
      }
      // default to JSON
      res.setContentType(responseContentType);
      out = res.getOutputStream();
      // get the stream for the xsl stylesheet
      if (xslURLString != null) {
        // check the web root for the resource
        URL xslURL = null;
        xslURL = ctx.getResource(resourcesDir + "xsl/" + xslURLString);
        // if not in the web root check the classpath
        if (xslURL == null) {
          xslURL =
              XmlHttpProxyServlet.class.getResource(classpathResourcesDir + "xsl/" + xslURLString);
        }
        if (xslURL != null) {
          xslInputStream = xslURL.openStream();
        } else {
          String message =
              "Could not locate the XSL stylesheet provided for service id "
                  + serviceKey
                  + ". Please check the XMLHttpProxy configuration.";
          getLogger().severe(message);
          try {
            out.write(message.getBytes());
            out.flush();
            return;
          } catch (java.io.IOException iox) {
          }
        }
      }
      if (!isPost) {
        xhp.doGet(urlString, out, xslInputStream, paramsMap, userName, password);
      } else {
        if (bodyContent == null)
          getLogger()
              .info(
                  "XmlHttpProxyServlet attempting to post to url "
                      + urlString
                      + " with no body content");
        xhp.doPost(
            urlString,
            out,
            xslInputStream,
            paramsMap,
            bodyContent.toString(),
            req.getContentType(),
            userName,
            password);
      }
    } catch (Exception iox) {
      iox.printStackTrace();
      getLogger().severe("XmlHttpProxyServlet: caught " + iox);
      try {
        writer = res.getWriter();
        writer.write(iox.toString());
        writer.flush();
      } catch (java.io.IOException ix) {
        ix.printStackTrace();
      }
      return;
    } finally {
      try {
        if (out != null) out.close();
        if (writer != null) writer.close();
      } catch (java.io.IOException iox) {
      }
    }
  }
Esempio n. 18
0
  /**
   * Parses a text file of JSONs into an array of tweets
   *
   * @param textFile The textfile containing the JSONs
   */
  public parseJSON() {

    // get folder paths
    String currentDir = System.getProperty("user.dir");
    String root = currentDir;
    String textFile = root + "/tweet_input/tweets.txt";
    String outputFolderF1 = root + "/tweet_output/f1.txt";
    String outputFolderF2 = root + "/tweet_output/f2.txt";

    int numTweets = 0;
    int numGoodTweets = 0;
    hashtagEdges hashEdges = new hashtagEdges();

    // try objects
    try {
      // create a reader object for tweet's textfile and read-in first line
      BufferedReader reader = new BufferedReader(new FileReader(textFile));
      String currentJSON = reader.readLine();
      // System.out.println(textFile);

      // initate a writer object with the outputFolder name
      PrintWriter writerF1 = new PrintWriter(outputFolderF1, "UTF-8");
      PrintWriter writerF2 = new PrintWriter(outputFolderF2, "UTF-8");

      // create an array list to save parsedTweets and declare problem variables
      List<parsedTweet> tweetList = new ArrayList<parsedTweet>();
      String tweetText, tweetTime;
      JSONObject objJSON;
      String[] hashArray;
      parsedTweet tweetObj;
      float currentAverage;

      while (currentJSON != null) {
        try {
          objJSON = new JSONObject(currentJSON);
          // if the JSON has a text and time stamp, process it
          numTweets = numTweets + 1;
          if (objJSON.has("created_at") && objJSON.has("text")) {

            // get timestamp and text from JSON object
            tweetTime = objJSON.getString("created_at");
            tweetText = objJSON.getString("text");

            // create a tweet object and add it to folder
            tweetObj = new parsedTweet(tweetTime, tweetText, currentJSON);
            tweetList.add(tweetObj);

            // update hashObjet
            hashArray = tweetObj.hashtags.toArray(new String[tweetObj.hashtags.size()]);
            currentAverage = hashEdges.updateEdges(hashArray, tweetTime);

            // write correctly-formated cleen-tweet to output folder
            writerF1.println(tweetObj.formatTweet());
            numGoodTweets = numGoodTweets + 1;
            writerF2.format("%.2f%n", currentAverage);
          }

        } catch (Exception e) {
          System.out.println("Error in parseJSON - 1");
          e.printStackTrace();
        }

        // read next line (which has the next JSON object)
        currentJSON = reader.readLine();
      }
      writerF1.close();
      writerF2.close();
    } catch (Exception e) {

      System.out.println("Error in parseJSON - 2");
      e.printStackTrace();
    }
  }
  /**
   * Sends an HTTP GET request to a url
   *
   * @param url the url
   * @return - HTTP response code
   */
  public int sendGetRequest(String url)
      throws IOException, OAuthMessageSignerException, OAuthExpectationFailedException,
          OAuthCommunicationException {

    System.out.println("url in sendrequest= " + url);
    int responseCode = 500;
    try {
      HttpURLConnection uc = getConnection(url);

      responseCode = uc.getResponseCode();

      if (200 == responseCode || 401 == responseCode || 404 == responseCode) {
        BufferedReader rd =
            new BufferedReader(
                new InputStreamReader(
                    responseCode == 200 ? uc.getInputStream() : uc.getErrorStream()));
        StringBuffer sb = new StringBuffer();
        String line;
        while ((line = rd.readLine()) != null) {
          System.out.println("line = " + line);
          sb.append(line);
          System.out.println("sb.append = " + sb);
        }
        String response = sb.toString();
        try {
          JSONObject json = new JSONObject(response);

          System.out.println("\nResults:");
          System.out.println(
              "Total results = "
                  + json.getJSONObject("bossresponse")
                      .getJSONObject("web")
                      .getString("totalresults"));

          System.out.println();

          JSONArray ja =
              json.getJSONObject("bossresponse").getJSONObject("web").getJSONArray("results");
          BufferedWriter out = new BufferedWriter(new FileWriter("outfile.csv", true));
          String str = "";
          System.out.println("\nResults:");
          for (int i = 0; i < ja.length(); i++) {
            // System.out.print((i+1) + ". ");
            JSONObject j = ja.getJSONObject(i);
            str += j.getString("url") + "," + j.getString("abstract") + "\n";
            out.write(str);
          }

        } catch (Exception e) {
          System.err.println("Something went wrong...");
          e.printStackTrace();
        }
        rd.close();
        setResponseBody(sb.toString());
      }
    } catch (MalformedURLException ex) {
      throw new IOException(url + " is not valid");
    } catch (IOException ie) {
      throw new IOException("IO Exception " + ie.getMessage());
    }
    return responseCode;
  }
Esempio n. 20
0
  /**
   * Request URL
   *
   * @param List<String> request of url directories.
   * @return JSONArray from JSON response.
   */
  public JSONArray _request(List<String> url_components) {
    String json = "";
    StringBuilder url = new StringBuilder();
    Iterator url_iterator = url_components.iterator();

    url.append(this.ORIGIN);

    // Generate URL with UTF-8 Encoding
    while (url_iterator.hasNext()) {
      try {
        String url_bit = (String) url_iterator.next();
        url.append("/").append(_encodeURIcomponent(url_bit));
      } catch (Exception e) {
        e.printStackTrace();
        JSONArray jsono = new JSONArray();
        try {
          jsono.put("Failed UTF-8 Encoding URL.");
        } catch (Exception jsone) {
        }
        return jsono;
      }
    }

    // Fail if string too long
    if (url.length() > this.LIMIT) {
      JSONArray jsono = new JSONArray();
      try {
        jsono.put(0);
        jsono.put("Message Too Long.");
      } catch (Exception jsone) {
      }
      return jsono;
    }

    try {
      URL request = new URL(url.toString());
      URLConnection conn = request.openConnection();
      String line = "";

      conn.setConnectTimeout(200000);
      conn.setReadTimeout(200000);

      BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));

      try {
        // Read JSON Message
        while ((line = reader.readLine()) != null) {
          json += line;
        }

      } catch (Exception ex) {

      } finally {
        reader.close();
      }

    } catch (Exception e) {

      JSONArray jsono = new JSONArray();

      try {
        jsono.put("Failed JSONP HTTP Request.");
      } catch (Exception jsone) {
      }

      e.printStackTrace();
      System.out.println(e);

      return jsono;
    }

    // Parse JSON String
    try {
      return new JSONArray(json);
    } catch (Exception e) {
      JSONArray jsono = new JSONArray();

      try {
        jsono.put("Failed JSON Parsing.");
      } catch (Exception jsone) {
      }

      e.printStackTrace();
      System.out.println(e);

      // Return Failure to Parse
      return jsono;
    }
  }
  public static ArrayList<ContentValues> parsePosts(
      TagNode aThread,
      int aThreadId,
      int unreadIndex,
      int opId,
      AwfulPreferences prefs,
      int startIndex) {
    ArrayList<ContentValues> result = new ArrayList<ContentValues>();
    boolean lastReadFound = false;
    int index = startIndex;
    String update_time = new Timestamp(System.currentTimeMillis()).toString();
    Log.v(TAG, "Update time: " + update_time);
    try {
      if (!Constants.isICS()
          || !prefs.inlineYoutube) { // skipping youtube support for now, it kinda sucks.
        aThread = convertVideos(aThread);
      }
      TagNode[] postNodes = aThread.getElementsByAttValue("class", "post", true, true);

      for (TagNode node : postNodes) {
        // fyad status, to prevent processing postbody twice if we are in fyad
        ContentValues post = new ContentValues();
        post.put(THREAD_ID, aThreadId);

        // We'll just reuse the array of objects rather than create
        // a ton of them
        int id = Integer.parseInt(node.getAttributeByName("id").replaceAll("post", ""));
        post.put(ID, id);
        post.put(AwfulProvider.UPDATED_TIMESTAMP, update_time);
        post.put(POST_INDEX, index);
        if (index > unreadIndex) {
          post.put(PREVIOUSLY_READ, 0);
          lastReadFound = true;
        } else {
          post.put(PREVIOUSLY_READ, 1);
        }
        index++;
        post.put(IS_MOD, 0);
        post.put(IS_ADMIN, 0);
        TagNode[] postContent = node.getElementsHavingAttribute("class", true);
        for (TagNode pc : postContent) {
          if (pc.getAttributeByName("class").contains("author")) {
            post.put(USERNAME, pc.getText().toString().trim());
          }

          if (pc.getAttributeByName("class").contains("role-mod")) {
            post.put(IS_MOD, 1);
          }

          if (pc.getAttributeByName("class").contains("role-admin")) {
            post.put(IS_ADMIN, 1);
          }

          if (pc.getAttributeByName("class").equalsIgnoreCase("title")
              && pc.getChildTags().length > 0) {
            TagNode[] avatar = pc.getElementsByName("img", true);

            if (avatar.length > 0) {
              post.put(AVATAR, avatar[0].getAttributeByName("src"));
            }
            post.put(AVATAR_TEXT, pc.getText().toString().trim());
          }

          if (pc.getAttributeByName("class").equalsIgnoreCase("postbody")
              || pc.getAttributeByName("class").contains("complete_shit")) {
            TagNode[] images = pc.getElementsByName("img", true);

            for (TagNode img : images) {
              // don't alter video mock buttons
              if ((img.hasAttribute("class")
                  && img.getAttributeByName("class").contains("videoPlayButton"))) {
                continue;
              }
              boolean dontLink = false;
              TagNode parent = img.getParent();
              String src = img.getAttributeByName("src");

              if ((parent != null && parent.getName().equals("a"))
                  || (img.hasAttribute("class")
                      && img.getAttributeByName("class")
                          .contains("nolink"))) { // image is linked, don't override
                dontLink = true;
              }
              if (src.contains(".gif")) {
                img.setAttribute(
                    "class",
                    (img.hasAttribute("class") ? img.getAttributeByName("class") + " " : "")
                        + "gif");
              }

              if (img.hasAttribute("title")) {
                if (!prefs.showSmilies) { // kill all emotes
                  String name = img.getAttributeByName("title");
                  img.setName("p");
                  img.addChild(new ContentNode(name));
                }
              } else {
                if (!lastReadFound && prefs.hideOldImages || !prefs.imagesEnabled) {
                  if (!dontLink) {
                    img.setName("a");
                    img.setAttribute("href", src);
                    img.addChild(new ContentNode(src));
                  } else {
                    img.setName("p");
                    img.addChild(new ContentNode(src));
                  }
                } else {
                  if (!dontLink) {
                    img.setName("a");
                    img.setAttribute("href", src);
                    TagNode newimg = new TagNode("img");
                    if (!prefs.imgurThumbnails.equals("d") && src.contains("i.imgur.com")) {
                      int lastSlash = src.lastIndexOf('/');
                      if (src.length() - lastSlash <= 9) {
                        int pos = src.length() - 4;
                        src = src.substring(0, pos) + prefs.imgurThumbnails + src.substring(pos);
                      }
                    }
                    newimg.setAttribute("src", src);
                    img.addChild(newimg);
                  }
                }
              }
            }

            StringBuffer fixedContent = new StringBuffer();
            Matcher fixCharMatch = fixCharacters_regex.matcher(NetworkUtils.getAsString(pc));

            while (fixCharMatch.find()) {
              fixCharMatch.appendReplacement(fixedContent, "");
            }

            fixCharMatch.appendTail(fixedContent);
            post.put(CONTENT, fixedContent.toString());
          }

          if (pc.getAttributeByName("class").equalsIgnoreCase("postdate")) {
            post.put(
                DATE,
                NetworkUtils.unencodeHtml(pc.getText().toString())
                    .replaceAll("[^\\w\\s:,]", "")
                    .trim());
          }

          if (pc.getAttributeByName("class").equalsIgnoreCase("profilelinks")) {
            TagNode[] links = pc.getElementsHavingAttribute("href", true);

            if (links.length > 0) {
              String href = links[0].getAttributeByName("href").trim();
              String userId = href.substring(href.lastIndexOf("rid=") + 4);

              post.put(USER_ID, userId);

              if (Integer.toString(opId).equals(userId)) { // ugh
                post.put(IS_OP, 1);
              } else {
                post.put(IS_OP, 0);
              }
            }
          }
          if (pc.getAttributeByName("class").equalsIgnoreCase("editedby")
              && pc.getChildTags().length > 0) {
            post.put(EDITED, "<i>" + pc.getChildTags()[0].getText().toString() + "</i>");
          }
        }
        TagNode[] editImgs = node.getElementsByAttValue("alt", "Edit", true, true);

        if (editImgs.length > 0) {
          post.put(EDITABLE, 1);
        } else {
          post.put(EDITABLE, 0);
        }
        result.add(post);
      }

      Log.i(
          TAG,
          Integer.toString(postNodes.length) + " posts found, " + result.size() + " posts parsed.");
    } catch (Exception e) {
      e.printStackTrace();
    }

    return result;
  }
    protected Boolean doInBackground(ArrayList... params) {
      download_photos.this.runOnUiThread(
          new Runnable() {
            public void run() {
              mtext.setText(
                  getText(R.string.download_textview_message_1) + " " + path.toString() + ". ");
            }
          });

      if (resume_file.exists()) {
        initial_value = readProgress()[0];
      } else {
        initial_value = 1;
      }

      for (int i = initial_value - 1; i < links.size(); i++) {
        // asynctask expects more than one ArrayList<String> item, but we are sending only one,
        // which is params[0]
        Uri imageuri = Uri.parse(params[0].get(i).toString());
        URL imageurl = null;
        HttpURLConnection connection = null;
        total_files_to_download = links.size();
        completed_downloads = i + 1;
        try {
          imageurl = new URL(params[0].get(i).toString());
          connection = (HttpURLConnection) imageurl.openConnection();
          connection.connect();
        } catch (Exception e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
        }

        // extracts the real file name of the photo from url
        path_segments = imageuri.getPathSegments();
        int total_segments = path_segments.size();
        file_name = path_segments.get(total_segments - 1);

        path.mkdirs();

        // if(i==0)
        //	first_image = path.toString() + "/" + file_name;

        InputStream input;
        OutputStream output;
        try {
          input = new BufferedInputStream(imageurl.openStream());
          fully_qualified_file_name = new File(path, file_name);
          output = new BufferedOutputStream(new FileOutputStream(fully_qualified_file_name));
          byte data[] = new byte[1024];
          int count;
          while ((count = input.read(data)) != -1) {
            output.write(data, 0, count);
          }
          output.flush();
          output.close();
          input.close();
          connection.disconnect();

          new folder_scanner(getApplicationContext(), fully_qualified_file_name);

          publishProgress(completed_downloads, total_files_to_download);
          if (this.isCancelled()) {
            writeProgress(completed_downloads, total_files_to_download);
            break;
          }

        } catch (IOException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
        }

        // creates required folders and subfolders if they do not exist already
        // boolean success = path.mkdirs();

        // makes request to download photos
        // DownloadManager.Request request = new DownloadManager.Request(imageuri);

        // set path for downloads
        // request.setDestinationInExternalPublicDir(Environment.DIRECTORY_PICTURES,sub_path);

        // request.setDescription("Downloaded using Facebook Album Downloader");

        // DownloadManager dm = (DownloadManager)getSystemService(DOWNLOAD_SERVICE);

        // download is enqueue in download list. it returns unique id for each download
        // download_id = dm.enqueue(request);

      }
      // returns the unique id. we are not using this id
      return true;
    }