Skip to content

Commit

Permalink
refactor paths
Browse files Browse the repository at this point in the history
  • Loading branch information
kliner committed Nov 3, 2017
1 parent ec98b58 commit 2294308
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 20 deletions.
10 changes: 10 additions & 0 deletions pybili/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,12 @@
import os
import logging

__author__ = 'kliner'
__version__ = '0.3.1'

# init config & temp dir
home = os.path.expanduser("~")
__workdir__ = os.path.join(home, '.pybili')
if not os.path.exists(__workdir__):
os.makedirs(__workdir__)
__loglevel__ = logging.DEBUG
3 changes: 2 additions & 1 deletion pybili/bili.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@
import sys
import pybili
import logging
import os.path

reload(sys)
sys.setdefaultencoding('utf-8')

HOST = 'livecmt-2.bilibili.com'
PORT = 788
BUFFER_SIZE = 128 * 1024
logging.basicConfig(filename='bili.log', level=logging.DEBUG)
logging.basicConfig(filename=os.path.join(pybili.__workdir__, 'bili.log'), level=pybili.__loglevel__)

DEBUG = 0

Expand Down
17 changes: 13 additions & 4 deletions pybili/bili_config.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#!/usr/bin/python
#coding=utf-8
import os.path
from shutil import copyfile
import pybili
import os
import sys
import ConfigParser

Expand Down Expand Up @@ -64,10 +66,16 @@ class Config(object):
data = {}
cookies = {}

def __init__(self, path = os.path.expanduser("~") + '/.pybili.conf'):
def __init__(self, path = os.path.join(pybili.__workdir__, 'config')):
if not os.path.isfile(path):
print path
print 'warning! please set the config file!'
old_path = os.path.join(os.path.expanduser("~"), '.pybili.conf')
if os.path.isfile(old_path):
print 'updating config...'
copyfile(old_path, path)
print 'config updated! new path: %s' % path
else:
print path
print 'warning! please set the config file!'
return
self.path = path
self.read(path)
Expand Down Expand Up @@ -170,3 +178,4 @@ def main():

if __name__ == '__main__':
main()

9 changes: 6 additions & 3 deletions pybili/bili_sender.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
import thread
import logging
import logging.handlers
import ocr
import os.path
import pybili

SEND_URL = 'http://live.bilibili.com/msg/send'
TV_URL = 'http://api.live.bilibili.com/SmallTV/join'
Expand All @@ -20,8 +21,8 @@
class Sender(object):

def _initLogger(self, logger):
logger.setLevel(logging.DEBUG)
ch = logging.handlers.TimedRotatingFileHandler('bili_sender.log', when='midnight')
logger.setLevel(pybili.__loglevel__)
ch = logging.handlers.TimedRotatingFileHandler(os.path.join(pybili.__workdir__, 'bili_sender.log'), when='midnight')
logger.addHandler(ch)
# create formatter
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
Expand Down Expand Up @@ -163,6 +164,8 @@ def queryFreeSilver(self):
return int(r['data']['time_end'] - cur)

def startFreeSilverThread(self):
print 'init ocr function...'
import ocr
if self.cookies:
print 'checking free silver coins...'
thread.start_new_thread(self.checkFreeSilver, ())
Expand Down
22 changes: 12 additions & 10 deletions pybili/ocr.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import os.path
from PIL import Image
import training
import pybili
import urllib

DEBUG = 0

Expand All @@ -16,20 +18,23 @@ def recognize(path):
pix /= 128
pix = 1 - pix

if DEBUG: consolePrint(pix)
if DEBUG:
consolePrint(pix)
training.train()

# load trained data
global trained_data
p = 'trained_data.json'
if os.path.exists(p):
with open(p, 'r') as f:
trained_data = json.load(f)
else: trained_data = training.train()
p = os.path.join(pybili.__workdir__, 'trained_data.json')
if not os.path.exists(p):
print 'download ocr data...'
urllib.urlretrieve("https://raw.githubusercontent.com/kliner/pyBili/dev/pybili/trained_data.json", p)
print 'success'
with open(p, 'r') as f:
trained_data = json.load(f)

# do recognize
s = ''
for area in _selectImageArea(pix): s += _recognize(pix, area)
if DEBUG: print s
return eval(s)

def consolePrint(pix):
Expand All @@ -54,14 +59,12 @@ def _selectImageArea(pix):
image_area += [image_lines[i], image_lines[j-1]]
i = j
image_area = zip(image_area[::2], image_area[1::2])
#print image_area
return image_area

def _match(pix):
global trained_data
arr = pix.ravel()
key = ''.join(map(str, arr.tolist()))
#print key
for k, v in trained_data:
sim = sum([1 for i in xrange(len(v)) if v[i] == key[i]])
if sim > 620: return k
Expand All @@ -85,5 +88,4 @@ def _recognize_17(pix, area):

if __name__ == '__main__':
f = sys.argv[1]
#f = 'img.jpg'
print recognize(f)
7 changes: 5 additions & 2 deletions pybili/training.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
import json
import numpy as np
from PIL import Image
import os.path
import pybili

def train():
h, w = 40, 16
nums = '02356789'
data = []
for f in nums:
pic = Image.open('training_data/%s.png' % f).convert('L')
pic = Image.open(os.path.join(pybili.__workdir__, 'training_data', '%s.png' % f)).convert('L')
pix = np.array(pic)
pix = pix / 128
pix = 1 - pix
arr = pix.ravel()
key = ''.join(map(str, arr.tolist()))
data += [(f, key)]
with open('trained_data.json', 'w') as outfile:
with open(os.path.join(pybili.__workdir__, 'trained_data.json'), 'w') as outfile:
json.dump(data, outfile)
return data

if __name__ == '__main__':
print train()

0 comments on commit 2294308

Please sign in to comment.