1:
2: namespace SLPivotViewer.Web.Services
3: {
4: using System;
5: using System.Collections.Generic;
6: using System.ComponentModel;
7: using System.ComponentModel.DataAnnotations;
8: using System.Linq;
9: using System.ServiceModel.DomainServices.Hosting;
10: using System.ServiceModel.DomainServices.Server;
11: using LinqToTwitter;
12: using System.IO;
13: using System.Web;
14: using System.Xml.Linq;
15: using Microsoft.DeepZoomTools;
16: using System.Security.Principal;
17: using System.Net;
18:
19: [EnableClientAccess()]
20: public class TwitterService : DomainService
21: {
22:
23: [Invoke]
24: public void GenerateUsersCollection(string pstrUserName)
25: {
26: //LOGANDO OS PASSOS
27: StreamWriter writer = new StreamWriter(HttpContext.Current.Server.MapPath("~/log.txt"));
28: try
29: {
30: //GARANTINDO A EXISTENCIA DOS DIRETORIOS
31: if (!File.Exists(HttpContext.Current.Server.MapPath("~/Collections/" + pstrUserName + ".cxml")))
32: {
33: writer.Write("INICIO - CRIAR DIRETORIOS");
34: if (!Directory.Exists(HttpContext.Current.Server.MapPath("~/Collections/")))
35: {
36: Directory.CreateDirectory(HttpContext.Current.Server.MapPath("~/Collections/"));
37: }
38:
39: if (!Directory.Exists(HttpContext.Current.Server.MapPath("~/imgs/")))
40: {
41: Directory.CreateDirectory(HttpContext.Current.Server.MapPath("~/imgs/"));
42: }
43:
44: if (!Directory.Exists(HttpContext.Current.Server.MapPath("~/Collections/")))
45: {
46: Directory.CreateDirectory(HttpContext.Current.Server.MapPath("~/Collections/"));
47: }
48:
49: if (!Directory.Exists(HttpContext.Current.Server.MapPath("~/ClientBin/")))
50: {
51: Directory.CreateDirectory(HttpContext.Current.Server.MapPath("~/ClientBin/"));
52: }
53:
54: if (!Directory.Exists(HttpContext.Current.Server.MapPath("~/ClientBin/" + pstrUserName + "/")))
55: {
56: Directory.CreateDirectory(HttpContext.Current.Server.MapPath("~/ClientBin/" + pstrUserName + "/"));
57: }
58:
59: if (!Directory.Exists(HttpContext.Current.Server.MapPath("~/ClientBin/" + pstrUserName + "/GeneratedImages/")))
60: {
61: Directory.CreateDirectory(HttpContext.Current.Server.MapPath("~/ClientBin/" + pstrUserName + "/GeneratedImages/"));
62: }
63:
64: if (!Directory.Exists(HttpContext.Current.Server.MapPath("~/ClientBin/" + pstrUserName + "/GeneratedImages/output_images/")))
65: {
66: Directory.CreateDirectory(HttpContext.Current.Server.MapPath("~/ClientBin/" + pstrUserName + "/GeneratedImages/output_images/"));
67: }
68:
69:
70: writer.Write("FIM - CRIAR DIRETORIOS");
71:
72: writer.Write("INICIO - CONEXAO TWITTER");
73:
74: //INICIANDO A CONSULTA A API DO TWITTER
75: TwitterContext twitterCtx = new TwitterContext();
76: //CARREGANDO OS USUARIOS QUE ESTÃO SENDO SEGUIDOS PELO USUARIO DA APLICAÇÃO
77: var users =
78: from tweet in twitterCtx.User
79: where tweet.Type == UserType.Friends &&
80: tweet.ID == pstrUserName
81: select tweet;
82:
83:
84: writer.Write("FIM - CONEXAO TWITTER");
85: //CRIAÇÃO DO DEEP ZOOM
86: List<string> lstXMLs = new List<string>();
87:
88: string strCaminhoImagensGeradas = HttpContext.Current.Server.MapPath("/Collections/" + pstrUserName + "/GeneratedImages/");
89:
90: writer.Write("INICIO - CRIAR DEEP ZOOM");
91: CollectionCreator cc = new CollectionCreator();
92:
93: //CRIAÇÃO DO XML DA COLEÇÃO
94: XDocument doc = new XDocument();
95: XElement elCollection = new XElement("Collection");
96: XElement elFacetCateogies = new XElement("FacetCategories");
97: XElement facet = new XElement("FacetCategory");
98: XElement elItems = new XElement("Items");
99: XAttribute at = new XAttribute("Name", "Teste");
100: XAttribute at2 = new XAttribute("Type", "String");
101: facet.Add(at);
102: facet.Add(at2);
103: elFacetCateogies.Add(facet);
104: elCollection.Add(elFacetCateogies);
105:
106: //ADICIONANDO OS NÓS DAS IMAGENS
107: int intContador = 0;
108: foreach (User objUser in users)
109: {
110: XElement elItem = new XElement("Item");
111: writer.Write(" INICIO - CRIAR IMAGEM");
112: ImageCreator ic = new ImageCreator();
113: ic.TileSize = 256;
114: ic.TileFormat = ImageFormat.AutoSelect;
115: ic.ImageQuality = 1;
116: string target = strCaminhoImagensGeradas + "output_images\\" + intContador;
117:
118: if (!File.Exists(HttpContext.Current.Server.MapPath("~/imgs/" + objUser.Identifier.ScreenName + ".jpg")))
119: {
120: WebClient client = new WebClient();
121: client.DownloadFile(objUser.ProfileImageUrl, HttpContext.Current.Server.MapPath("~/imgs/" + objUser.Identifier.ScreenName + ".jpg"));
122: }
123:
124: ic.Create(HttpContext.Current.Server.MapPath("~/imgs/" + objUser.Identifier.ScreenName + ".jpg"), target);
125: writer.Write(" FIM - CRIAR IMAGEM");
126: lstXMLs.Add(Path.ChangeExtension(target, ".xml"));
127: elItem.Add(new XAttribute("Img", "#" + intContador.ToString()));
128: elItem.Add(new XAttribute("Id", intContador.ToString()));
129: elItem.Add(new XAttribute("Href", "http://www.twitter.com/" + objUser.Identifier.ScreenName));
130: elItem.Add(new XAttribute("Name", objUser.Name));
131: elItems.Add(elItem);
132: intContador++;
133: }
134:
135: cc.TileSize = 256;
136:
137: cc.TileFormat = ImageFormat.Jpg;
138:
139: cc.MaxLevel = 8;
140:
141: cc.ImageQuality = 1;
142:
143: cc.Create(lstXMLs, strCaminhoImagensGeradas + "output");
144: writer.Write("FIM - CRIAR DEEPZOOM");
145: elItems.Add(new XAttribute("ImgBase", pstrUserName + @"\GeneratedImages\output.xml"));
146: elCollection.Add(elItems);
147: doc.Add(elCollection);
148: string strFinal = doc.ToString();
149: strFinal = strFinal.Replace("<Collection>", "<Collection xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" SchemaVersion=\"1\" xmlns:d1p1=\"http://schemas.microsoft.com/livelabs/pivot/collection/2009\" xmlns=\"http://schemas.microsoft.com/collection/metadata/2009\">");
150: StreamWriter final = new StreamWriter(HttpContext.Current.Server.MapPath("~/Collections/" + pstrUserName + ".cxml"));
151: final.Write(strFinal);
152: final.Close();
153: }
154: }
155: catch (Exception ex)
156: {
157: writer.Write(ex.Message);
158: }
159: finally
160: {
161: writer.Close();
162: }
163: }
164:
165:
166: }
167: }
168:
169: