Sunday 21 December 2014

C# : Making a RSS reader


What is RSS ?


RSS (Rich Site Summary); originally RDF Site Summary; often called Really Simple Syndication, uses a family of standard web feed formats[2] to publish frequently updated information: blog entries, news headlines, audio, video. An RSS document (called "feed", "web feed",[3] or "channel") includes full or summarized text, and metadata, like publishing date and author's name.
RSS feeds enable publishers to syndicate data automatically. A standard XML file format ensures compatibility with many different machines/programs. RSS feeds also benefit users who want to receive timely updates from favourite websites or to aggregate data from many sites.
Subscribing to a website RSS removes the need for the user to manually check the website for new content. Instead, their browser constantly monitors the site and informs the user of any updates. The browser can also be commanded to automatically download the new data for the user.
Software termed "RSS reader", "aggregator", or "feed reader", which can be web-baseddesktop-based, or mobile-device-based, present RSS feed data to users. Users subscribe to feeds either by entering a feed's URI into the reader or by clicking on the browser's feed icon. The RSS reader checks the user's feeds regularly for new information and can automatically download it, if that function is enabled. The reader also provides a user interface.

How to Proceed ?
As from the above we now know that RSS feed is basically an XML file which regularly gets updated. As C# contains its own class and methods to read XML files i.e.
using System.Xml;
using Syste.Xml.Linq;
so the program is going to be simple. Regarding the speech you can always use SpeechSythesizer class of C# System.Synthesis library.

Project and Code :

Requirements :
        1. Visual Studio or Visual C# express .
Code :

First Draw a regular GUI for your project. A demo GUI is given below. It should atleast contain a textbox for posting URLs and start , play and pause buttons.


(For tutorials regarding GUI in C# visit  Desgining User Interface or go through youtube)

After completing GUI check whether references given below are present in your project or not. If not add it to your project.
(NOTE : Adding refernces . No need to download, all are system references only).

System.Data.SetExtensions
System.ServiceModel
System.Speech
System.Xml
System.Xml.Linq

Now add these headers to your project

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Speech;
using System.Speech.Synthesis;
using System.Xml;
using System.IO;
using System.ServiceModel.Syndication;

Using Xml reader of C# 

//loading of the XML file
XmlReader reader = XmlReader.Create(url);    // url is the link of the RSS feed
SyndicationFeed feed = SyndicationFeed.Load(reader);
reader.Close();
//Loading completed
foreach (SyndicationItem item in feed.Items)   // iterating through each item of XML file
{
    Console.WriteLine(item.Title.Text);
    Console.WriteLine(item.Summary.Text);
}

Using SpeechSynthesiser

// creating a new object of class
SpeechSynthesizer ss = new SpeechSynthesizer();
//using it
ss.SpeakAsync("Hello..");
//controls
ss.Pause(); //for pausing
ss.Resume(); //for resuming
ss.SpeakAsyncCancelAll(); //for cancelling all

Below is the demo project code with the above demo GUI. 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Speech;
using System.Speech.Synthesis;
using System.Xml;
using System.IO;
using System.ServiceModel.Syndication;

namespace txtspeech
{
    public partial class Form1 : Form
    {

        SpeechSynthesizer ss = new SpeechSynthesizer();
        public Form1()
        {
            InitializeComponent();
            ss.SpeakAsync("Welcome ,Suman ,  R.S.S Speech at your service");
            radioButton2.Checked = true;
            comboBox1.Items.Add("Google");
            comboBox1.Items.Add("Reuters");
            comboBox1.Items.Add("NDTV");
            comboBox1.Items.Add("Hindustan Times");
            comboBox1.Items.Add("The Hindu");
        }

        private void btnPlay(object sender, EventArgs e)
        {
            string url = "https://news.google.com/?output=rss";
            if(radioButton1.Checked==true)
            {
                url = textBox2.Text;
            }
            else
            {
                if (comboBox1.Text == "Google")
                    url = "https://news.google.com/?output=rss";
                else if (comboBox1.Text == "Reuters")
                    url = "http://feeds.reuters.com/reuters/INtopNews";
                else if (comboBox1.Text == "NDTV")
                    url = "http://feeds.feedburner.com/NdtvNews-TopStories";
                else if (comboBox1.Text == "Hindustan Times")
                    url = "http://feeds.hindustantimes.com/HT-HomePage-TopStories";
                else
                    url = "http://www.thehindu.com/news/national/?service=rss";
            }
            XmlReader reader = XmlReader.Create(url);
            SyndicationFeed feed = SyndicationFeed.Load(reader);
            reader.Close();
            textBox1.Text = "";
            foreach (SyndicationItem item in feed.Items)
            {
                textBox1.AppendText(item.Title.Text);
                ss.SpeakAsync(item.Title.Text);
                textBox1.AppendText("\n");
                textBox1.AppendText("---------------------------\n");
            }
            textBox1.Select(0, 0);
        }

        private void btnPause_Click(object sender, EventArgs e)
        {
            ss.Pause();
        }

        private void btnContinue_Click(object sender, EventArgs e)
        {
            ss.Resume();
        }

        private void btnStop_Click(object sender, EventArgs e)
        {
            ss.SpeakAsyncCancelAll();
            ss.Resume();
        }
    }
}





Saturday 20 December 2014

C# : Simple hack script for games with repeated pressing of same keys in order.


There are lot of simple games available online which are basically not strategic, but just require one key to play or set of keys to be pressed in order repeatedly in order to score the maximum point.
For example you can take
http://playtap.in/ where we have to press Spacebar repeatedly, or
http://gabrielecirulli.github.io/2048/ where the Right Arrow and the Down Arrow is pressed repeatedly etc.

For these games we can do a simple hack using C# SendKeys class .

How to implement 

Just create a small GUI with START and STOP buttons . A demo GUI is given below.


Inside the START button's click method run a loop n times for the set of keys to be pressed.
(here START button is btnStart )

 private void btnStart_Click(object sender, EventArgs e)
 {
     //Here n is the no. of times you want to run the loop
     for(int i = 0; i < N ; i++)
     {
         //SendKeys.Send("wsad");  this method can be used to press 'w'
         //'s' 'a' and 'd' , N no. of times in same order
         SendKeys.Send(" "); //for sending spacebar
     }
 }
NOTE : For more information regarding SendKeys visit SendKeys.send();

Friday 5 December 2014

Solving Rubiks Cube

This is a simple tutorial book for solving rubiks cube.
  rubik_solution.pdf