Yahoo Finance down loader stopped working - java

I have a JAVA program that i have been using for the last year to downlaod data from yahoo finance. A few days ago it stopped working and yahoo started to refuse the connection.
java.net.ConnectException: Connection refused: connect
Has anyone else experienced this?
import java.net.URL;
import java.net.URLConnection;
import java.util.Calendar;
import java.util.Scanner;
import java.util.GregorianCalendar;
import java.util.Calendar;
import java.util.ArrayList;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.Scanner;
public class C_GetStocksFromYahoo {
public Scanner input;
public void GetStocks(String symbol, GregorianCalendar start, GregorianCalendar end) {
String url = "http://real-chart.finance.yahoo.com/table.csv?s="
+ symbol
+ "&a=" + start.get(Calendar.MONTH)
+ "&b=" + start.get(Calendar.DAY_OF_MONTH)
+ "&c=" + start.get(Calendar.YEAR)
+ "&d=" + end.get(Calendar.MONTH)
+ "&e=" +end.get(Calendar.DAY_OF_MONTH)
+ "&f=" + end.get(Calendar.YEAR)
+ "&g=d&ignore=.csv"; //g=d means daily stock quotes
try {
URL yhoofin=new URL(url);//Create New URL data
URLConnection data=yhoofin.openConnection();//Open a connection
input = new Scanner(data.getInputStream());//get data from input stream
//System.out.println("This is the scanner " + input);
//System.out.println("This is the first line " + this.nextline());
//System.out.println("This is the first line " + this.nextline());
} catch (Exception e) {
System.out.println("This is the symbol i failed on " + symbol);
System.err.println(e);
}
}
//scanner value
public Scanner getScan() {
return input;
}
//determine if file has next entry
public boolean hasnext() {
return input.hasNext();
}
//contains next line
public String nextline() {
return input.nextLine();
}
public void closeConnection() {
input.close();
}
}

Unfortunately Yahoo discontinued their API for historical data, therefor your URL http://real-chart.finance.yahoo.com/table.csv? won't get you any data back anymore.
On https://forums.yahoo.net/t5/Yahoo-Finance-help/Is-Yahoo-Finance-API-broken/td-p/250503/page/3 Nixon (Official 'Hoo Staff) said:
Hi All - This feature was discontinued by the Finance team and they will not be reintroducing that functionality.
Taken from: Yahoo Finance URL not working

Related

Trying to get IP of other devices connected to the same WIFI network

In the following code I am trying to verify the availability of devices sharing the same WIFI network but when I run it nothing is being printed in the console and the program just pauses for a while and closes. Am I checking with the wrong address in the first place or the wrong way all-together ?
If so then what would be a correct way of doing this in Java ?
import java.io.File;
import java.util.Scanner;
import java.net.InetAddress;
public class net_chat {
public static void checkHosts(String subnet){
int timeout=1000;
for (int i=1;i<255;i++){
String host=subnet + "." + i;
try{
if (InetAddress.getByName(host).isReachable(timeout)){
System.out.println(host + " is reachable");
}
}catch(Exception e){
System.out.println( "None at " + host );
}
}
}
public static void main ( String[] args ) {
checkHosts("192.168.0");
}
}

Java split not working consistently

I have the following code below the output.
My issue is that the split function is not working correctly/consistently.
I want to split on each "$".
Reason being is that I want to parse the GGA and RMC data.
Before anyone spends to much time, is this the right way to do this?
My steps:
Read GPS data
Store sentence type (GGA, RMC) in variables that only store the most recent data
Parse those variables and pass to program and then database?
[
$GPGSV,,,,,,,,,*43
$GPRMC,055106.000,A,,N,,W,0.00,61.40,,,,A*4D]
[
$GPVTG,,,,,,T,,M,,,,K,A*3E
$GPGGA,055107.000,,N,,W,,,,M,-33.3,M,,0000*6T
$GPGLL,,N,,W,055107.000,A,A*44]
[
$GPRMC,055107.000,A,,N,,W,0.00,,,,,A*4F
$GPVTG,,,,,,T,,M,0.00,,0.0,,A*3E]
[
$GPGGA,055108.000,3,N,,W,1,09,0.9,,,M,,0000*62]
import jssc.SerialPort;
import jssc.SerialPortEvent;
import jssc.SerialPortEventListener; import jssc.SerialPortException;
import java.util.Arrays;
//import java.awt.List;
//import java.util.Base64;
//import java.io.BufferedReader;
//import java.io.ByteArrayInputStream;
//import java.io.InputStream;
//import java.io.InputStreamReader;
//import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.io.IOException;
import java.lang.*;
//import static java.util.Arrays.asList;
//import java.util.List;
//import java.util.stream.Collectors;
//import org.apache.commons.lang3.StringUtils;
public class test {
static List<String> datat = new ArrayList<String>();
static SerialPort serialPort;
public static void main(String[] args) {
serialPort = new SerialPort("COM1");
try {
serialPort.openPort();//Open ports
serialPort.setParams(4800, 8, 1, 0);//Set params
int mask = SerialPort.MASK_RXCHAR + SerialPort.MASK_CTS + SerialPort.MASK_DSR;//Prepare mask
serialPort.setEventsMask(mask);//Set mask
serialPort.addEventListener(new SerialPortReader());//Add SerialPortEventListener
}
catch (SerialPortException ex) {
System.out.println(ex);
}
}
/*
* In this class must implement the method serialEvent, through it we learn about
* events that happened to our port. But we will not report on all events but only
* those that we put in the mask. In this case the arrival of the data and change the
* status lines CTS and DSR
*/
static class SerialPortReader implements SerialPortEventListener {
public void serialEvent(SerialPortEvent event) {
// if(event.isRXCHAR()){//If data is available
// if(event.getEventValue() < 577){//Check bytes count in the input buffer
//Read data, if 10 bytes available
try {
String getdata = serialPort.readString(event.getEventValue()+1);
String[] parts= getdata.split("$");
if(!datat.isEmpty()){
datat.set(datat.size() - 1, datat.get(datat.size() - 1) + parts[0]);
}
//data.set(data.size() - 1, data.get(data.size() - 1) + parts[0]);
for (int i=1; i<parts.length; i++) {
datat.add(parts[i]);
// System.out.println(Arrays.toString(parts));
}
String[] data2 = datat.toArray(new String[0]);
for(String s : data2)
{
data2 = s.split("$");
List<String> data3 = Arrays.asList(data2);
// int testing = data3.size();
System.out.println(data3);
}
}
catch (SerialPortException ex) {
}
}
}
}
The split function takes a regular expression, not a string, You are using a special character in a regular expresion ($) then you need to scape that character
String s= "$........$...$....";
String[] data2= s.split("\\$");

Jsoup Reddit Image Scraper Over 18 issue

I'm working on an image scraper that scrapes the first page of various subreddits using JSOUP. The issue that arises however is when attempting to scrape a NSFW subreddit, reddit redirects to an over 18 authentication page and the scraper scrapes the authentication page instead. I'm new to scraping and understand this is a noob question, but any help would be much appreciated as I am totally lost.
import java.io.IOException;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.io.*;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.io.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Attributes;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import java.io.IOException;
import java.net.URL;
import java.util.Scanner;
public class javascraper{
public static final String USER_AGENT = "<User-Agent: github.com/dabeermasood:v1.2.3 (by /u/swedenotswiss)>";
public static void main (String[]args) throws MalformedURLException
{
Scanner scan = new Scanner (System.in);
System.out.println("Where do you want to store the files?");
String folderpath = scan.next();
System.out.println("What subreddit do you want to scrape?");
String subreddit = scan.next();
subreddit = ("http://reddit.com/r/" + subreddit);
new File(folderpath + "/" + subreddit).mkdir();
//test
try{
//gets http protocol
Document doc = Jsoup.connect(subreddit).userAgent(USER_AGENT).timeout(0).get();
//get page title
String title = doc.title();
System.out.println("title : " + title);
//get all links
Elements links = doc.select("a[href]");
for(Element link : links){
//get value from href attribute
String checkLink = link.attr("href");
Elements images = doc.select("img[src~=(?i)\\.(png|jpe?g|gif)]");
if (imgCheck(checkLink)){ // checks to see if img link j
System.out.println("link : " + link.attr("href"));
downloadImages(checkLink, folderpath);
}
}
}
catch (IOException e){
e.printStackTrace();
}
}
public static boolean imgCheck(String http){
String png = ".png";
String jpg = ".jpg";
String jpeg = "jpeg"; // no period so checker will only check last four characaters
String gif = ".gif";
int length = http.length();
if (http.contains(png)|| http.contains("gfycat") || http.contains(jpg)|| http.contains(jpeg) || http.contains(gif)){
return true;
}
else{
return false;
}
}
private static void downloadImages(String src, String folderpath) throws IOException{
String folder = null;
//Exctract the name of the image from the src attribute
int indexname = src.lastIndexOf("/");
if (indexname == src.length()) {
src = src.substring(1, indexname);
}
indexname = src.lastIndexOf("/");
String name = src.substring(indexname, src.length());
System.out.println(name);
//Open a URL Stream
URLConnection connection = (new URL(src)).openConnection();
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
} //Delay to comply with rate limiting
connection.setRequestProperty("User-Agent", USER_AGENT);
InputStream in = connection.getInputStream();
OutputStream out = new BufferedOutputStream(new FileOutputStream( folderpath+ name));
for (int b; (b = in.read()) != -1;) {
out.write(b);
}
out.close();
in.close();
}
}
I've posted an answer to authenticate against the server using Jsoup in this link. Basically you need to POST your login ID & password and other required data to the server using:
Connection.Response res = Jsoup.connect(url).data(...).method(Method.Post).execute();, then save the response cookie from the server to keep your session authenticated.

Want to Know size of folder on remote windows PC

I want to know the folder size of a remote Windows PC. I have credentials of the remote PC also.
So far I have tested this program. I works on my local system but not remote PC.
package ext.Size;
import java.io.File;
import org.apache.commons.io.FileUtils;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
public class FileSize {
public static void main(String[] args)
{
FileSize fs= new FileSize();
Float size=fs.ReturnSize("\\\\199.258.63.85\\D:\\test_folder");
if(size!=07)
System.out.println(size);
}
public static float ReturnSize(String args) {
String server = "\\\\199.258.63.85";
int port = 22;
String user = "test75";
String pass = "testpass75";
System.out.println("server=="+server);
try {
FTPClient ftpClient = new FTPClient();
ftpClient.connect(server,port);
ftpClient.login(user, pass);
//ftpClient.enterLocalPassiveMode();
//ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
float size = FileUtils.sizeOfDirectory
(new File(args));
System.out.println("Size: " + size + " bytes" + size/1073741824 + "GB" );
return size;
}
catch(Exception e)
{
System.out.println(e);
return 07;
}
/*float size = FileUtils.sizeOfDirectory
(new File(args));
System.out.println("Size: " + size + " bytes" + size/1073741824 + "GB" );*/
}
}
Error:- java.net.UnknownHostException: \199.258.63.85

Protocol get Java URL

I'm trying to get a JSON format of all the websites found when querying google.
Code:
import java.io.FileWriter;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
/**
* Created by Vlad on 19/03/14.
*/
public class Query {
public static void main(String[] args){
try{
String arg;
arg = "random";
URL url = new URL("GET https://www.googleapis.com/customsearch/v1?key=&cx=017576662512468239146:omuauf_lfve&q=" + arg);
InputStreamReader reader = new InputStreamReader(url.openStream(),"UTF-8");
int ch;
while((ch = reader.read()) != -1){
System.out.print(ch);
}
}catch(Exception e)
{
System.out.println("This ain't good");
System.out.println(e);
}
}
}
Exception:
java.net.MalformedURLException: no protocol: GET https://www.googleapis.com/customsearch/v1?key=AIzaSyCS26VtzuCs7bEpC821X_l0io_PHc4-8tY&cx=017576662512468239146:omuauf_lfve&q=random
You should delete the GET at the beginning ;)
You should replace your code by :
URL url = new URL("https://www.googleapis.com/customsearch/v1?key=AIzaSyCS26VtzuCs7bEpC821X_l0io_PHc4-8tY&cx=017576662512468239146:omuauf_lfve&q=" + arg);
Url never start by GET or POSTor anything like that ;)
Urls are supposed to start with a protocol for transfer and GET https://www.googleapis.com/customsearch/v1?key=AIzaSyCS26VtzuCs7bEpC821X_l0io_PHc4-8tY&cx=017576662512468239146:omuauf_lfve&q=random is starting with GET, that is why the exception is received.
Change it to https://www.googleapis.com/customsearch/v1?key=AIzaSyCS26VtzuCs7bEpC821X_l0io_PHc4-8tY&cx=017576662512468239146:omuauf_lfve&q=random

Categories