atmosphereのアンドロイド日記

方向性がわからないです

Twitterのidが取得済みか未取得か確認するプログラムを書きました

まず、どのようにしてtwitterのidが取得済みか未取得か、というのを知るのかというのは「http://twitter.com/hoge」のhogeの部分がidなので、このhogeの部分を乱数生成してtwitter.com/のあとにくっつけて、そのurlが存在しているかを例外処理を利用して確認しています。

並列処理していないので重いです。私のパソコンでは3文字idまでならプログラムが動きました。

 

 

 

# -*- coding: utf-8 -*-

import random
import urllib.request
from string import ascii_lowercase, digits
 
 
strings = lambda : ascii_lowercase + digits + '_'
 
def id_cnt(num):
    return len(strings()) ** num
 
def random_str(loop):
    box = []
    while 1:
        if len(box) == id_cnt(loop) : return box
        id_gen = ''.join([random.choice(strings()) for i in range(loop)])
        if id_gen not in box:
            box.append(id_gen)
 
def twitter_acc(url):
    try:
        t_url = urllib.request.urlopen(url)
        print('{0} {1}'.format(url[19:], 'is found'))
        t_url.close()
    except Exception as e:
        print('{0} {1}'.format(url[19:], 'is not found'))
 
def end(loop_2):
    liv = random_str(loop_2)
    for p in range(len(liv)):
        inp_url = 'http://twitter.com/{}'.format(liv[p])
        twitter_acc(inp_url)

end(hoge); のhogeが探索するidの文字の数です。この関数を呼ぶと動作します