FCI-Helwan blog

Just another FCI-H weblog

Twitter Common Friends

For the past couple of months, A lot of people (who I don’t know) have been following my twitter.. Mostly they don’t look like anybody I know, not even Egyptians, & I’m not that popular anyway.. I’m ok with people following me anyway.. My twitter is public, & I’m not planning to change that.. But sometimes I need to know if these are real people who are in my twitter “social network” or just some sort of zombies or bots..

I’ve just created a small app to act like “common friends” in Facebook so I can find out whether I really know them or not.. Actually a couple of these followers are really interesting people & I’m following them now.. The app finds common people you’re follwoing.. not common followers, the followers all is almost the same it just need authentacation..



The application depend on a single twitter API which takes the user name as a parameter & returns an xml (or JSON) file of their freinds for eg these are my friends xml file:

http://twitter.com/statuses/friends/mshady.xml

I used the “XML schema definition” tool (Xsd.exe) to generate the class to use the xml nodes directly as nodes rather than querying the XML document.. I’m such a lazy coder.. I know :D..

Once the xml is deserialised into the users datatype like this:

XmlSerializer oXmlSerializer = new XmlSerializer(typeof(users));
users oUsers1 = (users)oXmlSerializer.Deserialize(
                     XmlReader.Create(@”http://twitter.com/statuses/friends/”+textBox1.Text+“.xml”
                        ));

All you need to do is to find the intersection set between the two list/sets.. & display them
 
The source code & binaries are available here: http://cid-570d40f05cc0dc13.skydrive.live.com/self.aspx/Sample%20Code/TwitterTest.rar

Advertisements

April 12, 2009 Posted by | C#, Tips | 2 Comments

Limitations on using Cookies

Make note of the following limitations when using Cookies (I’m not sure if that applies to all Web-technologies or just Asp.Net, but I’ve just solved a bug related to this issue using Asp.Net):

  • A cookie size is limited to 4096 bytes. It is not much, so cookies should be used to store small amounts of data. For larger amounts of data
  • Also, number of cookies is limited to 20 per website. If you make new cookie when you already have 20 cookies, browser will delete oldest one.
Quoted from beansoftware.com

November 18, 2008 Posted by | ASP.NET, Tips | 1 Comment

Amazon S3 via C#

Amazon Simple Storage Service (Amazon S3) via C#

Amazon S3 is storage for the Internet. It is designed to make web-scale computing easier for developers. It’s provides a simple web services interface that can be used to store and retrieve any amount of data, at any time, from anywhere on the web.

It uses standards-based REST and SOAP interfaces so it is platform independent and language independent.

It `s commercial but highly scalable, reliable, fast and inexpensive data storage infrastructure.

To know more about its advantage and the cost click here:

http://www.amazon.com/gp/browse.html?node=16427261

I am writing here to give you my experiences and tricks which I faced it when I started in develop the service using Dot Net (C#) and soap.

First trick:

When you work using .Net, you must use WSE 2.0 NOT WSE 3.0 as Amazon support DIME (WSE 2.0) NOT MTOM (WSE 3.0).

Second trick:

You can only using the class System.Web.Services.Protocols.SoapHttpClientProtocol, if you want upload files to AWS (Amazon Web Service) that are less than one Megabytes.

Here is an excellent example that server the required (files less than megabyte) with complete demo code for view and upload files.

http://developer.amazonwebservices.com/connect/entry.jspa?externalID=774&categoryID=55

Third trick:

If your files are bigger than one Megabytes (ten, twenty or may be 50 megabytes), you must to change the class System.Web.Services.Protocols.SoapHttpClientProtocol to Microsoft.Web.Services2.WebServicesClientProtocol . (As mentioned in this link)

http://developer.amazonwebservices.com/connect/entry!default.jspa?categoryID=103&externalID=689&fromSearchPage=true

Then follow the last trick…

Last trick:

You must raise the Timeout property that inherits the Microsoft.Web.Services2.WebServicesClientProtocol (the AmazonS3class if you opened the following example) more time out (this property measures in milliseconds) .

Here it an excellent example that server the required (more than one megabyte) with complete code.

http://developer.amazonwebservices.com/connect/entry!default.jspa?categoryID=47&externalID=690&fromSearchPage=true

The Amazon S3 Developer Guide :

http://docs.amazonwebservices.com/AmazonS3/2006-03-01/

March 27, 2008 Posted by | C#, Tips | 5 Comments

A common bar-code problem

A tiny detail that is usually missed when working with bar-codes (written in fonts like: “3 of 9 Extended”, etc) is writing the bar-code text between brackets or asterisks:

myLabel.Text = “*”+myBarcode+”*”;

or

myLabel.Text = “(“+myBarcode+”)”;

Forgetting using one of the above ways makes the code unreadable by bar-code readers..

February 18, 2008 Posted by | C#, Tips | 3 Comments

C# : indexed pixel problem

Here I will mention the solve for a common problem happens when you try to create Graphic object from Bitmap images as in the next two lines..yes, it would fire an exception if your bitmap was indexed pixel .

Bitmap tmp = new Bitmap(“Fci-h.jpg”);
Graphics grPhoto = Graphics.FromImage(tmp);

Exception : “A Graphics object cannot be created from an image that has an indexed pixel format”.

To pass this exception you will need to add some additional lines of code . Just create an image with the same size like the original image (Fci-h.jpg) then draw the original image onto the new Graphics object .

Here is an Example :

Bitmap bm = (Bitmap) System.Drawing.Image.FromFile(“Fci-h.jpg”,true);
Bitmap tmp=new Bitmap (bm.Width ,bm.Height );
Graphics grPhoto = Graphics.FromImage(tmp);
grPhoto.DrawImage(bm, new Rectangle(0, 0, tmp.Width , tmp.Height ), 0, 0, tmp.Width , tmp.Height , GraphicsUnit.Pixel);

February 18, 2008 Posted by | C#, Microsoft, Tips | 2 Comments

Stamping text file in PDF pages

I wrote C# function that Stamping the content of text file (text and hyper links) at the front of the select pages (selected by the user) from the PDF .

NOTE :
This C# function stamp the text with aligning the text at the center of the selected pages ,the function based on the latest library called ItextCharp ( iTextSharp 4.0.8 (2008-01-25) .
Download the library from here

iText

# (iTextSharp) is a port of the iText open source java library written entirely in C# for the .NET platform.
iText is a library that allows you to generate PDF files on the fly. It is implemented as an assembly.

Parameter Example :

string sourcefile –> “input.pdf”
string SaveFileName–> “output.pdf”
float Y_space –> 405
float lead–> 25
float FontSize –> 12
string TxtFileName–> “textostamp.txt”
string _Font –> “HELVETICA”
string pagesRange –> “3,6,8” or “3-6,9-15”

you must to import this namespaces 🙂
using System.IO;
using iTextSharp.text.pdf;
using iTextSharp.text;
==

/* StamperTxt is a class which stamps the content of text file (text and hyper links)

* at the front of the select pages (selected by the user)from the PDF .

* */

public StamperTxt(string sourcefile, string SaveFileName, float Y_space, float lead, float FontSize, string TxtFileName, string _Font , string pagesRange)

{

PdfReader pdfreader = new PdfReader(sourcefile);

float pagewidth = pdfreader.GetPageSize(1).Width;

int j = pdfreader.NumberOfPages;

bool[] flag ;

flag = new bool[j];

for (int i = 0; i

flag[i] = false;

if ((pagesRange.Trim() == “all” )&&(pagesRange.IndexOf(“-“) == -1 )&&(pagesRange.IndexOf(“,”) == -1 ) )

{

for (int ii = 0; ii

flag[ii] = true;

}

else if ((pagesRange.IndexOf(“-“) != -1))

{

string[] tempArr = pagesRange.Trim().Split(‘,’);

foreach (string str in tempArr)

{

string[] tempArrhyphen = str.Trim().Split(‘-‘);

int startIndex = 0, endIndex = 0;

startIndex =int.Parse(tempArrhyphen[0].Trim());

endIndex = int.Parse(tempArrhyphen[1].Trim());

for (int s = startIndex; s <= endIndex; s++)

{

flag[s-1] = true;

}

}

}

else if ((pagesRange.IndexOf(‘,’) > -1))

{

string[] tempArr = pagesRange.Trim().Split(‘,’);

foreach (string str in tempArr)

{

int index = int.Parse(str);

flag[index-1] = true;

}

}

PdfGState pdfgstate = new PdfGState();

pdfgstate.FillOpacity = 1.0f;

Stream output_stream = new FileStream(SaveFileName, FileMode.OpenOrCreate);

PdfStamper pdfstamper = new PdfStamper(pdfreader, output_stream);

for (int k = 0; k

{

if (flag[k] != true)

continue;

PdfContentByte pdfcontentbyte = pdfstamper.GetOverContent(k+1);

float v_space = Y_space;

pdfcontentbyte.SaveState();

pdfcontentbyte.SetGState(pdfgstate);

BaseFont PF = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.WINANSI, false) ;

pdfcontentbyte. SetFontAndSize(PF , FontSize);

StreamReader SR;

string S;

SR = File.OpenText(TxtFileName);

ArrayList chunklist = new ArrayList();

S = SR.ReadLine();

while (S != null)

{

Chunk chk = new Chunk(S + “\r\n”, FontFactory.GetFont(_Font, FontSize, Font.NORMAL));

if (S.IndexOf(“http:/”) > -1)

{

chk = new Chunk(S + “\r\n”, FontFactory.GetFont(_Font, FontSize, Font.UNDERLINE));

chk.SetAnchor(S);

}

chunklist.Add(chk);

S = SR.ReadLine();

}

//Console.WriteLine(S);

SR.Close();

Phrase PH = new Phrase();

PH.Font = FontFactory.GetFont(_Font, FontSize, Font.NORMAL);

PH.AddRange(chunklist);

PH.Leading = lead;

PdfPTable table = new PdfPTable(1);

table.DefaultCell.Border = Rectangle.NO_BORDER;

table.DefaultCell.SetLeading(lead, 0);

table.TotalWidth = pagewidth;

table.HorizontalAlignment = Rectangle.ALIGN_MIDDLE;

table.DefaultCell.HorizontalAlignment = Rectangle.ALIGN_MIDDLE;

table.HorizontalAlignment = Rectangle.ALIGN_CENTER;

table.DefaultCell.HorizontalAlignment = Rectangle.ALIGN_CENTER;

table.AddCell(PH);

table.WriteSelectedRows(0, -1,0, v_space, pdfcontentbyte);

}

byte[] arr = pdfstamper.Writer.XmpMetadata;

pdfstamper.Close();

pdfreader.Close();

Console.WriteLine(SaveFileName + “Was Created in Success…”);

}////end Function

February 14, 2008 Posted by | C#, Tips | 2 Comments