ohlala-switcher

Ohlala User-Agent Switcher for surf
git clone git://r-36.net/ohlala-switcher
Log | Files | Refs | README | LICENSE

ohlala-switcher (1216B)


      1 #!/usr/bin/env python
      2 # coding=utf-8
      3 #
      4 # Copy me if you can.
      5 # by 20h
      6 #
      7 
      8 import os
      9 import sys
     10 import getopt
     11 import requests
     12 import json
     13 
     14 def usage(app):
     15 	app = os.path.basename(app)
     16 	print("usage: %s [-h] [uri|[file|-]]" % (app), file=sys.stderr)
     17 	sys.exit(1)
     18 
     19 def main(args):
     20 	try:
     21 		opts, largs = getopt.getopt(args[1:], "h")
     22 	except getopt.GetoptError as err:
     23 		print(str(err))
     24 		usage(args[0])
     25 
     26 	defaulturi = "https://raw.githubusercontent.com/dillbyrne/random-agent-spoofer/master/data/json/useragents.json"
     27 	for o, a in opts:
     28 		if o == "-h":
     29 			usage(args[0])
     30 		else:
     31 			assert False, "unhandled option"
     32 
     33 	if len(largs) > 0:
     34 		sstr = largs[0]
     35 	else:
     36 		sstr = defaulturi
     37 
     38 	if sstr == "-":
     39 		fd = sys.stdin.read()
     40 		js = json.read(fd)
     41 		fd.close()
     42 	elif os.path.exists(sstr):
     43 		fd = open(sstr, "r")
     44 		js = json.read(fd)
     45 		fd.close()
     46 	else:
     47 		fd = requests.get(sstr)
     48 		js = fd.json()
     49 		fd.close()
     50 
     51 	for ptype in js:
     52 		if not "list" in ptype:
     53 			continue
     54 		for ostype in ptype["list"]:
     55 			if not "useragents" in ostype:
     56 				continue
     57 			for ua in ostype["useragents"]:
     58 				if not "useragent" in ua:
     59 					continue
     60 				print("%s" % (ua["useragent"]))
     61 
     62 	return 0
     63 
     64 if __name__ == "__main__":
     65 	sys.exit(main(sys.argv))
     66