forked from osmandapp/OsmAnd-iOS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cleanup-testflight.py
executable file
·144 lines (123 loc) · 6.61 KB
/
cleanup-testflight.py
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
#!/usr/bin/python
# -*- coding: utf-8 -*
import sys
import os
import getpass
import urllib.request, urllib.parse
import http.cookiejar
import html.parser
# =============================================================================================================================================================
# =============================================================================================================================================================
# =============================================================================================================================================================
class TestflightJanitor(object):
# ---------------------------------------------------------------------------------------------------------------------------------------------------------
def __init__(self):
self.cookiesJar = http.cookiejar.CookieJar()
self.cookiesHandler = urllib.request.HTTPCookieProcessor(self.cookiesJar)
self.redirectHandler = urllib.request.HTTPRedirectHandler()
self.opener = urllib.request.build_opener(self.cookiesHandler, self.redirectHandler)
# ---------------------------------------------------------------------------------------------------------------------------------------------------------
def dumpCookies(self):
for cookie in self.cookiesJar:
print(cookie.name + "=" + cookie.value)
# ---------------------------------------------------------------------------------------------------------------------------------------------------------
def getSession(self):
request = urllib.request.Request("https://www.testflightapp.com/login/")
response = self.opener.open(request);
# ---------------------------------------------------------------------------------------------------------------------------------------------------------
def login(self, username, password):
self.getSession()
data = {
"csrfmiddlewaretoken" : next(cookie.value for cookie in self.cookiesJar if cookie.name == "csrftoken"),
"username" : username,
"password" : password
}
postData = urllib.parse.urlencode(data).encode('ascii')
request = urllib.request.Request("https://www.testflightapp.com/login/", postData, {}, "www.testflightapp.com/login/")
response = self.opener.open(request);
return True
# ---------------------------------------------------------------------------------------------------------------------------------------------------------
class PagesCollectionParser(html.parser.HTMLParser):
def __init__(self):
super().__init__()
self.maxPageNumber = 0
self.prefix = "/dashboard/applications/750280/builds/?page="
def handle_starttag(self, tag, attrs):
if tag != "a":
return
for k, v in attrs:
if k != "href" or not v.startswith(self.prefix):
continue
self.maxPageNumber = max(self.maxPageNumber, int(v[len(self.prefix):]))
def handle_endtag(self, tag):
return
def handle_data(self, data):
return
def getBuilds(self, appId):
request = urllib.request.Request("https://www.testflightapp.com/dashboard/applications/%d/builds/" % (appId))
response = self.opener.open(request);
htmlContent = response.read().decode('ascii')
parser = self.PagesCollectionParser()
parser.feed(htmlContent)
print("Found %d pages with builds" % (parser.maxPageNumber))
builds = []
for pageIndex in range(parser.maxPageNumber):
buildsOnPage = self.getBuildsFromPage(appId, pageIndex)
builds = builds + buildsOnPage
return builds
# ---------------------------------------------------------------------------------------------------------------------------------------------------------
class BuildsOnPageParser(html.parser.HTMLParser):
def __init__(self):
super().__init__()
self.builds = []
self.prefix = "/dashboard/builds/crashes/"
def handle_starttag(self, tag, attrs):
if tag != "a":
return
for k, v in attrs:
if k != "href" or not v.startswith(self.prefix):
continue
self.builds.append(int(v[len(self.prefix):]))
def handle_endtag(self, tag):
return
def handle_data(self, data):
return
def getBuildsFromPage(self, appId, pageIndex):
request = urllib.request.Request("https://www.testflightapp.com/dashboard/applications/%d/builds/?page=%d" % (appId, pageIndex + 1))
response = self.opener.open(request);
htmlContent = response.read().decode('ascii')
parser = self.BuildsOnPageParser()
parser.feed(htmlContent)
print("Found %d builds on page %d" % (len(parser.builds), pageIndex + 1))
return parser.builds
# ---------------------------------------------------------------------------------------------------------------------------------------------------------
def deleteBuild(self, buildId):
print("Deleting build #%d..." % (buildId))
page = "https://www.testflightapp.com/dashboard/builds/delete/%d/" % (buildId)
data = {
"csrfmiddlewaretoken" : next(cookie.value for cookie in self.cookiesJar if cookie.name == "csrftoken")
}
postData = urllib.parse.urlencode(data).encode('ascii')
request = urllib.request.Request(page, postData, {}, page)
response = self.opener.open(request);
# ---------------------------------------------------------------------------------------------------------------------------------------------------------
def cleanup(self, username, password, appId, buildsToKeep):
ok = self.login(username, password)
if ok == False:
return False
builds = self.getBuilds(appId)
buildsToDelete = builds[buildsToKeep:]
print("Going to delete oldest %d builds from %d..." % (len(buildsToDelete), len(builds)))
for buildId in buildsToDelete:
self.deleteBuild(buildId)
return True
# =============================================================================================================================================================
# =============================================================================================================================================================
# =============================================================================================================================================================
if __name__=='__main__':
username = input('Username: ')
password = getpass.getpass("Password: ")
appId = int(input("Application ID: "))
buildsToKeep = int(input("Number of builds to keep: "))
janitor = TestflightJanitor()
sys.exit(0 if janitor.cleanup(username, password, appId, buildsToKeep) else -1)