Beispiel #1
0
def main():
    print time.strftime('%c', time.localtime())
    pwfile = __file__.replace('.py', '.json')
    login = json.load(open(pwfile))
    dbfile = __file__.replace('.py', '.sqlite')
    newfile = not os.path.exists(dbfile)
    db = sqlite3.connect(dbfile)
    cur = db.cursor()
    if newfile:
        print 'create db'
        cur.execute('create table nicorepo (key text primary key, val text)')
    url = 'http://www.nicovideo.jp/my/top/all'
    h = {'User-Agent': ua}
    s = requests.session()
    cur.execute("select val from nicorepo where key='cookies'")
    c = (cur.fetchone() or ['{}'])[0]
    cj = requests.utils.cookiejar_from_dict(json.loads(c))
    for i in range(3):
        try:
            print 'load cookie'
            r = s.get(url, headers=h, cookies=cj, timeout=5)
            assert r.ok
            if 'login' in r.url:
                print 'login'
                r = s.post('https://account.nicovideo.jp/api/v1/login', login, headers=h, timeout=5)
                assert r.ok
                print 'save cookie'
                r = s.get(url, headers=h, timeout=5)
                assert r.ok
                c = json.dumps(requests.utils.dict_from_cookiejar(s.cookies))
                cur.execute('insert or ignore into nicorepo(key, val) values (?, ?)',
                            ['cookies', c])
            break
        except:
            print 'retry', i
            time.sleep(10)
    else:
        print 'out of retry'
        return
    q = pyquery.PyQuery(r.text)
    sent = set([0])
    for i in q('.log'):
        msg = q('.log-body', i).text()
        val = q('.log-target-info', i).text()
        href = q('.log-target a', i).attr('href')
        if not href:
            continue
        key = href.split('?')[0]
        msg = '\n'.join([msg, val, key])
        print msg.encode('utf8')
        cur.execute('insert or ignore into nicorepo(key, val) values (?, ?)',
                    [key, val])
        if cur.lastrowid not in sent and not ignore(msg):
            bot.sendMessage(chat_id=60069004, text=msg)
            sent.add(cur.lastrowid)
    db.commit()
    db.close()
Beispiel #2
0
def main():
    logging.basicConfig(format='%(asctime)s %(levelname)s %(message)s', level=logging.DEBUG)
    for (term, dst) in items:
        try:
            url = 'http://www.nyaa.se/?page=rss&term=' + term
            logging.info('Polling %s', url)
            for e in feedparser.parse(url).entries:
                if not exist(e.title):
                    logging.info('Adding %s %s', e.id, e.title)
                    args = ['transmission-remote', '-w', dst, '-a', e.link]
                    subprocess.check_call(args)
                    chat_id = (-7918260 if 'jojo' in term else 60069004)
                    bot.sendMessage(chat_id=chat_id, text=e.id)
                    mark(e.title)
                    time.sleep(3)
            time.sleep(3)
        except KeyboardInterrupt:
            break
        except:
            logging.error(traceback.format_exc())
            time.sleep(60)