<commit_before>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Jabbot.Core.Extensions;
using Jabbot.Core.Jabbr;
using Jabbot.Core.Sprockets;
using Newtonsoft.Json;

namespace Jabbot.Sprockets.Community
{
    /// <summary>
    /// Port / Mashup of the Hubot auto-stache.coffee & faceup.coffee scripts
    /// </summary>
    public class AutoStacheSprocket : RegexSprocket
    {
        private static string[] Accessories { get { return new string[] { "hipster", "clown", "mustache", "scumbag", "jason" }; } }

        public override string Name { get { return "Auto-Stache Sprocket"; } }

        public override string Description { get { return "Automatically add mustaches, hipster glasses, clown noses, scumbag hats, or Jason masks to any images it can."; } }

        public override IEnumerable<string> Usage
        {
            get
            {
                return new string[]
                {
                    "/msg <botnick> auto-stache help",
                    "Type a link to any image (png, jpg, or jpeg)"
                };
            }
        }
        
        protected override IEnumerable<Regex> PrivateMessagePatterns
        {
            get 
            { 
                return new Regex[] 
                { 
                    new Regex(@"(?i)^(auto-stache help)$"),
                }; 
            }
        }

        protected override IEnumerable<Regex> RoomMessagePatterns
        {
            get
            {
                return new Regex[] 
                {
                    new Regex(@"(?i)(?<=(>))(https?:(\S+)(png|jpg|jpeg))(?=(<))"),
                };
            }
        }

        public override void Handle(IPrivateMessage message, IJabbrClient jabbrClient)
        {
            base.Handle(message, jabbrClient);

            if (this.CanHandle(message))
            {
                if (PrivateMessagePatterns.First().Match(message.Content).Success)
                {
                    jabbrClient.PrivateReply(message.From, this.GetFormattedHelp());
                }
            }
        }

        public override void Handle(IRoomMessage message, IJabbrClient jabbrClient)
        {
            base.Handle(message, jabbrClient);

            if (this.CanHandle(message))
            {
                Task.Factory.StartNew(() =>
                {
                    var match = RoomMessagePatterns.First(p => p.Match(message.Content).Success).Match(message.Content);
                    var imageUrl = match.Groups[2].Value;

                    if (CanAccessorize(imageUrl))
                    {
                        var stachedImageUrl = String.Format("http://faceup.me/img.jpg?overlay={0}&src={1}", Accessories.RandomElement(), Uri.EscapeDataString(imageUrl));
                        jabbrClient.SayToRoom(message.Room, stachedImageUrl);
                    }
                }).ContinueWith(task =>
                {
                    var result = task.Exception;
                },
                TaskContinuationOptions.OnlyOnFaulted);
            }
        }

        protected virtual bool CanAccessorize(string imageUrl)
        {
            var canStache = false;

            var client = new HttpClient();
            client.DefaultRequestHeaders.AcceptLanguage.Add(new StringWithQualityHeaderValue("en-us"));
            client.DefaultRequestHeaders.AcceptCharset.Add(new StringWithQualityHeaderValue("utf-8"));
            var result = client.GetAsync(String.Format("http://stacheable.herokuapp.com?src={0}", Uri.EscapeDataString(imageUrl))).Result;

            if (result.IsSuccessStatusCode)
            {
                var content = result.Content.ReadAsStringAsync().Result;
                dynamic json = JsonConvert.DeserializeObject(content);
                canStache = json.count.Value > 0;
            }

            return canStache;
        }
    }
}<commit_msg>Fixed AutoStacheSprocket<commit_after>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Jabbot.Core.Extensions;
using Jabbot.Core.Jabbr;
using Jabbot.Core.Sprockets;
using Newtonsoft.Json;

namespace Jabbot.Sprockets.Community
{
    /// <summary>
    /// Port / Mashup of the Hubot auto-stache.coffee & faceup.coffee scripts
    /// </summary>
    public class AutoStacheSprocket : RegexSprocket
    {
        private static string[] Accessories { get { return new string[] { "hipster", "clown", "mustache", "scumbag", "jason" }; } }

        public override string Name { get { return "Auto-Stache Sprocket"; } }

        public override string Description { get { return "Automatically add mustaches, hipster glasses, clown noses, scumbag hats, or Jason masks to any images it can."; } }

        public override IEnumerable<string> Usage
        {
            get
            {
                return new string[]
                {
                    "/msg <botnick> auto-stache help",
                    "Type a link to any image (png, jpg, or jpeg)"
                };
            }
        }
        
        protected override IEnumerable<Regex> PrivateMessagePatterns
        {
            get 
            { 
                return new Regex[] 
                { 
                    new Regex(@"(?i)^(auto-stache help)$"),
                }; 
            }
        }

        protected override IEnumerable<Regex> RoomMessagePatterns
        {
            get
            {
                return new Regex[] 
                {
                    new Regex(@"(?i)(?<=(>))(https?:(\S+)(png|jpg|jpeg))(?=(<))"),
                };
            }
        }

        public override void Handle(IPrivateMessage message, IJabbrClient jabbrClient)
        {
            base.Handle(message, jabbrClient);

            if (this.CanHandle(message))
            {
                if (PrivateMessagePatterns.First().Match(message.Content).Success)
                {
                    jabbrClient.PrivateReply(message.From, this.GetFormattedHelp());
                }
            }
        }

        public override void Handle(IRoomMessage message, IJabbrClient jabbrClient)
        {
            base.Handle(message, jabbrClient);

            if (this.CanHandle(message))
            {
                Task.Factory.StartNew(() =>
                {
                    var match = RoomMessagePatterns.First(p => p.Match(message.Content).Success).Match(message.Content);
                    var imageUrl = match.Groups[2].Value;
                    var stachedImageUrl = String.Format("http://faceup.me/img.jpg?overlay={0}&src={1}", Accessories.RandomElement(), Uri.EscapeDataString(imageUrl));
                    
                    jabbrClient.SayToRoom(message.Room, stachedImageUrl);
                }).ContinueWith(task =>
                {
                    var result = task.Exception;
                },
                TaskContinuationOptions.OnlyOnFaulted);
            }
        }
    }
}
{'fraction_non_alphanumeric': 0.10534372390759811, 'fraction_numerical': 0.0012524352908433063, 'mean_word_length': 1.6335654085745694, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 24, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1362783', 'n_tokens_mistral': 1977, 'n_tokens_neox': 1832, 'n_words': 378}