示例#1
0
        public YouTubeBotController(ILogger <YouTubeBotController> _logger,
                                    IOptions <LocalDebugConfig> localDebugOptions,
                                    IOptions <VideoDownloadConfig> videoDownloadOptions,
                                    IOptions <BitLySettings> bitLyOptions,
                                    IHostingEnvironment environment,
                                    ITelegramBotClient botClient,
                                    HttpClient httpClient)
        {
            localDebugSettings    = localDebugOptions.Value;
            videoDownloadSettings = videoDownloadOptions.Value;
            bitLySettings         = bitLyOptions.Value;

            logger = _logger;
            bot    = botClient;
            env    = environment;
        }
示例#2
0
        public async static Task <string> ShortenUrlAsync(string longUrl, BitLySettings settings)
        {
            var httpClient = new HttpClient();

            string requestBody = $"{{ \"group_guid\": \"{settings.GroupGuid}\", \"domain\": \"{settings.Domain}\", \"long_url\": \"{longUrl}\" }}";

            var requestContent = new StringContent(requestBody);

            var requestMessage = new HttpRequestMessage(HttpMethod.Post,
                                                        new Uri("https://api-ssl.bitly.com/v4/shorten", UriKind.Absolute));

            requestMessage.Content = requestContent;
            requestMessage.Headers.Add("Authorization", settings.Authorization);
            requestMessage.Headers.Add("origin", "https://bitly.com");
            requestMessage.Headers.Add("referer", "https://bitly.com/");

            requestContent.Headers.Remove("content-type");
            requestContent.Headers.Add("content-type", "application/json");

            string res = await requestMessage.Content.ReadAsStringAsync();

            string shortLink = "error";

            using (var response = await httpClient.SendAsync(requestMessage))
            {
                response.EnsureSuccessStatusCode();
                string responseBody = await response.Content.ReadAsStringAsync();

                Regex  linkRegex = new Regex("\"link\":\"(http:\\/\\/bit.ly\\/[A-z0-9]{1,10})\"");
                Match  linkFound = linkRegex.Match(responseBody);
                string link      = linkFound.Groups[1].Value;

                shortLink = link;
            }

            return(shortLink);
        }
        public static async Task <IList <DownloadLink> > GetDownloadLinksAsync(
            VideoDownloadConfig config,
            string videoLink,
            Action onNotLoading         = null,
            bool shortenLinks           = false,
            BitLySettings bitLySettings = null)
        {
            List <DownloadLink> result = new List <DownloadLink>();
            var httpClient             = new HttpClient();
            var provider = config.VideoProvider;
            // if we didn't get a response in 30 seconds, we throw sorry
            var waitTokenSource = new CancellationTokenSource(TimeSpan.FromSeconds(30));

            string videoLinkEncoded = WebUtility.UrlEncode(videoLink);
            string fullUrl          = string.Format(provider.UrlFormat, videoLinkEncoded);

            try
            {
                // will throw if cancelled
                using (var response = await httpClient.GetAsync(fullUrl, waitTokenSource.Token))
                {
                    response.EnsureSuccessStatusCode();

                    var document = new HtmlDocument();
                    document.Load(await response.Content.ReadAsStreamAsync());

                    foreach (var linkInfo in provider.DownloadLinksInfo)
                    {
                        string link = null, quality = null, size = null, format = null;
                        try
                        {
                            quality = document
                                      .QuerySelector(linkInfo.VideoQualityCSSSelector)
                                      .InnerText;
                            format = document
                                     .QuerySelector(linkInfo.FileFormatCSSSelector)
                                     .InnerText;
                            size = document
                                   .QuerySelector(linkInfo.FileSizeCSSSelector)
                                   .InnerText;

                            link = document
                                   .QuerySelector(linkInfo.DownloadCSSSelector)
                                   .GetAttributeValue("href", def: "error - failed getting href attribute");
                        }
                        catch (NullReferenceException)
                        {
                            continue;
                        }

                        if (shortenLinks)
                        {
                            link = await BitLyApi.ShortenUrlAsync(link, bitLySettings);
                        }

                        result.Add(new DownloadLink(quality, format, size, link));
                    }
                }
            }
            catch (HttpRequestException)
            {
                if (waitTokenSource.Token.IsCancellationRequested)
                {
                    onNotLoading?.Invoke();
                }
                else
                {
                    throw;
                }
            }

            return(result);
        }