-
Notifications
You must be signed in to change notification settings - Fork 0
/
redirect.py
executable file
·120 lines (90 loc) · 3.67 KB
/
redirect.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
#! /usr/bin/env python
"""
Simple HTTP URL redirector
Shreyas Cholia 10/01/2015
usage: redirect.py [-h] [--port PORT] [--ip IP] redirect_url
HTTP redirect server
positional arguments:
redirect_url
optional arguments:
-h, --help show this help message and exit
--port PORT, -p PORT port to listen on
--ip IP, -i IP host interface to listen on
From https://gist.github.com/shreddd/b7991ab491384e3c3331
"""
import SimpleHTTPServer
import SocketServer
import sys
import argparse
def redirect_handler_factory():
"""Returns a request handler class
XXX Migrate to Python 3 (except macOS 10.12 does not ship it).
XXX Should really concatenate the answers when there are hits for
with and without a data track.
XXX Should also think about if and how rate limiting applies here.
XXX Can reconstruct DiscID from FreeDB ID? Only need the last
part of the path (e.g. NNN-0013682b-00aee95b-9711560c), the rest
can be reconstructed. See https://musicbrainz.org/search, or
maybe rather trace where the FreeDB stuff in libmusicbrainz comes
from.
XXX The server could be eliminated by configuration options to
remap discs in sndchk.
"""
class RedirectHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
# Path must start with a slash--use join() instead?
discid2path = {
# The best of The Doors, disc 2
"ejrfwBlgmFMxnD3AiDOE4AqqMI8-":
"/accuraterip/b/b/b/dBAR-018-002c7bbb-02480762-1f124813.bin",
# Flash
"fRovc1tARQUreSsbP4zvcg6HCO4-":
"/accuraterip/5/1/0/dBAR-003-00035015-000bc47d-2806f604.bin",
# In lust we trust
"7vqa6F_exuZ4paBsKHn0GO.tCrw-":
"/accuraterip/b/2/8/dBAR-011-0013682b-00aee95b-9711560c.bin",
# Live 2003
"ELycL.Syz4kvJT.qkCNjBvHRSJA-":
"/accuraterip/9/7/e/dBAR-012-001f0e79-01222683-ba10f60d.bin",
# Tour de France
"IRLOxr5FVyI.c1he6OXS7hrNxZM-":
"/accuraterip/a/a/3/dBAR-003-0002c3aa-0009f0cc-30065d04.bin"
}
def do_GET(self):
#print "Got command " + self.command
#print "Got path " + self.path
#self.send_response(301)
#self.send_header('Location', url)
# XXX Should probably log this instead of printing it.
for ua in self.headers.getheaders('User-Agent'):
print "User-Agent: ", ua
try:
url = 'http://www.accuraterip.com' + self.discid2path[self.path]
except KeyError:
pass
if 'url' in locals():
# send_response() must be called before send_header()
# XXX read the documentation
self.send_response(301)
self.send_header('Location', url)
else:
self.send_response(404)
# response = 'http://www.accuraterip.com' + self.discid2path[self.path]
# print "Will respond with " + response
# self.send_response(404)
self.end_headers()
return RedirectHandler
def main():
parser = argparse.ArgumentParser(description='HTTP redirect server')
port = 1984
# See OpenBSD's httpd for arguments, etc.
parser.add_argument(
'--ip', '-i', action="store", default="",
help='address to listen on')
# parser.add_argument('redirect_url', action="store")
myargs = parser.parse_args()
redirectHandler = redirect_handler_factory()
handler = SocketServer.TCPServer((myargs.ip, port), redirectHandler)
print("Listening on port %d" % port)
handler.serve_forever()
if __name__ == "__main__":
main()