/// <summary> /// /// </summary> /// <param name="data"></param> /// <returns></returns> public OpdsItems GetItems(OpdsData data) { if (data == null) { throw new ArgumentNullException("data"); } return(new OpdsItems { Items = ConvertDataItems(data) }); }
private SyndicationItem BuildEntity(OpdsData data, IPropertyAccessor accessor, object item, bool withDetail) { var syndicationItem = CreateBasicDataItems(accessor, item); // 详细页 if (withDetail) { // 购买链接 var buyLink = LinkGenerator.Generate(item, OpdsRelations.Buy, null, accessor, Names); if (buyLink != null) { buyLink.Prices.Single().CurrencyCode = item.GetProperty(Names.CurrencyCode, accessor).ToNullableString() ?? "CNY"; syndicationItem.Links.Add(buyLink); } // 下载链接 var downloadLink = LinkGenerator.Generate(item, OpdsRelations.OpenAcquisition, null, accessor, Names); if (downloadLink != null) { downloadLink.MediaType = item.GetProperty(Names.MimeType, accessor).ToNullableString(); downloadLink.Length = Convert.ToInt64(item.GetProperty(Names.Size, accessor)); downloadLink.Uri = new Uri(item.GetProperty(Names.DownloadAddress, accessor).ToNullableString() ?? downloadLink.Uri.ToString(), UriKind.RelativeOrAbsolute); syndicationItem.Links.Add(downloadLink); } // 其它详细信息 syndicationItem.Content = item.GetProperty(Names.Content, accessor).MakeSyndicationContent(); syndicationItem.Copyright = item.GetProperty(Names.Copyright, accessor).MakeSyndicationContent(); syndicationItem.ISBN = item.GetProperty(Names.Isbn, accessor).ToNullableString(); syndicationItem.Language = item.GetProperty(Names.Language, accessor).ToNullableString(); syndicationItem.Issued = item.GetProperty(Names.IssueTime, accessor).ToNullableString(); syndicationItem.Extent = item.GetProperty(Names.Extent, accessor).ToNullableString(); var relevance = item.GetProperty(Names.Relevance, accessor); if (relevance != null) { syndicationItem.Relevance = Convert.ToDouble(relevance); } var publishDate = item.GetProperty(Names.PublishDate, accessor); if (publishDate != null && (DateTime)publishDate != DateTime.MinValue) { syndicationItem.PublishDate = new DateTimeOffset(Convert.ToDateTime(publishDate)); } var categories = item.GetProperty(Names.CategoryInfo, accessor); if (categories != null) { if (categories is string) { syndicationItem.Categories.Add(new SyndicationCategory(categories as string)); } else if (categories is IEnumerable) { foreach (var category in categories as IEnumerable) { FillInCategoryInfo(syndicationItem, category); } } else { FillInCategoryInfo(syndicationItem, categories); } } else { FillInCategoryInfo(syndicationItem, item, accessor, Names.CategoryPrefix); } ExtracPersonInfo(item, syndicationItem.Contributors, Names.ContributorPrefix, accessor); } // 书籍列表页需要有详细页链接 // 是否有详细页链接,与是否显示详细信息没有直接关系。 if (data.MediaType == OpdsMediaType.AcquisitionFeed) { var detailLink = LinkGenerator.Generate(item, OpdsRelations.Alternate, OpdsMediaType.Entry, accessor, Names); if (detailLink != null) { syndicationItem.Links.Add(detailLink); } else { throw new InvalidProgramException("LinkGenerator don't provide detail link"); } } // 图片链接 var thumbnail = item.GetProperty(Names.Thumbnail, accessor).ToNullableString(); if (thumbnail != null) { syndicationItem.Links.Add(new SyndicationLink { Uri = new Uri(thumbnail), RelationshipType = OpdsRelations.Thumbnail, }); } var cover = item.GetProperty(Names.Cover, accessor).ToNullableString(); if (cover != null) { syndicationItem.Links.Add(new SyndicationLink { Uri = new Uri(cover), RelationshipType = OpdsRelations.Cover, }); } syndicationItem.Publisher = item.GetProperty(Names.Publisher, accessor).ToNullableString(); syndicationItem.Source = item.GetProperty(Names.Source, accessor).ToNullableString(); OnSyndicationItemCreated(syndicationItem, item); return(syndicationItem); }
private IEnumerable <KeyValuePair <object, SyndicationItem> > ConvertDataItems(OpdsData data) { // Assuming every item is of different type. // PropertyAdapter should be retreived for every item. foreach (var item in data.Data ?? new IOpdsDataTypeHost[] { }) { var accessor = AccessorFactory.GetAccessor(item); var dataType = TypeDetector.DetectType(item); if (dataType == OpdsDataType.Category) { var syndicationItem = CreateBasicDataItems(accessor, item); var navigationLink = LinkGenerator.Generate(item, OpdsRelations.Alternate, OpdsMediaType.NavigationFeed, accessor, Names); if (navigationLink != null) { var count = item.GetProperty(Names.Count, accessor); if (count != null) { navigationLink.Count = Convert.ToInt32(count); } syndicationItem.Links.Add(navigationLink); } OnSyndicationItemCreated(syndicationItem, item); yield return(new KeyValuePair <object, SyndicationItem>(item, syndicationItem)); } else { yield return(new KeyValuePair <object, SyndicationItem>(item, BuildEntity(data, accessor, item, dataType == OpdsDataType.Detial))); } } }