dwlakes

News Source

May 1st, 2022
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.64 KB | None | 0 0
  1. package com.example.webscrapingtutorial;
  2.  
  3. import androidx.appcompat.app.AppCompatActivity;
  4.  
  5. import android.content.Intent;
  6. import android.graphics.drawable.Drawable;
  7. import android.net.Uri;
  8. import android.os.AsyncTask;
  9. import android.os.Bundle;
  10. import android.view.View;
  11. import android.widget.Button;
  12. import android.widget.ImageView;
  13. import android.widget.TextView;
  14.  
  15. import com.squareup.picasso.Picasso;
  16. import com.squareup.picasso.Transformation;
  17.  
  18. import org.jsoup.Jsoup;
  19. import org.jsoup.nodes.Document;
  20. import org.jsoup.nodes.Element;
  21. import org.jsoup.select.Elements;
  22.  
  23. import java.io.IOException;
  24. import java.util.ArrayList;
  25. import java.util.Random;
  26.  
  27. public class MainActivity<nprArticles> extends AppCompatActivity {
  28.     ImageView img1;
  29.     TextView texx;
  30.     ImageView logo;
  31.     Drawable drawable;
  32.  
  33.     @Override
  34.     protected void onCreate(Bundle savedInstanceState) {
  35.         super.onCreate(savedInstanceState);
  36.         setContentView(R.layout.activity_main);
  37.         img1 = findViewById(R.id.img1);
  38.         texx= findViewById(R.id.tex1);
  39.         logo = findViewById(R.id.logo);
  40.  
  41.         new doit().execute();
  42.  
  43.     }
  44.  
  45.     public class doit extends AsyncTask<Void, Void, Void >{
  46.  
  47.  
  48.         String words;
  49.         String nprImageURL;
  50.         String nprStoryLink;
  51.         String logoLink;
  52.         ArrayList<Element> nprImageLinks;
  53.         ArrayList<Elements> nprStoryLinks = new ArrayList<Elements>();
  54.         ArrayList<String> nprStoryDirect= new ArrayList<String>();
  55.         ArrayList<Element> imageLinksEachStory = new ArrayList<Element>();
  56.         ArrayList<String> nprImageDirect= new ArrayList<String>();
  57.         ArrayList<Article> nprArticles = new ArrayList<Article>();
  58.  
  59.         @Override
  60.         protected Void doInBackground(Void... voids) {
  61.             Document doc = null;
  62.             Document docForImageLinks = null;
  63.             Elements storyLinks;
  64.             Elements imageLinks = null;
  65.  
  66.  
  67.             // Finds NPR site and scrapes it
  68.             try {
  69.                 doc = Jsoup.connect("https://www.npr.org/sections/news/").get();
  70.  
  71.             } catch (IOException e) {
  72.                 e.printStackTrace();
  73.             }
  74.  
  75.  
  76.             // searches for articles
  77.             Elements elements = doc.getElementsByClass("title");
  78.  
  79.  
  80.             //gets href class and extra stuff
  81.             for(Element e : elements){
  82.                 nprStoryLinks.add(e.getElementsByAttribute("href"));
  83.             }
  84.             //trims down to just the hyper link to the story
  85.             for (Elements e : nprStoryLinks){
  86.                 nprStoryDirect.add(e.attr("href"));
  87.  
  88.             }
  89.             String imageLink;
  90.             int count = 0;
  91.             //Gets image links for each npr story
  92.             for (String links : nprStoryDirect){
  93.                 //System.out.println(links);
  94.  
  95.                 try {
  96.                     docForImageLinks = Jsoup.connect(nprStoryDirect.get(count)).get();
  97.                     count++;
  98.  
  99.  
  100.                 } catch (IOException e) {
  101.                     e.printStackTrace();
  102.                 }
  103.                 //gets basically all image links for npr stories
  104.                 imageLinks = docForImageLinks.getElementsByAttribute("data-original");
  105.                 if(imageLinks.first().toString()==null){
  106.                     System.out.println("found null");
  107.                 }
  108.  
  109.                 //gets the first image data for each npr story in array
  110.                 imageLinksEachStory.add(imageLinks.first());
  111.             }
  112.  
  113.             //trims down to just the hyper link to the image
  114.             for (Element e : imageLinksEachStory){
  115.                 if (e != null) {
  116.                     nprImageDirect.add(e.attr("data-original"));
  117.                 }
  118.             }
  119.  
  120.             /* Converts the elements and imageLinks arraylists into
  121.             string arrays to be be able to use a random number generator */
  122.             Object nprStoryTextStrings [] = elements.toArray();
  123.             Object nprImageLinkStrings [] = nprImageDirect.toArray();
  124.  
  125.             for (String e : nprStoryDirect){
  126.  
  127.             }
  128.             int counter = 0;
  129.             // Creates article objects and stores them into a list
  130.             for(String e : nprStoryDirect){
  131.                 Article nprArticle = new Article(nprStoryDirect.get(counter),elements.get(counter).text(), nprImageLinkStrings[counter].toString(), "NPR");
  132.                 nprArticles.add(nprArticle);
  133.                 counter++;
  134.             }
  135.             Button articleButton = new Button("HI");
  136.             for (Article e : nprArticles){
  137.  
  138.             }
  139.  
  140.             Random r = new Random();
  141.             int newRandomNumber= r.nextInt(nprArticles.size());
  142.  
  143.             nprStoryLink = nprArticles.get(newRandomNumber).storyLink;
  144.             nprImageURL = nprArticles.get(newRandomNumber).imageUrl;
  145.             words= nprArticles.get(newRandomNumber).storyText;
  146.             return null;
  147.         }
  148.  
  149.         @Override
  150.         protected void onPostExecute(Void unused) {
  151.             super.onPostExecute(unused);
  152.             Picasso.get().load(nprImageURL).fit().centerCrop().into(img1);
  153.             drawable = getResources().getDrawable(R.drawable.nprlogo);
  154.             logo.setImageDrawable(drawable);
  155.  
  156.             texx.setText(words);
  157.  
  158.  
  159.             Button btn1 = findViewById(R.id.button2);
  160.  
  161.             btn1.setOnClickListener(new View.OnClickListener() {
  162.                 @Override
  163.                 public void onClick(View view) {
  164.                     Uri uri = Uri.parse(nprStoryLink);
  165.                     startActivity(new Intent(Intent.ACTION_VIEW, uri));
  166.  
  167.                 }
  168.             });
  169.  
  170.         }
  171.  
  172.     }
  173.  
  174.  
  175. }
  176.  
  177.  
Advertisement
Add Comment
Please, Sign In to add comment