Ejemplo n.º 1
0
        public Feed SearchByGenre(string genre, [FromUri] int page = 1)
        {
            if (page < 1)
            {
                page = 1;
            }
            genre = Uri.UnescapeDataString(genre);
            var searcher = new LuceneIndexStorage();
            int total;
            var books = searcher.SearchExact(out total, "genre", genre, page);

            var feed = new Feed();

            feed.Id    = "tag:root:genre:" + genre;
            feed.Title = T._("Books in the genre of {0}", GenreExtensions.GetDisplayName(genre));
            feed.Total = total;
            AddNavigation($"{Prefix}/genre/{Uri.EscapeDataString(genre)}", feed, page, total, searcher);

            foreach (var book in books)
            {
                feed.Entries.Add(GetBook(book));
            }

            return(feed);
        }
Ejemplo n.º 2
0
        public IDictionary <string, Tuple <string, bool> > GetAllGenres(string startsWith)
        {
            const string field = "genre";

            using (var directory = new SimpleFSDirectory(new DirectoryInfo(Settings.Instance.DatabaseIndex)))
                using (var reader = IndexReader.Open(directory, true)) {
                    var result = new SortedDictionary <string, Tuple <string, bool> >();
                    var terms  = reader.Terms(new Term(field));

                    while (terms.Next())
                    {
                        var term = terms.Term;
                        if (term.Field == field && term.Text.StartsWith(startsWith))
                        {
                            var name        = GenreExtensions.Cut(term.Text, startsWith);
                            var displayName = GenreExtensions.GetNthName(name, 0);
                            var fullName    = GenreExtensions.Combine(startsWith, displayName);
                            var isLast      = GenreExtensions.IsLast(name);

                            result[displayName] = new Tuple <string, bool>(fullName, isLast);
                        }
                    }

                    return(result);
                }
        }
Ejemplo n.º 3
0
        public Feed GetByGenres(string genre)
        {
            genre = Uri.UnescapeDataString(genre ?? "");

            var feed = new Feed();

            feed.Id    = "tag:root:genres" + (genre == "" ? "" : $":{genre}");
            feed.Title = genre == "" ? T._("Books by genres") : T._("Books in the genre of {0}", GenreExtensions.GetDisplayName(genre));
            AddNavigation($"{Prefix}/genres/{Uri.EscapeDataString(genre)}", feed);

            var searcher = new LuceneIndexStorage();
            var genres   = searcher.GetAllGenres(genre);

            foreach (var k in genres)
            {
                var tag   = k.Value.Item2 ? "genre" : "genres";
                var url   = k.Value.Item1;
                var entry = new FeedEntry();
                entry.Id    = $"tag:root:{tag}:{url}";
                entry.Title = k.Key;
                entry.Links.Add(new FeedLink {
                    Type = FeedLinkType.AtomAcquisition, Href = $"{Prefix}/{tag}/{Uri.EscapeDataString(url)}"
                });
                entry.Content = new FeedEntryContent {
                    Text = T._("Books in the genre of {0}", k.Key)
                };
                feed.Entries.Add(entry);
            }

            return(feed);
        }
Ejemplo n.º 4
0
        protected List <Book> Search(Query query, int take, int skip, out int count)
        {
            Query = query.ToString();
            watch = Stopwatch.StartNew();
            var fields = new SortField[]
            {
                new SortField("title_sort", SortField.STRING, false),
                new SortField("pubdate", SortField.STRING, true),
                SortField.FIELD_SCORE
            };
            var sort = new Sort(fields);

            using (var directory = new SimpleFSDirectory(new DirectoryInfo(Settings.Instance.DatabaseIndex)))
                using (var searcher = new IndexSearcher(directory))
                {
                    var docs = searcher.Search(query, null, skip + take, sort);
                    count = docs.TotalHits;

                    var books = new List <Book>();
                    for (int i = skip; i < docs.TotalHits; i++)
                    {
                        if (i > (skip + take) - 1)
                        {
                            break;
                        }

                        var doc     = searcher.Doc(docs.ScoreDocs[i].Doc);
                        var authors = doc.GetFields("author_fullname")
                                      .Select(x => x.StringValue.Split(','))
                                      .Select(x => new Author {
                            FirstName = x[0], MiddleName = x[1], LastName = x[2]
                        });
                        var genres = doc.GetFields("genre")
                                     .Select(x => x.StringValue)
                                     .Select(x => GenreExtensions.Construct(x));

                        Cover cover            = null;
                        var   coverContentType = doc.Get("_cover_type");
                        if (coverContentType != null)
                        {
                            cover = new Cover
                            {
                                Data        = doc.GetBinaryValue("_cover_data"),
                                ContentType = coverContentType
                            };
                        }

                        var meta      = new List <MetaField>();
                        var docFields = doc.GetFields();
                        foreach (var f in docFields)
                        {
                            if (!KNOWN_FIELDS.Contains(f.Name, StringComparer.OrdinalIgnoreCase))
                            {
                                meta.Add(new MetaField {
                                    Name = f.Name, Value = f.StringValue
                                });
                            }
                        }

                        var book = new Book
                        {
                            Id              = Guid.Parse(doc.Get("guid")),
                            LibraryId       = Guid.Parse(doc.Get("_library_id")),
                            UpdatedFromFile = bool.Parse(doc.Get("_updated_from_file")),
                            UpdatedAt       = DateTools.StringToDate(doc.Get("_updated_at")),
                            Title           = doc.Get("title"),
                            Series          = doc.Get("series"),
                            SeriesNo        = int.Parse(doc.Get("seriesno")),
                            File            = doc.Get("file"),
                            Ext             = doc.Get("ext"),
                            Date            = DateTime.Parse(doc.Get("pubdate")),
                            Archive         = doc.Get("archive"),
                            Authors         = authors,
                            Genres          = genres,
                            Annotation      = doc.Get("annotation"),
                            Language        = doc.Get("language"),
                            Cover           = cover,
                            Meta            = meta
                        };
                        books.Add(book);
                    }

                    watch.Stop();

                    return(books);
                }
        }