Last active
August 28, 2019 20:56
-
-
Save SilentGopherLnx/1663fcb129fe003586421729482ec857 to your computer and use it in GitHub Desktop.
ВКонтакте число участников чата в wiki-страничку через бота-группы, добавленного в беседу
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
. "github.com/SilentGopherLnx/easygolang" | |
) | |
const v = "5.101" | |
const app_id = 1111111 // Сгененированное приложение | |
const app_secret_key = "111111111" // его же секретный ключ | |
const code_for_hour = "1111111111" // Временный код, после того как юзер открыл первыю ссылку, ввёл пароль и согласился на предоставление прав | |
const user_token = "111111111111111111111111111111111111111111111111" // Получается после запуска второй ссылки с верменным кодом | |
const club_token = "111111111111111111111111111111111111111111111111" // Просто берём токен из группы | |
const group_id = 11111111 // сама группа | |
const page_id = 11111111 // wiki-страничка | |
const user_id = 11111111 // Юзер, обновляющий wiki-страницу, чей токен используем | |
const url_hidden = "https://vk.com/11111111" // Просто прячу ссылку | |
const pic_male = "photo-1111111111_11111111111" // Картинка мужичка | |
const pic_female = "photo-111111111_11111111111" // Картинка женского пола | |
const filename = "vk_chats.txt" // файл wiki-странички, где %@#$ i| %@#$ заменится на число парней и девушек в чате i | |
// -1 -2 - получение токена юзера, 0 - основная работа, прочие числа - проверка чатаов под идентификатором введённого номера | |
func main() { | |
scope := "offline,pages" | |
standalone_redirect := "https://oauth.vk.com/blank.html" | |
l := Scln() | |
switch l { | |
case "-1": | |
Prln("https://oauth.vk.com/authorize?client_id=" + I2S(app_id) + "&display=mobile&scope=" + scope + "&redirect_uri=" + standalone_redirect + "&response_type=code&v=" + v) | |
case "-2": | |
req := "https://oauth.vk.com/access_token?client_id=" + I2S(app_id) + "&client_secret=" + app_secret_key + "&redirect_uri=" + standalone_redirect + "&code=" + code_for_hour + "&v=" + v | |
Prln(req) | |
case "0": | |
MainWork(user_token, club_token) | |
default: | |
scan_id := S2I(l) | |
json_str := GetClubChatJSON(club_token, int64(scan_id)) | |
_, _, admin := ParseMalesJSON(json_str) | |
Prln(admin) | |
} | |
} | |
func SaveWikiPage(token string, text string) { | |
p := "group_id=" + I2S(group_id) + "&page_id=" + I2S(page_id) + "&user_id=" + I2S(user_id) + "&text=" + UrlQueryEscape(text) | |
resp, err := NetPostUrl("https://api.vk.com/method/pages.save?" + p + "&access_token=" + token + "&v=" + v) | |
if err == nil { | |
Prln(resp) | |
} else { | |
Prln("error:" + err.Error()) | |
} | |
} | |
func GetClubChatJSON(token string, id int64) string { | |
chat := I2S64(2000000000 + id) | |
p := "peer_id=" + chat | |
resp, err := NetReadUrlText("https://api.vk.com/method/messages.getConversationMembers?" + p + "&access_token=" + token + "&v=" + v) | |
if err == nil { | |
Prln(resp) | |
} else { | |
Prln("error:" + err.Error()) | |
return "ERROR" | |
} | |
return resp | |
} | |
func GetMalesString(id int64, male int, female int) string { | |
args := "|10x16px;noborder;nopadding| " | |
return "[[" + pic_male + args + "]]" + I2S(male) + " [[" + pic_female + args + "]]" + I2S(female) + " [" + url_hidden + "_" + I2S64(id) + "|.]" | |
} | |
func MainWork(user_token string, club_token string) { | |
text, ok := FileTextRead(filename) | |
if !ok { | |
Prln("NoFile!") | |
return | |
} | |
arr := StringSplit(text, "%@#$") | |
for j := 0; j < len(arr); j++ { | |
if j%2 == 1 { | |
id_creator := StringSplit(arr[j], "|") | |
id := S2I64(StringTrim(id_creator[0])) | |
if id > 0 { | |
json_str := GetClubChatJSON(club_token, id) | |
//json_str, _ := FileTextRead("test_json.txt") | |
if json_str == "ERROR" { | |
arr[j] = "Ошибка 1-" + I2S64(id) | |
} else { | |
male, female, admin := ParseMalesJSON(json_str) | |
if male > 0 || female > 0 { | |
arr[j] = GetMalesString(id, male, female) | |
Prln("заменено " + I2S64(id) + " " + admin + "||" + I2S(male) + "/" + I2S(female)) | |
} else { | |
arr[j] = "Ошибка 2-" + I2S64(id) | |
} | |
} | |
} else { | |
arr[j] = "Ошибка 0-" + arr[j] | |
} | |
SleepMS(200) | |
} | |
} | |
out := StringJoin(arr, "") | |
//Prln(out) | |
ending := "Обновлено количество человек в чате по Московскому времени: " + TimeNowStr() | |
out = out + "\n" + ending | |
SaveWikiPage(user_token, out) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
. "github.com/SilentGopherLnx/easygolang" | |
"encoding/json" | |
) | |
//https://mholt.github.io/json-to-go/ | |
type AutoGeneratedJSON struct { | |
Response struct { | |
Items []struct { | |
MemberID int `json:"member_id"` | |
InvitedBy int `json:"invited_by"` | |
JoinDate int `json:"join_date"` | |
IsAdmin bool `json:"is_admin,omitempty"` | |
IsOwner bool `json:"is_owner,omitempty"` | |
CanKick bool `json:"can_kick,omitempty"` | |
} `json:"items"` | |
Count int `json:"count"` | |
Profiles []struct { | |
ID int `json:"id"` | |
FirstName string `json:"first_name"` | |
LastName string `json:"last_name"` | |
IsClosed bool `json:"is_closed"` | |
CanAccessClosed bool `json:"can_access_closed"` | |
Sex int `json:"sex"` | |
ScreenName string `json:"screen_name"` | |
Photo50 string `json:"photo_50"` | |
Photo100 string `json:"photo_100"` | |
Online int `json:"online"` | |
} `json:"profiles"` | |
Groups []struct { | |
ID int `json:"id"` | |
Name string `json:"name"` | |
ScreenName string `json:"screen_name"` | |
IsClosed int `json:"is_closed"` | |
Type string `json:"type"` | |
Photo50 string `json:"photo_50"` | |
Photo100 string `json:"photo_100"` | |
Photo200 string `json:"photo_200"` | |
} `json:"groups"` | |
} `json:"response"` | |
} | |
func ParseMalesJSON(json_str string) (int, int, string) { | |
r := AutoGeneratedJSON{} | |
err := json.Unmarshal([]byte(json_str), &r) | |
if err != nil { | |
Prln(err.Error()) | |
} | |
p := r.Response.Profiles | |
//Prln("profiles: " + I2S(len(p))) | |
male := 0 | |
female := 0 | |
owner := 0 | |
for j := 0; j < len(r.Response.Items); j++ { | |
if r.Response.Items[j].IsOwner { | |
owner = r.Response.Items[j].MemberID | |
} | |
} | |
admin := "" | |
for j := 0; j < len(p); j++ { | |
s := p[j].Sex | |
//Prln("sex[" + I2S(j) + "]:" + I2S(s)) | |
if s == 1 { | |
female++ | |
} | |
if s == 2 { | |
male++ | |
} | |
if p[j].ID == owner { | |
admin = p[j].FirstName + " " + p[j].LastName + " | " + I2S(owner) + " " + p[j].ScreenName | |
} | |
} | |
return male, female, admin | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment