2014年10月29日 星期三

[NodeJS] domain-parser

晚上7:58
NodeJS - domain-parser
前言:
剛好想要這個功能,但是沒有現成的
有找到php版本,不過判斷的情況不是很完美
只好自己寫一個 XD

套件名稱:
domain-parser
https://www.npmjs.org/package/domain-parser

Version: 0.0.5 (2014/10/29)

Source Code on Github
https://github.com/kilfu0701/node-domain-parser


安裝方式 (Install)
npm install domain-parser

用法 (Usage)
var dp = require('domain-parser');

var result = dp('www.google.com.tw').domainName;
console.log(result);
// google.com.tw

result = dp('direct.jp-bank.japanpost.jp').domainName;
console.log(result);
// japanpost.jp

2014年6月10日 星期二

[Chome] 非chrome web store的套件被強制停用

凌晨2:51

2014年1月3日 星期五

[Python] sqlalchemy 輸出json格式

清晨6:57
sqlalchemy 輸出json格式

前言:
剛好有人發問,寫了一個範例,

做個記錄一下,也許以後自己會用到 XD

範例:
http://pastie.org/8595769
# -*- coding: utf-8 -*-
import hashlib
import sqlalchemy
from sqlalchemy import Column, Integer, String, ForeignKey, DateTime, Table
from sqlalchemy.schema import MetaData
from sqlalchemy.orm import relationship
from sqlalchemy.orm import Bundle
from sqlalchemy.orm import lazyload
from sqlalchemy.orm.session import Session, sessionmaker
from sqlalchemy.ext.declarative import declarative_base

engine = sqlalchemy.create_engine('mysql+mysqldb://user:passwd@host/your_db?charset=utf8')
connection = engine.connect()
session = Session(bind=connection)

my_declarative_base = lambda cls: declarative_base(cls=cls)

@my_declarative_base
class Base(object):
    @property
    def columns(self):
        return [ c.name for c in self.__table__.columns ]

    @property
    def columnitems(self):
        return dict([ (c, getattr(self, c)) for c in self.columns ])

    def __repr__(self):
        return u'{}({})'.format(self.__class__.__name__, self.columnitems)

    def to_json(self):
        return self.columnitems

    def rec_json(self, key_str):
        result = self.columnitems
        result[key_str] = getattr(self, key_str).columnitems

        return result
        
class User(Base):
    __tablename__ = 'user'
    __table_args__ = {
        'mysql_engine': 'InnoDB',
        'mysql_charset': 'utf8'
    }

    id = Column(Integer, primary_key=True)
    name = Column(String(64))
    username = Column(String(64))
    password = Column(String(256))
    address_id = Column(Integer, ForeignKey('user_address.id'))
    address = relationship("Address", lazy="joined")

    def __init__(self, name, username, password):
        self.name = name
        self.username = username
        self.password = hashlib.sha1(password).hexdigest()

    def __repr__(self):
       return u"[User('%s','%s', '%s')]" % \
           (self.name, self.username, self.password)


class Address(Base):
    __tablename__ = 'user_address'
    __table_args__ = {
        'mysql_engine': 'InnoDB',
        'mysql_charset': 'utf8'
    }

    id = Column(Integer, primary_key=True)
    address = Column(String(256), nullable=False)

    def __init__(self, address):
        self.address = address

    def __repr__(self):
        return u"[Address('%s')]" % self.address
        
        

# insert test data...
"""
Base.metadata.create_all(engine)

user_1 = User("user1", "username1", "password_1")
user_2 = User("user2", "username2", "password_2")
address_1 = Address(u'臺灣凱達格蘭大道')
user_1.address = address_1
user_2.address = address_1
session.add(user_1)
session.add(user_2)
session.commit()
"""

for r in session.query(User).options(lazyload('address')):
    print r.rec_json('address')


"""
Output Result:

{'username': 'username1', 'address_id': 17L, 'name': 'user1', 'address': {'id': 17L, 'address': u'\u81fa\u7063\u51f1\u9054\u683c\u862d\u5927\u9053'},
 'password': '3833b3a1c69cf71a31d86cb5bb4d3866789b4d1e', 'id': 33L}
{'username': 'username2', 'address_id': 17L, 'name': 'user2', 'address': {'id': 17L, 'address': u'\u81fa\u7063\u51f1\u9054\u683c\u862d\u5927\u9053'},
 'password': '148dfdc3c539d35004cb808ca84e17ff962af744', 'id': 34L}

"""

參考資料(Refs):
1. http://chimerhapsody.blogspot.tw/2013/09/python-sqlalchemy-orm-part-3-end.html
2. http://docs.sqlalchemy.org/en/rel_0_9/orm/relationships.html
3. http://blogs.gnome.org/danni/2013/03/07/generating-json-from-sqlalchemy-objects/


2014年1月1日 星期三

[Solr] 安裝Solr 4.6 in Debian

凌晨4:14
安裝Solr 4.6

前言:
距離上次寫的安裝設定很遙遠,而且那時候用的還是比較舊的版本,

就趁這次重新安裝的時候,順便寫一篇給自己以後來用 XD

安裝步驟:

這邊我是都把所有東西放到 /opt/ 底下,

依個人需求自行調整。

1. 下載 solr 4.6
cd /opt/

wget http://ftp.tc.edu.tw/pub/Apache/lucene/solr/4.6.0/solr-4.6.0.tgz
tar zxvf solr-4.6.0.tgz

2. 下載 java sdk (http://www.oracle.com/technetwork/java/javase/downloads/index.html)
tar zxvf jdk-7u45-linux-i586.gz

3. 複製solr-4.6/example到/opt/solr/
cp -pr /opt/solr-4.6.0/example/ /opt/solr/

4. 啟動solr
方法1:
cd /opt/solr/
java -Dsolr.solr.home=multicore -jar start.jar

方法2:
wget https://gist.github.com/Polzme/7367523/raw/9ccf8b040b70eca214497dc6ee2b23e9abb0b8ef/solr -O /etc/init.d/solr
chmod +x /etc/init.d/solr
# 依個人需求,修改java的路徑
vim /etc/init.d/solr

#DAEMON=/usr/bin/java
DAEMON=/opt/jdk1.7.0_45/bin/java
service solr start

5. 檢查是否已經啟動?
http://localhost:8983/solr/

6. 設定開機的時候 啟動solr
update-rc.d solr defaults

7. 設定iptables

只允許localhost access 8983的port
iptables -A INPUT -p tcp -s localhost --dport 8983 -j ACCEPT
iptables -A INPUT -p tcp --dport 8983 -j DROP



參考資料(Refs):
1. http://apache.osuosl.org/lucene/solr/ref-guide/apache-solr-ref-guide-4.6.pdf
2. http://wiki.apache.org/solr/SolrTomcat
3. https://coderwall.com/p/0s3flw
4. https://cwiki.apache.org/confluence/display/solr/Running+Solr+on+Tomcat
5. https://gist.github.com/Polzme/7367523/