Bjoern Olausson

opera_blocklist.py
Monday, 23 November 2009 23:20
  1. # -*- coding: utf-8 -*-
  2. #
  3. #--------------------------------------------------------------------------------
  4. #opera_blocklist.py v0.1, Copyright Bjoern Olausson
  5. #--------------------------------------------------------------------------------
  6. #This program is free software; you can redistribute it and/or modify
  7. #it under the terms of the GNU General Public License as published by
  8. #the Free Software Foundation; either version 2 of the License, or
  9. #(at your option) any later version.
  10. #
  11. #This program is distributed in the hope that it will be useful,
  12. #but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. #GNU General Public License for more details.
  15. #
  16. #To view the license visit
  17. #http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  18. #or write to
  19. #Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  20. #--------------------------------------------------------------------------------
  21. #--------------------------------------------------------------------------------
  22. #
  23. #http://pgl.yoyo.org/adservers/ holds a regular updated list of hostnames/IPs which
  24. #provide advertising services such as banners. Many banners are just annoying because
  25. #they are animated. To get rid of many of these, feed opera with this list.
  26. #To stay up to date, just run this script weekly via cron.
  27. #
  28. #Another alternative would be, to redirect the advertising hostnames to localhost by adding
  29. #them to /etc/hosts, which would affect the whole system. A ready to use hostfile and updatescript
  30. #can be found here: http://hostsfile.mine.nu/ (http://hostsfile.mine.nu/downloads/updatehosts.sh.txt)
  31. #
  32. import os, urllib, errno, sys, shutil, re
  33.  
  34. #~~~~~~~~~~~~~~ CONFIGURE VARIABLES BELOW TO FIT YOUR SYSTEM ~~~~~~~~~~~~~~
  35.  
  36. #Path to Opera urlfilter.ini
  37. URLFILTER = "/home/blub/.opera/urlfilter.ini"
  38. #URL to the blocklist in plaintext operafilter format
  39. BLOCKLIST_URL = "http://pgl.yoyo.org/as/serverlist.php?hostformat=operafilter&showintro=0&mimetype=plaintext"
  40. #List of URLs or part matching multiple URLs you want to remove from the blocklist (for what reason ever...)
  41. #KILL = ['URL','adserver','banner',...]
  42. KILL = []
  43.  
  44. #~~~~~~~~~~~~~~ NOTHING TO CONFIGUER BELOW THIS LINE ~~~~~~~~~~~~~~
  45.  
  46. URLFILTER_NEW = URLFILTER.replace(".ini", ".ini.new")
  47. URLFILTER_BACKUP = URLFILTER.replace(".ini", ".ini.bac")
  48.  
  49. INIPATH = os.path.split(URLFILTER)[0]
  50. os.chdir(INIPATH)
  51.  
  52. #Define a function for file selection
  53. def linefilter(content,mark):
  54. start = False
  55. content = iter(content)
  56. for line in content :
  57. if line.strip() == mark :
  58. start = True
  59. if start and not line.strip() == mark :
  60. yield line.strip("
    "
    )
  61.  
  62. #Open the new file for writing
  63. URLFILTERNEW = open(URLFILTER_NEW, "w")
  64.  
  65. #Write copy the first lines up to and including [exclude] to the new file
  66. for headline in open(URLFILTER, "r"):
  67. if headline.strip() != "[exclude]":
  68. URLFILTERNEW.write(headline)
  69. if headline.strip() == "[exclude]":
  70. URLFILTERNEW.write(headline)
  71. break
  72.  
  73. #Open the BLocklist from URL and convert it to a list
  74. try:
  75. BLOCKLIST_CONT = list(urllib.urlopen(BLOCKLIST_URL))
  76. except IOError:
  77. raise Exception("Failed to download the Blocklist")
  78.  
  79. #Check if the downloaded content is what we need (Search for "operafilter" keyword)
  80. check = re.compile("operafilter")
  81. if not check.search("".join(BLOCKLIST_CONT)):
  82. raise Exception("Failed to download a correct Blocklist")
  83.  
  84. #Get all lines after "[exclude]" from the original Opera urlfilter.ini file and the downloaded Blocklist
  85. OPERA_EXCLUDELIST = list(linefilter(open(URLFILTER, "r"), "[exclude]"))
  86. BLOCK_EXCLUDELIST = list(linefilter(BLOCKLIST_CONT, "[exclude]"))
  87.  
  88. #Merge the two lists
  89. for value in OPERA_EXCLUDELIST:
  90. BLOCK_EXCLUDELIST.append(value)
  91.  
  92. #Remove duplicate entries and sort the merged list
  93. UNIQUE_EXCLUDE = list(set(BLOCK_EXCLUDELIST))
  94. UNIQUE_EXCLUDE.sort()
  95.  
  96. #Get the items of the which should be removed
  97. KILL_ITEMS = []
  98. for exclude in KILL:
  99. exclude = re.compile(exclude)
  100. for index in xrange(len(UNIQUE_EXCLUDE)):
  101. if exclude.search(UNIQUE_EXCLUDE[index]):
  102. KILL_ITEMS.append(UNIQUE_EXCLUDE[index])
  103.  
  104. #Remove the all found items
  105. for items in KILL_ITEMS:
  106. UNIQUE_EXCLUDE.remove(items)
  107.  
  108. #Write the merged list to urlfilter.ini.new
  109. URLFILTERNEW.write("".join([line+"
    "
    for line in UNIQUE_EXCLUDE]))
  110. URLFILTERNEW.close()
  111.  
  112. #Backup the old Oper urlfiter.ini file to urlfiter.ini.bac
  113. shutil.copy(URLFILTER, URLFILTER_BACKUP)
  114.  
  115. #Overwrite the old urlfiter.ini with the new urlfiter.ini.new
  116. os.rename(URLFILTER_NEW, os.path.split(URLFILTER)[1])
  117.  
  118.  
Attachments:
FileFile sizeLast Modified
Download this file (opera_blocklist.py)opera_blocklist.py4 Kb11/23/09 23:59
Last Updated ( Tuesday, 24 November 2009 00:02 )
 

Add comment


Security code
Refresh

Comments

Qt Ambassador

Qt Ambassador

www. is deprecated

Banner

Play OGG

Banner

Gixen

web2sms

Banner