Esempio n. 1
0
        static void DownloadFile()
        {
            var name = string.Empty;
            var url  = string.Empty;
            var http = new RestSharp.RestClient();

            http.UserAgent = _UserAgent;

            while (!_NameQueue.IsEmpty)
            {
                _NameQueue.TryDequeue(out name);
                url = _Urls[name];

                http.BaseUrl = new Uri(url);
                if (http.BaseUrl.Scheme == "https")
                {
                    System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
                }
                else
                {
                    System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;
                }

                var req = new RestSharp.RestRequest(RestSharp.Method.GET);
                req.AddParameter("Accept", "application/json, text/javascript, */*; q=0.01", RestSharp.ParameterType.HttpHeader);
                req.AddParameter("Accept-Language", "en", RestSharp.ParameterType.HttpHeader);
                req.AddParameter("Accept-Encoding", "gzip, deflate, br", RestSharp.ParameterType.HttpHeader);

                try
                {
                    var data = http.DownloadData(req);
                    if (data != null && data.Length > 0)
                    {
                        //var ext = System.IO.Path.GetExtension(url);
                        var ext      = System.IO.Path.GetExtension(new Uri(url).LocalPath);
                        var fileName = name;
                        var file     = $"{_SaveDir}{fileName}{ext}";
                        System.IO.File.WriteAllBytes(file, data);

                        Console.WriteLine($"[下载成功]{url}");
                        logger.Debug($@"文件下载成功:{name}\t{url}");
                    }
                    else
                    {
                        Console.WriteLine($"[x下载失败]{url}");
                        logger.Debug($@"文件下载失败:{name}\t{url}");
                    }
                }
                catch (Exception ex)
                {
                    logger.Error(ex, $@"文件下载异常:{name}\t{url}");
                    Console.WriteLine($"[x下载异常]{url}");
                }
                finally
                {
                    _UrlCompleted[name] = true;
                }
                Thread.Sleep(300);
            }
        }
Esempio n. 2
0
        public byte[] FindStockQuote(string stockCode)
        {
            var request = new RestSharp.RestRequest($"https://stooq.com/q/l/?s={stockCode}&f=sd2t2ohlcv&h&e=csv");

            var client = new RestSharp.RestClient();

            return(client.DownloadData(request));
        }
Esempio n. 3
0
        public OmdbModule()
        {
            var omdb = new Omdb.Integration.OmdbQuery();

            Post["/imdb"] = parameters =>
            {
                var slackCommand = new Model.SlackCommandRequest();
                slackCommand = this.Bind <Model.SlackCommandRequest>();

                var searchTerm   = slackCommand.text.Trim();
                var isListSearch = searchTerm.StartsWith("?");
                var response     = new Model.SlackWebhookResponse();

                if (isListSearch)
                {
                }
                else
                {
                    var singleSearchResultCmd = new Commands.SearchSingleResultCommand();
                    response = singleSearchResultCmd.Execute(searchTerm, slackCommand.channel_name, slackCommand.user_name);
                }

                var slackResponder = new Integration.SlackWebhookResponder();
                slackResponder.Send(response);

                var jsonResponse = JsonConvert.SerializeObject(response);
                return(jsonResponse);
            };

            Get["/imdb/{imdbid}.jpg"] = parameters =>
            {
                var client       = new RestSharp.RestClient("http://img.omdbapi.com");
                var request      = new RestSharp.RestRequest(RestSharp.Method.GET).AddQueryParameter("apikey", "cce8fe13").AddQueryParameter("i", parameters["imdbid"]);
                var responseData = client.DownloadData(request);
                var r            = new Response();
                r.Contents = s => {
                    using (var writer = new BinaryWriter(s))
                    {
                        writer.Write(responseData);
                    };
                };
                r.ContentType = "image/jpeg";

                return(r);
            };
        }
Esempio n. 4
0
        /// <summary>
        /// Bulk-download multiple files and folders into one single zip file. (Only Premium Accounts)
        /// </summary>
        /// <param name="IDs">a comma-separated list of quickkeys and folderkeys to be zipped.</param>
        /// <returns></returns>
        public byte[] GetZip(List<string> IDs)
        {
            //Get API base URL
            this.BaseUrl = MediaFireConfiguration.APIBaseURL;

            //Create an Authorization request and Execute it.
            var rr = MediaFireApiHelper.Generate_GetZip_Request(this.Token.Access_Token, IDs);

            var client = new RestSharp.RestClient();
            client.BaseUrl = BaseUrl;

            byte[] result = client.DownloadData(rr);

            try
            {
                string txt = Encoding.UTF8.GetString(result);
                var res2 = Newtonsoft.Json.JsonConvert.DeserializeObject<BaseResponse>(BaseResponse.ClearResponse(txt));

                //Check if received Token is valid
                if (res2 == null || res2.action == null || res2.result == null || res2.action != "file/zip" || res2.result != "Success")
                {
                    if (res2.result == "Error")
                    {
                        throw new ApplicationException(res2.message);
                    }
                    //Not valid
                    return null;
                }
            }
            catch (Newtonsoft.Json.JsonReaderException ex)
            {
                //Propably correct zip file
            }

            return result;
        }
Esempio n. 5
0
        /// <summary>
        /// Downloads a file and returns it in a byte array
        /// </summary>
        /// <param name="FileData">It is FileID or Direct Download Link. If IsID is True this should be FileID, else this should be Direct Download Link</param>
        /// <param name="IsID">If True the FileData should be the FileID. Otherwise FileID should be Direct Download Link</param>
        /// <returns>Bytes array</returns>
        public byte[] DownloadFile(string FileData, bool IsID = true)
        {
            string DDLink = "";
            if (IsID)
            {
                DDLink = GetDirectDownloadLink(FileData);
            }

            if (!string.IsNullOrEmpty(DDLink))
            {
                var client = new RestSharp.RestClient();
                client.BaseUrl = "";
                var rr = MediaFireApiHelper.Generate_DownloadFile_Request(DDLink);
                byte[] testRes = client.DownloadData(rr);
                return testRes;
            }
            return null;
        }