| ソースコード |
# -*- encoding: utf-8 -*-
# drawr.py
# Copyright c Masaaki Kawata All rights reserved.
import clr
clr.AddReferenceByPartialName("PresentationCore")
clr.AddReferenceByPartialName("PresentationFramework")
clr.AddReferenceByPartialName("WindowsBase")
clr.AddReferenceByPartialName("System.Xml")
clr.AddReferenceByPartialName("System.Configuration")
clr.AddReferenceByPartialName("coRockets")
from System import Byte, String, Int32, Uri, DateTime, TimeSpan, Environment
from System.IO import Stream, StreamReader, Path, DirectoryInfo
from System.Collections.Generic import List
from System.Collections.ObjectModel import Collection;
from System.Diagnostics import Trace
from System.Globalization import CultureInfo, DateTimeStyles
from System.Reflection import Assembly
from System.Security.Cryptography import MD5CryptoServiceProvider
from System.Text import StringBuilder, Encoding
from System.Text.RegularExpressions import Regex, RegexOptions, Match
from System.Timers import Timer
from System.Net import WebRequest, WebResponse, WebClient, HttpRequestHeader
from System.Net.NetworkInformation import NetworkInterface
from System.Windows.Threading import DispatcherTimer;
from System.Xml import XmlDocument, XmlNode, XmlAttribute
from System.Configuration import ConfigurationManager, ConfigurationUserLevel, ExeConfigurationFileMap
from CoRockets import Entry, Fetcher
def update():
try:
global rootEntry
if not NetworkInterface.GetIsNetworkAvailable():
return
cacheDirectory = None
config = None
directoryInfo = DirectoryInfo(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), Assembly.GetEntryAssembly().GetName().Name))
if directoryInfo.Exists:
fileName = Path.GetFileName(Assembly.GetEntryAssembly().Location)
for fileInfo in directoryInfo.GetFiles("*.config"):
if fileName.Equals(Path.GetFileNameWithoutExtension(fileInfo.Name)):
exeConfigurationFileMap = ExeConfigurationFileMap()
exeConfigurationFileMap.ExeConfigFilename = fileInfo.FullName
config = ConfigurationManager.OpenMappedExeConfiguration(exeConfigurationFileMap, ConfigurationUserLevel.None)
cacheDirectory = fileInfo.Directory.FullName
break
if config == None:
config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
if config.HasFile:
if config.AppSettings.Settings["Cache"] != None:
if String.IsNullOrEmpty(cacheDirectory):
cacheDirectory = config.AppSettings.Settings["Cache"].Value
else:
cacheDirectory = Path.Combine(cacheDirectory, config.AppSettings.Settings["Cache"].Value)
di = DirectoryInfo(cacheDirectory)
if di.Exists == False:
di.Create()
request = WebRequest.Create("http://drawr.net/feed.php?md=h")
response = request.GetResponse()
s = response.GetResponseStream()
doc = XmlDocument()
doc.Load(s)
entryList = List[Entry]()
for itemXmlNode in doc.SelectNodes("/rss/channel/item"):
newEntry = Entry()
count = 0
for xmlNode in itemXmlNode.ChildNodes:
if xmlNode.Name.Equals("title"):
newEntry.Title = xmlNode.InnerText
elif xmlNode.Name.Equals("link"):
newEntry.Link = Uri(xmlNode.InnerText)
elif xmlNode.Name.Equals("description"):
if cacheDirectory != None:
m = Regex.Match(xmlNode.InnerText, "src\\s*=\\s*(?:\"(?<1>[^\"]*)\"|(?<1>\\S+))", RegexOptions.IgnoreCase | RegexOptions.Singleline)
if m.Success:
uri = Uri (m.Groups[1].Value)
path = uri.Segments[uri.Segments.Length - 1]
stringBuilder = StringBuilder()
md5 = MD5CryptoServiceProvider()
for b in md5.ComputeHash(Encoding.UTF8.GetBytes(uri.AbsoluteUri)):
stringBuilder.Append(b.ToString("x2"))
if path.IndexOfAny(Path.GetInvalidFileNameChars()) < 0:
extension = Path.GetExtension(path)
if String.IsNullOrEmpty(extension) == False:
stringBuilder.Append(extension)
client = WebClient()
client.Headers.Add(HttpRequestHeader.Referer, "http://drawr.net/feed.php?md=h");
client.DownloadFile(uri, Path.Combine(cacheDirectory, stringBuilder.ToString()))
newEntry.Description = Entry.RemoveTags(xmlNode.InnerText)
newEntry.ImageUri = uri;
newEntry.Cache = newEntry.ImagePath = Path.Combine(cacheDirectory, stringBuilder.ToString())
elif xmlNode.Name.Equals("author"):
newEntry.Author = xmlNode.InnerText
elif xmlNode.Name.Equals("pubDate"):
newEntry.Updated = DateTime.ParseExact(xmlNode.InnerText, "ddd, dd MMM yyyy HH:mm:ss zz00", CultureInfo.InvariantCulture.DateTimeFormat, DateTimeStyles.None)
newEntry.Uri = newEntry.ImageUri
#newEntry.BaseUri = Uri("http://drawr.net/")
#newEntry.BaseTitle = "drawr"
if String.IsNullOrEmpty(newEntry.Title):
newEntry.Title = "N/A"
if newEntry.Uri != None:
isExist = False
for entry in rootEntry.ChildEntries:
if entry.Uri.AbsoluteUri.Equals(newEntry.Uri.AbsoluteUri):
isExist = True
if isExist == False:
entryList.Add(newEntry)
if entryList.Count > 0:
rootEntry.ImageUri = Uri("http://drawr.net/images/logo.png")
entryList.Reverse()
for entry in entryList:
rootEntry.InsertChild(0, entry)
if s != None:
s.Close()
if response != None:
response.Close()
except Exception, e:
Trace.WriteLine(e.clsException.Message)
Trace.WriteLine(e.clsException.StackTrace)
def onUpdate(s, e):
update()
rootEntry = None
for entry in Fetcher.Instance.Entries:
if entry.Title.Equals("drawr") and entry.ParentEntry == None:
rootEntry = entry
break
if rootEntry == None:
rootEntry = Entry()
rootEntry.Title = "drawr"
rootEntry.Type = "Photos"
Fetcher.Instance.Entries.Add(rootEntry)
Fetcher.Instance.Update += onUpdate
|