#!/usr/bin/python
#-*- coding: UTF-8 -*-

import pickle
from binocular import Bibliomaniac

class DumpTruck(Bibliomaniac):

	def __init__(self, name):
		Bibliomaniac.__init__(self, name)

	def makeFilter(self, keyword, lastpage): # When you turn key, recent article list become two filters' base
		self.bindPicture(keyword, lastpage)

		self.routes = self.fixList(self.routes)

		l_f = open(self.name + '_lightFilter.bin', 'wb') # l_f for light_file; Copying of last recent article list
		pickle.dump(self.routes, l_f)
		l_f.close()
		del l_f

		f_f = open(self.name + '_fineFilter.bin', 'wb') # f_f for fine_file; Incremental filter list from your turn key 
		pickle.dump(self.routes, f_f)
		f_f.close()
		del f_f
	
	def dumpRecent(self, keyword, lastpage):
		self.bindPicture(keyword, lastpage)

		self.routes = self.fixList(self.routes)
	
		r_f = open(self.name + '_recentTweet.bin', 'wb') # r_f for recent_file
		pickle.dump(self.routes, r_f)
		r_f.close()
		del r_f
	
	def judgeArticle(self): # Filter core

		r_f = open(self.name + '_recentTweet.bin', 'rb') 
		recentTweet = pickle.load(r_f)
		r_f.close()
		del r_f

		l_f = open(self.name + '_lightFilter.bin', 'rb') 
		lightFilter = pickle.load(l_f)
		l_f.close()
		del l_f

		f_f = open(self.name + '_fineFilter.bin', 'rb') 
		self.fineFilter = pickle.load(f_f)
		f_f.close()
		del f_f

		i = 0
		while i < 5: # Old one? or New one?
			
			if recentTweet[i] in lightFilter:
				print ' ' * 50 + 'Old one...Already tweeted.'

			elif recentTweet[i] in self.fineFilter: # Server stuttering situation procedure
				print '?' * 50 + 'Old one...Already tweeted.'
				print ' ' * 50 + 'Unstable Server Warning!!!'

			else:
				self.basket.insert(0, recentTweet[i])
				print '+' * 50 + 'New article!, Sent buffer.'

			i += 1

		self.getBackLost(self.keyword)

		for e in self.basket: # Update fine filter
			self.fineFilter.insert(0, e)

		f_f = open(self.name + '_fineFilter.bin', 'wb')
		pickle.dump(self.fineFilter, f_f)
		f_f.close()
		del f_f
		print ' ' * 50 + 'Updated _fineFilter.bin.'

		l_f = open(self.name + '_lightFilter.bin', 'wb') # Update light filter
		pickle.dump(recentTweet, l_f)
		l_f.close()
		del l_f
		print ' ' * 50 + 'Updated _lightFilter.bin.'



@@ -369,29 +369,28 @@
 .routes)%0A%0A%09%09
-l_f =
+with
  open(self.n
@@ -425,238 +425,100 @@
 b') 
-# l_f for light_file; Copying of last recent article list%0A%09%09pickle.dump(self.routes, l_f)%0A%09%09l_f.close()%0A%09%09del l_f%0A%0A%09%09f_f = open(self.name + '_fineFilter.bin', 'wb') # f_f for fine_file; Incremental filter list from your turn key %0A
+as l_f, open(self.name + '_fineFilter.bin', 'wb') as f_f:%0A%09%09%09pickle.dump(self.routes, l_f)%0A%09
 %09%09pi
@@ -549,32 +549,8 @@
 _f)%0A
-%09%09f_f.close()%0A%09%09del f_f%0A
 %09%0A%09d
@@ -674,21 +674,20 @@
 es)%0A%09%0A%09%09
-r_f =
+with
  open(se
@@ -726,30 +726,17 @@
 b') 
-# r_f for recent_file%0A
+as r_f:%0A%09
 %09%09pi
@@ -767,32 +767,8 @@
 _f)%0A
-%09%09r_f.close()%0A%09%09del r_f%0A
 %09%0A%09d
@@ -811,13 +811,12 @@
 %0A%0A%09%09
-r_f =
+with
  ope
@@ -859,239 +859,159 @@
 b') 
-%0A%09%09recentTweet = pickle.load(r_f)%0A%09%09r_f.close()%0A%09%09del r_f%0A%0A%09%09l_f = open(self.name + '_lightFilter.bin', 'rb') %0A%09%09lightFilter = pickle.load(l_f)%0A%09%09l_f.close()%0A%09%09del l_f%0A%0A%09%09f_f = open(self.name + '_fineFilter.bin', 'rb') %0A%09%09self.fine
+as r_f, open(self.name + '_lightFilter.bin', 'rb') as l_f, open(self.name + '_fineFilter.bin', 'rb') as f_f:%0A%09%09%09recentTweet = pickle.load(r_f)%0A%09%09%09light
 Filt
@@ -1031,36 +1031,50 @@
 oad(
-f
+l
 _f)%0A%09%09
-f_f.close()%0A%09%09del 
+%09self.fineFilter = pickle.load(
 f_f
+)
 %0A%0A%09%09
@@ -1624,13 +1624,62 @@
 %0A%0A%09%09
-f_f =
+with open(self.name + '_fineFilter.bin', 'wb') as f_f,
  ope
@@ -1686,36 +1686,37 @@
 n(self.name + '_
-fine
+light
 Filter.bin', 'wb
@@ -1717,17 +1717,26 @@
 ', 'wb')
-%0A
+ as l_f:%0A%09
 %09%09pickle
@@ -1768,31 +1768,8 @@
 f)%0A%09
-%09f_f.close()%0A%09%09del f_f%0A
 %09%09pr
@@ -1814,82 +1814,9 @@
 n.'%0A
-%0A
 %09
-%09l_f = open(self.name + '_lightFilter.bin', 'wb') # Update light filter%0A
 %09%09pi
@@ -1848,31 +1848,8 @@
 f)%0A%09
-%09l_f.close()%0A%09%09del l_f%0A
 %09%09pr

simplify verbose code