Last active
August 29, 2015 14:15
-
-
Save objmagic/049c224c49f9a04403b7 to your computer and use it in GitHub Desktop.
fast_block
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"api1":[ | |
{"consumer_key": "", | |
"consumer_secret": "", | |
"access_token": "", | |
"access_token_secret": ""}, | |
{"consumer_key": "", | |
"consumer_secret": "", | |
"access_token": "", | |
"access_token_secret": ""} | |
], | |
"api2":[] | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import read_config | |
import get_follower | |
import threading | |
import tweepy | |
import sys | |
import time | |
import datetime | |
from sets import Set | |
def hms_string(sec_elapsed): | |
h = int(sec_elapsed / (60 * 60)) | |
m = int((sec_elapsed % (60 * 60)) / 60) | |
s = sec_elapsed % 60. | |
return "{}:{:>02}:{:>05.2f}".format(h, m, s) | |
# for each jerk, use all apis to block their follower | |
def get_following(api1): | |
me1 = api1.me() | |
foing = [] | |
friends_cursor = tweepy.Cursor(api1.friends_ids, id=me1.id) | |
for id in friends_cursor.items(): | |
foing.append(str(id)) | |
return foing | |
lock = threading.Lock() | |
num_blocked = 0 | |
def get_jerk_follower(jid, following): | |
f = open("./block_list/" + jid + "foer.txt") | |
jerk_foer = [line[:-1] for line in f] | |
f.close() | |
return [j for j in jerk_foer if j not in following] | |
def block_jerk_thread(api, jl): | |
for jid in jl: | |
try: | |
api.create_block(jid) | |
with lock: | |
global num_blocked | |
print 'blocking ' + str(jid) | |
num_blocked = num_blocked + 1 | |
except tweepy.TweepError, e: | |
print 'Error when processing: ', jid | |
print str(e) | |
def block_jerk(apis, jl): | |
num_apis = len(apis) | |
num_jerks = len(jl) | |
fst_size = num_jerks - num_jerks % num_apis | |
chunk_size = fst_size / num_apis | |
thread = [] | |
for i in xrange(num_apis): | |
sub_jl = jl[i * chunk_size: (i + 1) * chunk_size] | |
t = threading.Thread(target=block_jerk_thread, args=(apis[i], sub_jl)) | |
t.daemon = True | |
thread.append(t) | |
rest_jl = jl[fst_size: num_jerks] | |
t = threading.Thread(target=block_jerk_thread, args=(apis[0], rest_jl)) | |
thread.append(t) | |
for t in thread: | |
t.start() | |
for t in thread: | |
t.join() | |
delta_time_str = hms_string(time.time() - t1) | |
def block_all_jerks(apis1, jerks): | |
following = get_following(apis1[0]) | |
dt_list = [] | |
jerk_follower = Set([]) | |
for jerk in jerks: | |
for jid in get_jerk_follower(jerk, following): | |
jerk_follower.add(jid) | |
t1 = time.time() | |
block_jerk(apis1, list(jerk_follower)) | |
total_time = time.time() - t1 | |
delta_time_str = hms_string(total_time) | |
print 'Blocking time: ' + delta_time_str | |
global num_blocked | |
print 'Num blocked: ', num_blocked | |
if __name__ == '__main__': | |
t1 = time.time() | |
# `apis1' are the APIs used by your main account | |
# for shouya, maybe u don't need `apis2' which is APIs for your vice account | |
apis1, apis2 = read_config.parse_api_json("api_config_shouya.json") | |
jerks = read_config.parse_jerks_json("jerks_shouya.json") | |
get_follower.get_followers(apis1, jerks) | |
block_all_jerks(apis1, jerks) | |
dt = time.time() - t1 | |
print 'Total time: ' + hms_string(dt) | |
print 'Done!' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import read_config | |
import threading | |
import tweepy | |
import sys | |
def get_followers(apis, jerks): | |
num_apis = len(apis) | |
jerks_size = len(jerks) | |
fs, rems = jerks_size - jerks_size % num_apis, jerks_size % num_apis | |
jsize = fs / num_apis | |
thread_list = [] | |
for i in xrange(num_apis): | |
js = jerks[i * jsize: (i + 1) * jsize] | |
t = threading.Thread(target = dump_follower, args=(apis[i], js)) | |
t.daemon = True | |
thread_list.append(t) | |
for thread in thread_list: | |
thread.start() | |
for thread in thread_list: | |
thread.join() | |
dump_follower(apis[0], jerks[fs: jerks_size]) | |
print "Got all followers!" | |
def dump_follower(api, jerks): | |
for jerk in jerks: | |
print "Getting " + jerk + "'s followers" | |
sys.stdout.flush() | |
f = open('./block_list/' + jerk + "foer.txt", 'w') | |
for page in tweepy.Cursor(api.followers_ids, jerk, count=5000).pages(): | |
for foer_id in page: | |
f.write(str(foer_id) + '\n') | |
print 'Got ' + jerk + '\'s followers' | |
f.close() | |
if __name__ == '__main__': | |
apis, x = read_config.parse_api_json("api_config_shouya.json") | |
jerks = read_config.parse_jerks_json("jerks_shouya.json") | |
get_followers(apis, jerks) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"jerks":[ | |
"imyangjf" | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment