Hello guys!

I'm making a simple app to view some comic strips from a website. My problem right now is to update it everytime it opens.

I did a search over the internet, but didn't find nothing that fits to me. I want to download every single gif file from the server that match an predefined name, put it on a folder and display it (displaying is not a concern right now).

I wish that I could save the strip name too. Looking at the page source code, I found this:

<tr>

                        <td><p align="left"><a

                        href="JavaScript:openImage('bom_mau_feio_01.gif',950,450,true)"><font

                        color="#FFFF00" face="Arial"><strong>APRESENTAÇÃO</strong></font></a></p>

                        </td>

                    </tr>

For every strip, there is an code that looks very like this, just changing the link text and image. I don't know if the link being in Java changes something, but the images all are in the root of the server (like http://www.mysite.com/image1.gif and so on).

How can I accomplish that? Thanks in advance!

Recommended Answers

All 3 Replies

I made a few assumptions about what you actually want, but this will give you somewhat of a start:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text.RegularExpressions;

namespace DW_392451
{
   class CDW_392451
   {
      static void Main(string[] args)
      {
         string strURL = "http://www.daniweb.com/software-development/2";
         string strBaseImageLoc = "http://images.daniweb.com/";
         WebClient wc = new WebClient();
         // Regex for capturing .gif names
         // I made this specfic to images.daniweb.com
         Regex rxGotImage = new Regex("src=\"http://images.daniweb.com.*/(?<image>.*.gif)\"");
         
         try
         {
            string strTempDir = Path.GetTempPath();
            // Keeps track of pre-existing files and added files
            List<string> lst_strFilesInDir =
               Directory.GetFiles(strTempDir, "*.gif")
               .Select(strFile => Path.GetFileName(strFile))
               .ToList();
            
            int intNumCurrentFiles = lst_strFilesInDir.Count;

            using (StreamReader fileIn = new StreamReader(wc.OpenRead(strURL)))
            {
               string strData = "";
               string strTargetFileName = "";
               string strOutFileName = "";
               //
               while (!fileIn.EndOfStream)
               {
                  strData = fileIn.ReadLine();
                  if (!rxGotImage.IsMatch(strData))
                  {
                     continue;
                  }
                     //
                  strTargetFileName = rxGotImage.Match(strData).Groups["image"].Value;
                  if (lst_strFilesInDir.Contains(strTargetFileName))
                  {
                     continue;
                  }

                  Console.WriteLine("Snagging: " + strTargetFileName);
                  lst_strFilesInDir.Add(strTargetFileName);
                  strOutFileName = Path.Combine(strTempDir, strTargetFileName);
                  wc.DownloadFile(strBaseImageLoc + strTargetFileName, strOutFileName);
               }

               fileIn.Close();
            }

            int intSnaggedCount = (lst_strFilesInDir.Count - intNumCurrentFiles);
            Console.WriteLine("Snagged {0} new file{1}.", 
               intSnaggedCount, (!intSnaggedCount.Equals(1) ? "s" : ""));
         }
         catch (Exception exc)
         {
            Console.WriteLine("Could not process: " + exc.Message);
            return;
         }
         
         Console.WriteLine("Finished");
      }
   }
}

Thank you! Can you just tell me a bit more about Regex? I've read something about it, but didn't understand very well how to use it. Perhaps an simple commented example?

A RegEx (regular expression) is a way of matching a pattern in a string.
It can replace a lot of string searching, string splitting and if/then/else statements.
Most modern languages utilize regular expressions in some form.

http://msdn.microsoft.com/en-us/library/system.text.regularexpressions.regex.aspx#Y0

static void Main(string[] args)
{
   List<string> lst_strEndings = new List<string>()
   {
      "and they lived happily ever after",
      "and they rode off into the sunset"
   };

   foreach (string s in lst_strEndings)
   {
      if (s.StartsWith("and ") && s.Contains(" happily "))
      {
         Console.WriteLine(s);
      }
   }

   //...can be replaced with... 
   Regex rx = new Regex(@"^and .* happily ");
   Console.WriteLine(lst_strEndings.Where(s => rx.IsMatch(s)).First());

   //...or (spread out...)
   foreach (string s in lst_strEndings)
   {
      if (rx.IsMatch(s))
      {
         Console.WriteLine(s);
      }
   }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.