added proxy and tor support
This commit is contained in:
parent
972165018c
commit
e4af5bbd6a
31
microblog.py
31
microblog.py
@ -373,13 +373,32 @@ if __name__ == "__main__":
|
|||||||
with open(config["file_output"], 'w') as f:
|
with open(config["file_output"], 'w') as f:
|
||||||
print(json.dumps(profile), file=f)
|
print(json.dumps(profile), file=f)
|
||||||
|
|
||||||
import urllib.request as http
|
|
||||||
def fn1(webring_config): # come up with better name later
|
def fn1(webring_config): # come up with better name later
|
||||||
def fetch(follow_list):
|
def fetch(follow_list):
|
||||||
|
import urllib.request as http
|
||||||
|
def support_tor(proxy_var="https_proxy"):
|
||||||
|
if proxy_var not in os.environ:
|
||||||
|
return False
|
||||||
|
proxy = os.environ[proxy_var]
|
||||||
|
if proxy.find("socks://") < 0:
|
||||||
|
return False
|
||||||
|
import socket
|
||||||
|
import socks
|
||||||
|
host = proxy[proxy.rfind('/') + 1: proxy.rfind(':')]
|
||||||
|
port = proxy[proxy.rfind(':') + 1:]
|
||||||
|
socks.setdefaultproxy(
|
||||||
|
socks.PROXY_TYPE_SOCKS5, host, int(port))
|
||||||
|
socket.socket = socks.socksocket
|
||||||
|
return True
|
||||||
|
|
||||||
other_people = []
|
other_people = []
|
||||||
|
proxy_handler = http.ProxyHandler(None) # use OS vars
|
||||||
|
if support_tor("https_proxy") or support_tor("http_proxy"):
|
||||||
|
proxy_handler = http.ProxyHandler({})
|
||||||
|
conn = http.build_opener(proxy_handler)
|
||||||
for someone in follow_list:
|
for someone in follow_list:
|
||||||
try:
|
try:
|
||||||
with http.urlopen(someone) as response:
|
with conn.open(someone) as response:
|
||||||
data = response.read()
|
data = response.read()
|
||||||
other_people.append(json.loads(data))
|
other_people.append(json.loads(data))
|
||||||
except http.HTTPError as e:
|
except http.HTTPError as e:
|
||||||
@ -389,9 +408,7 @@ if __name__ == "__main__":
|
|||||||
print(e, file=sys.stderr)
|
print(e, file=sys.stderr)
|
||||||
print("\t =>", someone, file=sys.stderr)
|
print("\t =>", someone, file=sys.stderr)
|
||||||
return other_people
|
return other_people
|
||||||
# list_of_json_objs = []
|
|
||||||
# with open("meta.json",'r') as f:
|
|
||||||
# list_of_json_objs.append(json.loads(f.read()))
|
|
||||||
list_of_json_objs = fetch(webring_config["list"])
|
list_of_json_objs = fetch(webring_config["list"])
|
||||||
list_of_json_objs.sort(
|
list_of_json_objs.sort(
|
||||||
key=lambda e: e["last-updated"], reverse=True)
|
key=lambda e: e["last-updated"], reverse=True)
|
||||||
@ -423,7 +440,6 @@ if __name__ == "__main__":
|
|||||||
__lastupdated__= strftime(
|
__lastupdated__= strftime(
|
||||||
"%Y %b %d", localtime(epoch_timestamp))
|
"%Y %b %d", localtime(epoch_timestamp))
|
||||||
))
|
))
|
||||||
# print(html, file=sys.stderr)
|
|
||||||
return rendered
|
return rendered
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
@ -488,3 +504,6 @@ if __name__ == "__main__":
|
|||||||
except toml.decoder.TomlDecodeError:
|
except toml.decoder.TomlDecodeError:
|
||||||
traceback.print_exc()
|
traceback.print_exc()
|
||||||
print("\n\tYour configuration file is malformed.")
|
print("\n\tYour configuration file is malformed.")
|
||||||
|
except ModuleNotFoundError:
|
||||||
|
traceback.print_exc()
|
||||||
|
print("\n\tIf the module is 'socks', Tor support has recently been added (install PySocks package)")
|
||||||
|
Loading…
x
Reference in New Issue
Block a user