Autoaccepting wallet gift cards (#885)

* Add accepting of wallet gift cards

* Split function by two, move bot-related parts to Bot.cs, better xpath

* Remove unneccessary warning

* Forgot to remove beginning chars

* Request data to separate variable, session request in GetDigital..., variable renaming

* Modify XPath a bit to make it more understandable

* Move substring call further, length checking

* Implement semaphore and queue

* Fix typo

* SessionID comment, entries are in alphabetical order

* Add a few spaces to xpath

* Logging: warning -> error

* Dispose GiftCardsSemaphore

* General formatting fixes

* Final formatting fixes, HandledGifts check

* AcceptGifts config value handling

* Handling 0 in function argument
This commit is contained in:
Vital7
2018-08-20 04:33:09 +03:00
committed by Łukasz Domeradzki
parent a936ae9ec1
commit ca9a3458fb
3 changed files with 111 additions and 8 deletions

View File

@@ -3,16 +3,16 @@
// / _ \ | '__|/ __|| '_ \ | |\___ \ | __|/ _ \ / _` || '_ ` _ \ | |_ / _` || '__|| '_ ` _ \
// / ___ \ | | | (__ | | | || | ___) || |_| __/| (_| || | | | | || _|| (_| || | | | | | | |
// /_/ \_\|_| \___||_| |_||_||____/ \__|\___| \__,_||_| |_| |_||_| \__,_||_| |_| |_| |_|
//
//
// Copyright 2015-2018 Łukasz "JustArchi" Domeradzki
// Contact: JustArchi@JustArchi.net
//
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//
// http://www.apache.org/licenses/LICENSE-2.0
//
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -84,6 +84,24 @@ namespace ArchiSteamFarm {
WebBrowser.Dispose();
}
internal async Task<bool> AcceptDigitalGiftCard(ulong gid) {
if (gid == 0) {
Bot.ArchiLogger.LogNullError(nameof(gid));
return false;
}
const string request = "/gifts/0/resolvegiftcard";
// Extra entry for sessionID
Dictionary<string, string> data = new Dictionary<string, string>(3) {
{ "accept", "1" },
{ "giftcardid", gid.ToString() }
};
Steam.NumberResponse result = await UrlPostToJsonObjectWithSession<Steam.NumberResponse>(SteamStoreURL, request, data).ConfigureAwait(false);
return result?.Success == true;
}
internal async Task<bool> AcceptTradeOffer(ulong tradeID) {
if (tradeID == 0) {
Bot.ArchiLogger.LogNullError(nameof(tradeID));
@@ -446,6 +464,37 @@ namespace ArchiSteamFarm {
return await UrlGetToHtmlDocumentWithSession(SteamCommunityURL, request).ConfigureAwait(false);
}
internal async Task<HashSet<ulong>> GetDigitalGiftCards() {
const string request = "/gifts";
HtmlDocument response = await UrlGetToHtmlDocumentWithSession(SteamStoreURL, request).ConfigureAwait(false);
if (response == null) {
return null;
}
HtmlNodeCollection htmlNodes = response.DocumentNode.SelectNodes("//div[@class='pending_gift']/div[starts-with(@id, 'pending_gift_')][count(div[@class='pending_giftcard_leftcol']) > 0]/@id");
HashSet<ulong> results = new HashSet<ulong>();
foreach (string gidText in htmlNodes.Select(node => node.GetAttributeValue("id", null))) {
if (string.IsNullOrEmpty(gidText)) {
Bot.ArchiLogger.LogNullError(nameof(gidText));
return null;
}
if (gidText.Length <= 13) {
Bot.ArchiLogger.LogGenericError(string.Format(Strings.ErrorIsInvalid, nameof(gidText)));
return null;
}
if (!ulong.TryParse(gidText.Substring(13), out ulong gid) || (gid == 0)) {
Bot.ArchiLogger.LogGenericError(string.Format(Strings.ErrorParsingObject, nameof(gid)));
return null;
}
results.Add(gid);
}
return results;
}
internal async Task<HtmlDocument> GetDiscoveryQueuePage() {
const string request = "/explore?l=english";
return await UrlGetToHtmlDocumentWithSession(SteamStoreURL, request).ConfigureAwait(false);