· 6 years ago · Sep 13, 2019, 02:24 PM
1function onOpen() {
2 var ui = SpreadsheetApp.getUi();
3 // Or DocumentApp or FormApp.
4 ui.createMenu('TM API')
5 .addItem('Очистить и загрузить историю операций снова', 'reloadHistory')
6 .addItem('Обновить историю операций', 'updateHistory')
7 .addToUi();
8}
9
10function loadv1() {
11 var range = SpreadsheetApp.getActive().getRange('A2:B2').getValues();
12 var start_time = Math.floor(range[0][0].getTime()/1000);
13 var end_time = Math.floor(range[0][1].getTime()/1000);
14 var url = "https://market.csgo.com/api/v2/history?key=h0qneUhjnk0g8V77d3E4qJ1M4cmDSut&date=" + start_time + "&date_end=" + end_time;
15 var data = JSON.parse(UrlFetchApp.fetch(url));
16 var sheet = SpreadsheetApp.getActiveSheet();
17 Logger.log(data);
18 SpreadsheetApp.getActive().getRange('A5:F100').clearContent();
19 data.data.map(function (x) {
20 sheet.appendRow([x.market_hash_name, x.event, (x.paid/100)]);
21 })
22 //sheet.appendRow(['Cotton Sweatshirt XL', 'css004']);
23
24}
25
26var STAGES = {
27 '2': 'Успешно',
28 '5': 'Таймаут'
29}
30
31function getApiKey() {
32 return SpreadsheetApp.getActive().getRange('C2').getValues()[0][0];
33}
34
35function getHyperlink(text, url) {
36 return '=HYPERLINK("' + url + '";"' + text + '")';
37}
38
39function reloadHistory() {
40 if (!getApiKey()) return SpreadsheetApp.getUi().alert('Укажите TM API KEY!');
41 var range = SpreadsheetApp.getActive().getRange('A2:B2').getValues();
42 SpreadsheetApp.getActive().getRange('A5:E100').clearContent();
43 var start_time = Math.floor(range[0][0].getTime()/1000);
44 var end_time = Math.floor(range[0][1].getTime()/1000);
45 appendHistory(start_time, end_time);
46}
47
48function updateHistory() {
49 if (!getApiKey()) return SpreadsheetApp.getUi().alert('Укажите TM API KEY!');
50 var range = SpreadsheetApp.getActive().getRange('A2:B2').getValues();
51 var last_date = SpreadsheetApp.getActive().getRange('E5:E').getValues().filter(String).slice(-1)[0][0].getTime()/1000;
52 Logger.log(new Date(last_date * 1000));
53 var end_time = Math.floor(range[0][1].getTime()/1000);
54 appendHistory(last_date, end_time);
55}
56
57function appendHistory(start_time, end_time) {
58 var url = "https://market.csgo.com/api/OperationHistory/" + start_time + "/" + end_time + "/?key=" + getApiKey();
59 Logger.log(url);
60 var data = JSON.parse(UrlFetchApp.fetch(url));
61 var sheet = SpreadsheetApp.getActiveSheet();
62 var numberOfRows = Math.max(0, SpreadsheetApp.getActive().getRange('E5:E').getValues().filter(String).length) + 4;
63 Logger.log("numberOfRows: " + numberOfRows);
64 var rows = data.history.reverse().filter(function(x) {
65 return x.h_event == "checkin" || x.h_event == "checkout" || x.h_event == "buy_go" || x.h_event == "sell_go";
66 }).filter(function(x) {
67 return x.stage == 2 || x.h_event == "checkin" || x.h_event == "checkout";
68 }).map(function (x) {
69 if (x.h_event == "checkin" || x.h_event == "checkout") {
70 var amount = x.i_amount || x.o_summ;
71 if (x.h_event == "checkout") amount *= -1;
72 return [x.i_system, x.h_event, (amount/100), STAGES['2'], new Date(x.h_time * 1000)];
73 }
74 var link = getHyperlink(x.market_hash_name, "https://market.csgo.com/?r=&q=&search=" + encodeURIComponent(x.market_hash_name));
75 if (x.h_event == "buy_go") {
76 return [link, x.h_event, -(x.paid/100), STAGES[x["stage"].toString()], new Date(x.h_time * 1000)];
77 }
78 if (x.h_event == "sell_go") {
79 return [link, x.h_event, (x.recieved/100), STAGES[x["stage"].toString()], new Date(x.h_time * 1000)];
80 }
81
82 });
83 if (rows.length == 0) return SpreadsheetApp.getUi().alert('За выбранный период не было совершено ни одной операции!');
84 Logger.log('A' + (numberOfRows + 1) + ':E' + (numberOfRows + rows.length));
85 sheet.getRange('A' + (numberOfRows + 1) + ':E' + (numberOfRows + rows.length)).setValues(rows);
86}
87
88function myFunction(range) {
89 Logger.log(range);
90 var from = Date.now() - 24 * 60 * 60 * 1000 * 1000 * 30;
91 var to = Date.now();
92 var url = "https://market.csgo.com/api/v2/history?key=h0qneUhjnk0g8V77d3E4qJ1M4cmDSut&date=" + from + "&date_end=" + to;
93 Logger.log(url);
94 var data = UrlFetchApp.fetch(url);
95 Logger.log(data);
96 return 123;
97 //return data;
98 //var sheet = SpreadsheetApp.getActiveSheet();
99 //sheet.appendRow(['Cotton Sweatshirt XL', 'css004']);
100}