private void MetaSearch(SearchMetadata metadata)
        {
            var sw      = new Stopwatch();
            var request = new UIRequest()
            {
                Id           = "MetaSearch",
                TypedRequest = metadata.TypedRequest,
                Delay        = metadata.Delay,
                OnBeforeRun  = () => {
                    sw.Start();
                    _progressBarTracker.Start(metadata.OperationId, metadata.HintText);
                },
                OnAfterRun = () => {
                    sw.Stop();
                    _progressBarTracker.Stop(metadata.OperationId);
                },
                SuccessCallback = typedResponse => {
                    metadata.ProcessResponse(typedResponse, sw);
                },
                ErrorCallback = errorResponse => {
                    ViewModel.SetErrorResponse(errorResponse);
                }
            };

            _uiRequestProcessor.Post(request);
        }
Beispiel #2
0
 public MatchedString(string part, SearchMetadata metadata)
 {
     _metadata     = metadata;
     Part          = part;
     IsMatch       = true;
     BackgroundHue = metadata.Highlight == HighlightingMode.Line ? metadata.HighlightHue : null;
 }
Beispiel #3
0
        public SearchOptionsProxy(SearchMetadata searchMetadata, IEnumerable <Swatch> swatches, Action <SearchMetadata> removeAction)
        {
            Swatches = swatches;
            if (searchMetadata == null)
            {
                throw new ArgumentNullException(nameof(searchMetadata));
            }
            if (swatches == null)
            {
                throw new ArgumentNullException(nameof(swatches));
            }
            if (removeAction == null)
            {
                throw new ArgumentNullException(nameof(removeAction));
            }

            _searchMetadata = searchMetadata;
            Highlight       = _searchMetadata.Highlight;
            Filter          = _searchMetadata.Filter;
            UseRegex        = searchMetadata.UseRegex;
            IgnoreCase      = searchMetadata.IgnoreCase;

            Status = searchMetadata.UseRegex ? SearchResultIndicatorStatus.Regex : SearchResultIndicatorStatus.Text;

            RemoveCommand = new Command(() => removeAction(searchMetadata));
        }
Beispiel #4
0
        private static IEnumerable <MatchedString> Yield(string input, SearchMetadata tomatch)
        {
            if (string.IsNullOrEmpty(input))
            {
                yield break;
            }


            if (tomatch.UseRegex)
            {
                if (!tomatch.RegEx.HasValue || !tomatch.SearchText.IsLongerThanOrEqualTo(3))
                {
                    yield return(new MatchedString(input));

                    yield break;
                }

                var matches = tomatch.RegEx.Value.Matches(input);

                if (matches.Count == 0)
                {
                    yield return(new MatchedString(input));

                    yield break;
                }

                if (matches.Count > 4)
                {
                    yield return(new MatchedString(input));

                    yield break;
                }

                var childMatches = new MatchedStringEnumerator2(input, matches.Cast <Match>()
                                                                .Select(match => match.Value)
                                                                .ToArray(),
                                                                tomatch);

                foreach (var item in childMatches)
                {
                    yield return(item);
                }
            }
            else
            {
                foreach (var item in Yield2(input, tomatch))
                {
                    if (!item.IsMatch)
                    {
                        yield return(new MatchedString(item.Part));
                    }
                    else
                    {
                        //yield return item;
                        yield return(new MatchedString(item.Part, tomatch));
                    }
                }
            }
        }
Beispiel #5
0
        public SearchOptionsProxy([NotNull] SearchMetadata searchMetadata,
                                  [NotNull] Action <SearchMetadata> changeScopeAction,
                                  [NotNull] IColourProvider colourProvider,
                                  [NotNull] IThemeProvider themeProvider,
                                  [NotNull] IconSelector iconSelector,
                                  [NotNull] Action <SearchMetadata> removeAction,
                                  [NotNull] IDefaultIconSelector defaultIconSelector,
                                  Guid parentId)
        {
            if (changeScopeAction == null)
            {
                throw new ArgumentNullException(nameof(changeScopeAction));
            }
            if (colourProvider == null)
            {
                throw new ArgumentNullException(nameof(colourProvider));
            }
            if (themeProvider == null)
            {
                throw new ArgumentNullException(nameof(themeProvider));
            }
            if (removeAction == null)
            {
                throw new ArgumentNullException(nameof(removeAction));
            }

            _searchMetadata      = searchMetadata ?? throw new ArgumentNullException(nameof(searchMetadata));
            _defaultIconSelector = defaultIconSelector ?? throw new ArgumentNullException(nameof(defaultIconSelector));

            IconSelector  = iconSelector ?? throw new ArgumentNullException(nameof(iconSelector));
            ParentId      = parentId;
            Highlight     = _searchMetadata.Highlight;
            Filter        = _searchMetadata.Filter;
            UseRegex      = searchMetadata.UseRegex;
            CaseSensitive = !searchMetadata.IgnoreCase;
            Position      = searchMetadata.Position;
            Hues          = colourProvider.Hues;
            HighlightHue  = searchMetadata.HighlightHue;
            IsGlobal      = searchMetadata.IsGlobal;
            IsExclusion   = searchMetadata.IsExclusion;

            ShowIconSelectorCommand = new Command(async() => await ShowIconSelector());
            RemoveCommand           = new Command(() => removeAction(searchMetadata));
            ChangeScopeCommand      = new Command(() => changeScopeAction((SearchMetadata)this));
            HighlightCommand        = new Command <Hue>(newHue => { HighlightHue = newHue; });

            IconKind = _searchMetadata.IconKind.ParseEnum <PackIconKind>()
                       .ValueOr(() => PackIconKind.ArrowRightBold);

            //combine system with user choice.
            var defaultHue = this.WhenValueChanged(vm => vm.HighlightHue)
                             .CombineLatest(themeProvider.Accent, (user, system) => user == Hue.NotSpecified ? system : user)
                             .Publish();

            Foreground = defaultHue.Select(h => h.ForegroundBrush).ForBinding();
            Background = defaultHue.Select(h => h.BackgroundBrush).ForBinding();

            _cleanUp = new CompositeDisposable(IconSelector, Foreground, Background, defaultHue.Connect());
        }
 public SearchState Map(SearchMetadata search)
 {
     return(new SearchState
            (
                search.SearchText,
                search.Position,
                search.UseRegex,
                search.Highlight,
                search.Filter,
                false,
                search.IgnoreCase,
                search.HighlightHue.Swatch,
                search.IconKind,
                search.HighlightHue.Name,
                search.IsExclusion));
 }
        public PSSearchListSavedSearchResponse(SavedSearchesListResult searchResponse)
        {
            if (searchResponse == null)
            {
                throw new ArgumentNullException("saved search response");
            }

            SearchMetadata m = searchResponse.Metadata;

            this.Metadata = new PSSearchMetadata(searchResponse.Metadata);
            this.Value    = new List <PSSavedSearchValue>();
            foreach (SavedSearch v in searchResponse.Value)
            {
                this.Value.Add(new PSSavedSearchValue(v));
            }
        }
        private static IEnumerable <MatchedString> Yield2(string input, SearchMetadata meta)
        {
            var tomatch    = meta.SearchText;
            var ignoreCase = meta.IgnoreCase;

            if (string.IsNullOrEmpty(input))
            {
                yield break;
            }

            string pattern = "(" + Regex.Escape(tomatch) + ")";
            var    split   = Regex.Split(input, pattern, ignoreCase ? CaseInsensitiveOptions : CaseSensitiveOptions);
            var    length  = split.Length;

            if (length == 0)
            {
                yield break;
            }

            if (length == 1)
            {
                yield return(new MatchedString(input));

                yield break;
            }

            if (meta.Highlight == HighlightingMode.Line)
            {
                yield return(new MatchedString(input, meta));

                yield break;
            }

            foreach (var item in split)
            {
                if (item.Equals(meta.SearchText, StringComparison.OrdinalIgnoreCase))
                {
                    yield return(new MatchedString(item, meta));
                }
                else
                {
                    yield return(new MatchedString(item));
                }
            }
        }
Beispiel #9
0
        public PSSearchMetadata(SearchMetadata metadata)
        {
            if (metadata != null)
            {
                this.ResultType = metadata.ResultType;
                this.Top        = metadata.Top;
                this.Total      = metadata.Total;
                this.SearchId   = metadata.SearchId;
                if (metadata.Id != null)
                {
                    this.Id = new Guid(metadata.Id);
                }

                List <PSCoreSummary> summaryList = new List <PSCoreSummary>();
                if (metadata.CoreSummaries != null)
                {
                    for (int i = 0; i < metadata.CoreSummaries.Count; i++)
                    {
                        summaryList.Add(new PSCoreSummary(metadata.CoreSummaries[i]));
                    }
                }

                this.CoreSummaries = summaryList;
                this.Status        = metadata.Status;
                this.StartTime     = metadata.StartTime;
                this.LastUpdated   = metadata.LastUpdated;
                this.ETag          = metadata.ETag;
                List <PSSearchSort> sortList = new List <PSSearchSort>();
                if (metadata.Sort != null)
                {
                    for (int j = 0; j < metadata.Sort.Count; j++)
                    {
                        sortList.Add(new PSSearchSort(metadata.Sort[j]));
                    }
                }

                this.Sort                     = sortList;
                this.RequestTime              = metadata.RequestTime;
                this.AggregatedValueField     = metadata.AggregatedValueField;
                this.AggregatedGroupingFields = metadata.AggregatedGroupingFields;
                this.Sum    = metadata.Sum;
                this.Max    = metadata.Max;
                this.Schema = new PSMetadataSchema(metadata.Schema);
            }
        }
Beispiel #10
0
        private static IEnumerable <MatchedString> Yield2(string input, SearchMetadata meta)
        {
            var tomatch    = meta.SearchText;
            var ignoreCase = meta.IgnoreCase;

            if (string.IsNullOrEmpty(input))
            {
                yield break;
            }


            //TODO: Check whether there are perf-issues with RegEx
            var split = Regex.Split(input, tomatch, ignoreCase ? CaseInsensitiveOptions : CaseSensitiveOptions);

            var length = split.Length;

            if (length == 0)
            {
                yield break;
            }

            if (length == 1)
            {
                yield return(new MatchedString(input));

                yield break;
            }

            //  int start =0;
            int currentLength = 0;

            for (int i = 0; i < split.Length; i++)
            {
                var current = split[i] ?? string.Empty;

                if (string.IsNullOrEmpty(current))
                {
                    //Get original string back as the user may have searched in a different case
                    var originalString = input.Substring(currentLength, tomatch.Length);
                    yield return(new MatchedString(originalString, meta));

                    currentLength = current.Length + currentLength + tomatch.Length;
                    if (currentLength + tomatch.Length > input.Length)
                    {
                        yield break;
                    }
                }
                else if (i > 0 && !string.IsNullOrEmpty(split[i - 1]))
                {
                    if (currentLength + tomatch.Length > input.Length)
                    {
                        yield break;
                    }

                    //Get original string back as the user may have searched in a different case
                    var originalString = input.Substring(currentLength, tomatch.Length);

                    yield return(new MatchedString(originalString, meta));

                    yield return(new MatchedString(current));

                    currentLength = current.Length + currentLength + tomatch.Length;
                }
                else
                {
                    yield return(new MatchedString(current));

                    currentLength = current.Length + currentLength;
                }
            }
        }
        public static IQueryable <TRecord> ApplyMetadata <TRecord>(this IQueryable <TRecord> query, SearchMetadata metadata, bool ignoreOrdering = false)
        {
            if (!ignoreOrdering)
            {
                // apply ordering
                var orderingColumn        = !string.IsNullOrEmpty(metadata?.OrderBy) ? metadata.OrderBy : "Id";
                var orderingKeyExpression = ExpressionHelper.GetMemberExpression(typeof(TRecord), orderingColumn);

                var orderingDirection  = metadata?.OrderDirection ?? OrderDirectionEnum.DESC;
                var orderingMethodName = orderingDirection == OrderDirectionEnum.ASC
                    ? "OrderBy"
                    : "OrderByDescending";

                // todo: replace with compiled expression
                var method = typeof(Queryable).GetMethods(BindingFlags.Static | BindingFlags.Public)
                             .Single(x => x.Name == orderingMethodName &&
                                     x.GetParameters().Length == 2);
                var genericMethod = method.MakeGenericMethod(typeof(TRecord), orderingKeyExpression.ReturnType);
                query = (IQueryable <TRecord>)genericMethod.Invoke(null, new object[] { query, orderingKeyExpression });
            }

            // apply paging
            if (metadata?.Start > 1)
            {
                query = query.Skip(metadata.Start - 1);
            }

            var pageSize = Math.Min(metadata?.PageSize ?? 20, Constants.MaxSearchPageSize);

            query = query.Take(pageSize);

            return(query);
        }
Beispiel #12
0
        public SearchOptionsViewModel(ICombinedSearchMetadataCollection combinedSearchMetadataCollection,
                                      ISearchProxyCollectionFactory searchProxyCollectionFactory,
                                      ISearchMetadataFactory searchMetadataFactory,
                                      ISchedulerProvider schedulerProvider,
                                      SearchHints searchHints)
        {
            SearchHints = searchHints;

            var global = combinedSearchMetadataCollection.Global;
            var local  = combinedSearchMetadataCollection.Local;

            Action <SearchMetadata> changeScopeAction = meta =>
            {
                if (meta.IsGlobal)
                {
                    //make global
                    global.Remove(meta.SearchText);
                    var newValue = new SearchMetadata(meta, local.NextIndex(), false);
                    local.AddorUpdate(newValue);
                }
                else
                {
                    //make local
                    local.Remove(meta.SearchText);
                    var newValue = new SearchMetadata(meta, global.NextIndex(), true);
                    global.AddorUpdate(newValue);
                }
            };

            Local  = searchProxyCollectionFactory.Create(local, Id, changeScopeAction);
            Global = searchProxyCollectionFactory.Create(global, Id, changeScopeAction);

            //command to add the current search to the tail collection
            var searchInvoker = SearchHints.SearchRequested
                                .ObserveOn(schedulerProvider.Background)
                                .Subscribe(request =>
            {
                var isGlobal  = SelectedIndex == 1;
                var nextIndex = isGlobal ? global.NextIndex() : local.NextIndex();

                var meta = searchMetadataFactory.Create(request.Text,
                                                        request.UseRegEx,
                                                        nextIndex,
                                                        false,
                                                        isGlobal);

                if (isGlobal)
                {
                    global.AddorUpdate(meta);
                }
                else
                {
                    local.AddorUpdate(meta);
                }
            });

            _cleanUp = new CompositeDisposable(searchInvoker,
                                               searchInvoker,
                                               SearchHints,
                                               Global,
                                               Local);
        }
Beispiel #13
0
 public static IEnumerable <MatchedString> MatchString(this string source, SearchMetadata itemsToMatch)
 {
     return(new SearchMetadataEnumerator(source, new [] { itemsToMatch }));
 }
Beispiel #14
0
        public SearchOptionsProxy([NotNull] SearchMetadata searchMetadata,
                                  [NotNull] IColourProvider colourProvider,
                                  [NotNull] IconSelector iconSelector,
                                  [NotNull] Action <SearchMetadata> removeAction,
                                  [NotNull] IDefaultIconSelector defaultIconSelector,
                                  Guid parentId)
        {
            if (searchMetadata == null)
            {
                throw new ArgumentNullException(nameof(searchMetadata));
            }
            if (colourProvider == null)
            {
                throw new ArgumentNullException(nameof(colourProvider));
            }
            if (iconSelector == null)
            {
                throw new ArgumentNullException(nameof(iconSelector));
            }
            if (removeAction == null)
            {
                throw new ArgumentNullException(nameof(removeAction));
            }
            if (defaultIconSelector == null)
            {
                throw new ArgumentNullException(nameof(defaultIconSelector));
            }


            _searchMetadata      = searchMetadata;
            _defaultIconSelector = defaultIconSelector;
            IconSelector         = iconSelector;

            ShowIconSelectorCommand = new Command(ShowIconSelector);
            RemoveCommand           = new Command(() => removeAction(searchMetadata));
            HighlightCommand        = new Command <Hue>(newHue =>
            {
                HighlightHue = newHue;
            });

            ParentId     = parentId;
            Highlight    = _searchMetadata.Highlight;
            Filter       = _searchMetadata.Filter;
            UseRegex     = searchMetadata.UseRegex;
            IgnoreCase   = searchMetadata.IgnoreCase;
            Position     = searchMetadata.Position;
            Hues         = colourProvider.Hues;
            HighlightHue = searchMetadata.HighlightHue;

            IconKind = _searchMetadata.IconKind.ParseEnum <PackIconKind>()
                       .ValueOr(() => PackIconKind.ArrowRightBold);

            Foreground = this.WhenValueChanged(vm => vm.HighlightHue)
                         .Select(h => h.ForegroundBrush)
                         .ForBinding();

            Background = this.WhenValueChanged(vm => vm.HighlightHue)
                         .Select(h => h.BackgroundBrush)
                         .ForBinding();

            _cleanUp = new CompositeDisposable(IconSelector, Foreground, Background);
        }
Beispiel #15
0
 public MatchedStringEnumerator2(string input, IEnumerable <string> itemsToMatch, SearchMetadata tomatch)
 {
     _input        = input;
     _itemsToMatch = itemsToMatch;
     _tomatch      = tomatch;
 }
Beispiel #16
0
 public MatchedString(string part, SearchMetadata metadata)
 {
     _metadata = metadata;
     Part      = part;
     IsMatch   = true;
 }
Beispiel #17
0
 public LineMatch(SearchMetadata searchMetadata)
 {
     _searchMetadata = searchMetadata;
 }
        public void Download(string query, Options options)
        {
            HashSet <long> idSet     = new HashSet <long>();
            WebClient      webClient = new WebClient();

            // search metadata를 조사해가면서 계속 처리해가야 함.
            SearchResult result    = null;
            long?        max_id    = null;
            int          itemCount = 100;

            do
            {
                result = tokens.Search.Tweets(query, count: itemCount, max_id: max_id);

                foreach (Status status in result)
                {
                    if (status.Entities.Media != null)
                    {
                        int statusRt = (status.RetweetCount.HasValue ? status.RetweetCount.Value : 0);
                        if (statusRt >= options.RetweetCount)
                        {
                            // search로 얻어온 media 정보에는 하나밖에 표시되지 않으므로 원본 트윗을 가져와야 함.
                            MediaEntity media    = status.Entities.Media[0];
                            long        sourceId = status.Id;
                            if (media.SourceStatusId.HasValue)
                            {
                                sourceId = media.SourceStatusId.Value;
                            }

                            if (!idSet.Contains(sourceId))
                            {
                                StatusResponse response = tokens.Statuses.Show(sourceId);

                                if (!options.Silence)
                                {
                                    string statusUrl = response.Entities.Media[0].Url;
                                    Console.WriteLine("downloading tweet media from {0} (rt count: {1})", statusUrl, statusRt);
                                }

                                int mediaIndex = 1;
                                foreach (MediaEntity media_ in response.Entities.Media)
                                {
                                    string medialUrl = media_.MediaUrlHttps;
                                    string filename  = string.Format("{0}-{1}{2}", sourceId, mediaIndex, Path.GetExtension(medialUrl));
                                    string filepath  = filename;
                                    if (!string.IsNullOrEmpty(options.DownloadPath))
                                    {
                                        if (!Directory.Exists(options.DownloadPath))
                                        {
                                            Directory.CreateDirectory(options.DownloadPath);
                                        }
                                        filepath = Path.Combine(options.DownloadPath, filename);
                                    }

                                    if (!File.Exists(filepath))
                                    {
                                        if (!options.Silence)
                                        {
                                            Console.WriteLine("\tsaving {0} to {1}", medialUrl, filepath);
                                        }
                                        webClient.DownloadFile(medialUrl, filepath);
                                    }
                                    else
                                    {
                                        if (!options.Silence)
                                        {
                                            Console.WriteLine("\t{0} already exists", filepath);
                                        }
                                    }

                                    mediaIndex++;
                                }

                                idSet.Add(sourceId);
                            }
                        }
                    }
                }

                SearchMetadata metadata = result.SearchMetadata;
                if (metadata.NextResults != null)
                {
                    var nvc = HttpUtility.ParseQueryString(metadata.NextResults);
                    max_id    = long.Parse(nvc.Get("max_id"));
                    itemCount = int.Parse(nvc.Get("count"));
                }
                else
                {
                    max_id = null;
                }
            }while (max_id.HasValue);
        }