summaryrefslogtreecommitdiff
path: root/web/forums.py
blob: ad733b83711b627251644db9d530c7d16bf8dd26 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#!/usr/bin/python3

import requests
from bs4 import BeautifulSoup # pip3 install beautifulsoup4

jar = requests.cookies.RequestsCookieJar()
fakeUserAgent = "Mozilla/5.0 (X11; CrOS aarch64 13261.0.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4161.0 Safari/537.36"

with open("login.txt") as f:
    username = f.read().replace('\n', '')
with open("password.txt") as f:
    password = f.read().replace('\n', '')
sid = ""
forumId = 79
topicId = 21488

def isAuthed():
    global jar, sid
    return len(jar) >= 3 and len(sid) >= 1

## Obtain the SID
r=requests.get("https://forums.themanaworld.org/index.php", headers={"User-Agent": fakeUserAgent})

## Create the initial (real) cookie jar
jar = r.cookies
for k in jar.keys():
    if k.endswith("_sid"):
        sid = r.cookies[k]

## Integrity check: Session ID
assert len(sid) > 1

## Pre-Login
r=requests.get("https://forums.themanaworld.org/ucp.php?mode=login&redirect=index.php&sid=%s" % sid, headers={"Origin": "https://forums.themanaworld.org", "Referer": "https://forums.themanaworld.org/", "User-Agent": fakeUserAgent}, cookies=jar)

## Retrieve creation time and token
soup = BeautifulSoup(r.text)
creationTag = None
tokenTag = None
for k in soup.find_all('input', type="hidden"):
    if k["name"] == "creation_time":
        creationTag = k["value"]
    elif k["name"] == "form_token":
        tokenTag = k["value"]

## Integrity check: Login Tags
assert creationTag is not None
assert tokenTag is not None


login = dict()
login["username"] = username
login["password"] = password
login["autologin"] = True #"on"
login["viewonline"] = True #"on"
login["redirect"] = "./ucp.php?mode=login&redirect=index.php"
login["creation_time"] = creationTag
login["form_token"] = tokenTag
login["sid"] = sid
#login["redirect"]="index.php"
login["login"]="Login"

## Login
r=requests.post("https://forums.themanaworld.org/ucp.php?mode=login&redirect=index.php&login=Login", headers={"Origin": "https://forums.themanaworld.org", "Referer": "https://forums.themanaworld.org/ucp.php?mode=login&redirect=index.php&sid=%s" % sid, "User-Agent": fakeUserAgent, "Content-Type": "application/x-www-form-urlencoded"}, cookies=jar, data=login, allow_redirects=False)

while r.status_code == 302:
    jar.update(r.cookies)
    print("Redirecting to %s..." % r.headers['Location'])
    r=requests.get(r.headers['Location'], cookies=jar)

del password
assert isAuthed()

#################################################################################
## Parse the news

def markup(r):
    r=r.replace('##0', '')
    r=r.replace('##1', '')
    r=r.replace('##2', '')
    r=r.replace('##3', '')
    r=r.replace('##4', '')
    r=r.replace('##5', '')
    r=r.replace('##6', '')
    r=r.replace('##7', '')
    r=r.replace('##8', '')
    r=r.replace('##9', '')
    r=r.replace('##B', '[b]')
    r=r.replace('##b', '[/b]')
    r=r.replace('[@@', '[url="')
    r=r.replace('|', '"]')
    r=r.replace('@@]', '[/url]')
    return r

def nn(r):
    return r.replace('\n', '')

SUBJECT=""
MESSAGE=""
with open("../update/news.txt") as f:
#with open("/home/jesusalva/projetos/tmw2/tools/update/news.txt") as f:
    for i in f:
        if ('##0 Actual Release: ##1' in i):
            SUBJECT=nn(i.replace('##0 Actual Release: ##1',''))
            MESSAGE+="[size=150][b]»%s[/b][/size]\n[hr]" % SUBJECT
        elif 'possible to skip music download under' in i:
            continue
        else:
            MESSAGE+=markup(i)

#################################################################################
## Reply-To something
rp=requests.get("https://forums.themanaworld.org/posting.php?mode=reply&f=%d&t=%d" % (forumId, topicId), headers={"Origin": "https://forums.themanaworld.org", "Referer": "https://forums.themanaworld.org/", "User-Agent": fakeUserAgent}, cookies=jar)

data = dict()
data["subject"] = SUBJECT
data["message"] = MESSAGE
data["post"] = "Submit"
data["topic_cur_post_id"] = None
data["creation_time"] = None
data["form_token"] = None
#data["lock_topic"] = False
#data["lock_post"] = False

soup = BeautifulSoup(rp.text)
for k in soup.find_all('input', type="hidden"):
    if k["name"] == "post":
        data["post"] = k["value"]
    elif k["name"] == "creation_time":
        data["creation_time"] = k["value"]
    elif k["name"] == "form_token":
        data["form_token"] = k["value"]
    elif k["name"] == "topic_cur_post_id":
        data["topic_cur_post_id"] = k["value"]

## Submit new form data
r=requests.post("https://forums.themanaworld.org/posting.php?mode=reply&f=%d&sid=%s&t=%d" % (forumId, sid, topicId), headers={"Origin": "https://forums.themanaworld.org", "Referer": "https://forums.themanaworld.org/posting.php?mode=reply&f=%d&t=%d" % (forumId, topicId), "User-Agent": fakeUserAgent}, cookies=jar, data=data, allow_redirects=False)


#################################################################################
## Logout
for k in jar.keys():
    if k.endswith("_sid"):
        sid = r.cookies[k]
r=requests.get("https://forums.themanaworld.org/ucp.php?mode=logout&sid=%s" % (sid), headers={"Origin": "https://forums.themanaworld.org", "Referer": "https://forums.themanaworld.org/", "User-Agent": fakeUserAgent}, cookies=jar)