How to scrape view count on Youtube video with Jsoup? - java

I am trying to get the view count of a youtube video in Jsoup. I started by getting the title which worked well but am having trouble getting the view count which is in a span class.
Here is my code so far:
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import java.io.IOException;
import java.awt.*;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class HigherOrLowerYoutube {
public static void main(String[] args) {
try {
Document doc =
Jsoup.connect("https://www.youtube.com/watchv=omlFsLz2WKM").get();
println(doc.title());
} catch (IOException e) {
e.printStackTrace();
}
}
private static void println(String string) {
System.out.println(string);
}
}

Try:
System.out.println(doc.select(".watch-view-count").first().text());

Related

Trying to draw a graph but getting Null on weigths

I'm trying to create an image file of a graph with vertices,edges and weigths with JgraphX and mxGraph.
I'm new to this thing of JGraph and stuff and found a tutorial to do this.
But I can't understand why when it creates the image, it shows the vertices and edges good but the weights are "Null".
I'll post the code below together with a image.
import com.mxgraph.layout.mxCircleLayout;
import com.mxgraph.layout.mxIGraphLayout;
import com.mxgraph.util.mxCellRenderer;
import org.jgrapht.ext.JGraphXAdapter;
import org.jgrapht.graph.DefaultDirectedGraph;
import org.jgrapht.graph.DefaultDirectedWeightedGraph;
import org.jgrapht.graph.DefaultEdge;
import org.jgrapht.*;
import org.jgrapht.graph.DefaultUndirectedWeightedGraph;
import org.jgrapht.traverse.BreadthFirstIterator;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
public class JgraphTest{
DefaultDirectedWeightedGraph<String, DefaultEdge> g = new DefaultDirectedWeightedGraph<>(DefaultEdge.class);
BreadthFirstIterator bfs;
public void crearGrafo(){
g.addVertex("Ninja1");
g.addVertex("Ninja2");
g.addVertex("Ninja3");
g.addVertex("Ninja5");
g.addVertex("Ninja8");
g.addEdge("Ninja1","Ninja2");
g.addEdge("Ninja2","Ninja1");
g.addEdge("Ninja8","Ninja3");
g.addEdge("Ninja3","Ninja5");
g.setEdgeWeight("Ninja1","Ninja2",12);
g.setEdgeWeight("Ninja2","Ninja1",2);
g.setEdgeWeight("Ninja8","Ninja3",10);
}
public void mostrarGrafo() throws IOException {
JGraphXAdapter<String, DefaultEdge> graphXAdapter = new JGraphXAdapter<>(g);
mxIGraphLayout layout = new mxCircleLayout(graphXAdapter);
layout.execute(graphXAdapter.getDefaultParent());
BufferedImage image = mxCellRenderer.createBufferedImage(graphXAdapter, null, 2, Color.white, true, null);
File imgFile = new File("src/graph.jpg");
ImageIO.write(image, "JPG",imgFile );
}
}
Image created after executing the algorithm
Thank you everyone and I hope I didn't miss any information, please let me know if I forgot to post something.

play mp3 file in background while running javafx application

I am making javafx cinema seat reservation system as semester project.I want to add .mp3 music so that it plays in background while application is running and performing its fuctions.I added music but what happens is when I run code applications starts and I can perform my seat selections in GUI but there is no background music.but when I click cross(close button) GUI closes and music starts playing...I only included the music code portion.have a look
import java.io.File;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import java.awt.Image;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
public class Cinemafx extends Application{
public void start(Stage primaryStage) {
try {
FileInputStream fileInputStream = new FileInputStream("song.mp3");
Player player = new Player(fileInputStream);
System.out.println("Song is playing...");
player.play();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JavaLayerException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}

Error in OpenCv in Java in Depth of image?

import com.googlecode.javacpp.Loader;
import com.googlecode.javacv.*;
import com.googlecode.javacv.cpp.*;
import com.googlecode.javacv.cpp.opencv_core.CvPoint;
import com.googlecode.javacv.cpp.opencv_core.CvScalar;
import com.googlecode.javacv.cpp.opencv_core.CvSeq;
import static com.googlecode.javacv.cpp.opencv_highgui.CvCapture;
import com.googlecode.javacv.cpp.opencv_core.IplImage;
import com.googlecode.javacv.cpp.opencv_core.*;
import com.googlecode.javacv.cpp.opencv_imgproc.*;
import com.googlecode.javacv.cpp.opencv_calib3d.*;
import com.googlecode.javacv.cpp.opencv_objdetect.*;
import static com.googlecode.javacv.cpp.opencv_core.cvGetSize;
import static com.googlecode.javacv.cpp.opencv_highgui.*;
public class main {
public static void main(String[] args) {
// TODO Auto-generated method stub
IplImage img = cvLoadImage("images.png");
IplImage hsvimg = cvCreateImage(cvGetSize(img),IPL_DEPTH_8U,3);
IplImage grayimg = cvCreateImage(cvGetSize(img),IPL_DEPTH_8U,1);
cvCvtColor(img,hsvimg,CV_BGR2HSV);
cvCvtColor(img,grayimg,CV_BGR2GRAY);
cvShowImage("Original", img);
cvShowImage("HSV", hsvimg);
cvShowImage("GRAY", grayimg);
cvWaitKey();
cvSaveImage("Original.jpg",img);
}
}
My "IPL_DEPTH_8U, CV_BGR2HSV" is not working, I took this code from YOUTUBE this tutorial I tried many times but this is not working please help me.

JavaFX Populate ComboBox from MySql

I am using Java Fx and Scene builder, and my code runs without exceptions, but the combobox remains empty, and I can't understand why. I set identity ID and OnAction field but still it doesn't works. Here is my controller:
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.lang.String;
import java.sql.PreparedStatement;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.ComboBox;
import javafx.stage.Stage;
public class AdaugareSediuController {
#FXML
ComboBox<String> meniu;
public void meniuCombo() throws SQLException{
try {
if(Conexiune.conexiuneBd !=null){
String query = "SELECT * FROM maginfo";
PreparedStatement dindb = Conexiune.conexiuneBd.prepareStatement(query);
ResultSet rezultate = dindb.executeQuery();
while (rezultate.next()) {
String Nume = rezultate.getString("nume");
ObservableList<String> list = FXCollections.observableArrayList(Nume);
meniu.getItems().addAll(list);
}
dindb.close();
rezultate.close();
}
}
catch (Exception ex) {
ex.printStackTrace();
}
}
}

Java does not load embedded Images

package common;
import java.awt.image.BufferedImage;
import java.io.InputStream;
import javax.imageio.ImageIO;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class ResourcesToAccess {
public static Icon sharedAbstractDownIcon;
public static Icon sharedAbstractPlayIcon;
public static Icon sharedAbstractPauseIcon;
public static Icon sharedAbstractBlackCursor;
public static Icon sharedAbstractWhiteCursor;
public ResourcesToAccess(){
InputStream is = this.getClass().getClassLoader().getResourceAsStream("/src/images/blackCursor.png");
try{
BufferedImage bi = ImageIO.read(is);
sharedAbstractBlackCursor = (Icon) new ImageIcon(bi);
new JFrame().add(new JLabel(sharedAbstractBlackCursor)).setVisible(true);
}catch(Exception e){
e.printStackTrace();
}
}
public static void main(String[] args) {
new ResourcesToAccess();
}
}
I am using this code to see whether the PNG images can be properly loaded to create JLabels, Icons, etc but I get the error that:
java.lang.IllegalArgumentException: input == null!
at javax.imageio.ImageIO.read(Unknown Source)
at common.ResourcesToAccess.<init>(ResourcesToAccess.java:21)
at common.ResourcesToAccess.main(ResourcesToAccess.java:29)
Why do I see that error message?
try with
this.getClass().getClassLoader().getResourceAsStream("images/blackCursor.png")
it is looking in your classpath so no more src directory there

Categories