~cszikszoy/do-plugins/fix-confluence

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
// TorrentSearchAction.cs
//
//GNOME Do is the legal property of its developers. Please refer to the
//COPYRIGHT file distributed with this
//source distribution.
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
//

using System;
using System.Collections.Generic;
using System.IO;
using System.Web;
using System.Net;
using System.Text.RegularExpressions;
using System.Xml;

using Do.Universe;

namespace Do.Riptide
{
	
	
	public class TorrentSearchAction : AbstractAction
	{
		
		public TorrentSearchAction()
		{

		}
		
		public override string Name {
			get { return "Search For Torrents"; }
		}

		public override string Description {
			get { return "Search the internet for torrents"; }
		}
		
		public override string Icon {
			get { return "gnome-searchtool"; }
		}

		public override Type[] SupportedItemTypes {
			get { return new Type[] { typeof (ITextItem) }; }
		}

		
		public override IItem[] Perform (IItem[] items, IItem[] modItems)
		{
			List<IItem> outItems;
			outItems = new List<IItem> ();
			string search;
			search = HttpUtility.UrlEncode ((items[0] as ITextItem).Text);
			
			search = "http://isohunt.com/js/rss/" + search;
			
			WebClient client = new WebClient ();
			Stream stream = client.OpenRead (search);
			
			XmlDocument xdoc = new System.Xml.XmlDocument ();
			xdoc.Load (stream);
			
			XmlNodeList nodes;
			nodes = xdoc.SelectNodes ("/rss/channel/item");
			
			TorrentResultItem result;
			Regex regx;
			MatchCollection mc;
			string description, seeds, leeches, size;
			foreach (XmlNode n in nodes) {
				description = n.SelectSingleNode("description").InnerText;
				
				mc = Regex.Matches (description, "Seeds: [0-9]*");
				seeds = mc[0].Value;
				
				mc = Regex.Matches (description, "Leechers: [0-9]*");
				leeches = mc[0].Value;
				
				mc = Regex.Matches (description, "Size: [0-9]*.[0-9]* MB");
				size = mc[0].Value;
				
				result = new TorrentResultItem (n.SelectSingleNode("title").InnerText);
				
				result.URL = n.SelectSingleNode("enclosure").Attributes[0].InnerText;
				result.Seeds    = Convert.ToInt32 (seeds.Substring (7));
				result.Leechers = Convert.ToInt32 (leeches.Substring (10));
				result.Size     = size.Substring (6);
				
				outItems.Add (result);
			}
			
			outItems.Sort ();
			
			if (outItems.Count == 0) {
				outItems.Add (new TextItem ("No Torrent Results Found For " + 
				                            (items[0] as ITextItem).Text));
			}
			
			return outItems.ToArray ();
		}

	}
}