def repost_friend(self, iterval): # 每相隔多久时间,转发一次朋友圈的微博 # 朋友圈 statuses = (self.get_friend_status( cnt = 100 ))["statuses"] if statuses: for item in statuses: #if item['text'].find('nicol:') != -1: # log_info(u"微博模块",u"Alreadyed include me, skip it[%d]!\n" % item['id'], self.log) # continue; #if item['id'] == wb_cfg.UID : # log_info(u"微博模块",u"My Own's weibo, skip it[%d]!\n" % item['id'], self.log) # print item['text'] # continue; if not REPOST_WHITE.has_key(item['user']['id']): log_info(u"微博模块",u"Not in the repost_white list, skip [%d]!\n" % item['id'], self.log) continue else: try: ret = self.client.post('statuses/repost', id = item['id'], status = u"[Nicol]强势转发微博", is_comment = 1) except Exception, e: log_warn(u"微博模块",u"Runtime Error:%s" % e, self.log) continue log_info(u"微博模块",u"Repost item for [%d]!\n" % item['id'],self.log) #等待一会儿 time.sleep(random.randint(300,1000))
def repost_friend(self, iterval): # 每相隔多久时间,转发一次朋友圈的微博 # 朋友圈 statuses = (self.get_friend_status(cnt=100))["statuses"] if statuses: for item in statuses: #if item['text'].find('nicol:') != -1: # log_info(u"微博模块",u"Alreadyed include me, skip it[%d]!\n" % item['id'], self.log) # continue; #if item['id'] == wb_cfg.UID : # log_info(u"微博模块",u"My Own's weibo, skip it[%d]!\n" % item['id'], self.log) # print item['text'] # continue; if not REPOST_WHITE.has_key(item['user']['id']): log_info( u"微博模块", u"Not in the repost_white list, skip [%d]!\n" % item['id'], self.log) continue else: try: ret = self.client.post('statuses/repost', id=item['id'], status=u"[Nicol]强势转发微博", is_comment=1) except Exception, e: log_warn(u"微博模块", u"Runtime Error:%s" % e, self.log) continue log_info(u"微博模块", u"Repost item for [%d]!\n" % item['id'], self.log) #等待一会儿 time.sleep(random.randint(300, 1000))
def __init__(self, Sinaweibo, logger=None): self.log = logger self.sw = Sinaweibo try: self.client = Twython( wb_cfg.TW_APP_KEY, wb_cfg.TW_APP_SECRET, wb_cfg.TW_AC_TOKEN, wb_cfg.TW_AC_TOKEN_SECRET ) log_info(u"推特模块", u"User %s Login Successful!" % wb_cfg.TW_OWN_ID, self.log) except Exception: log_warn(u"推特模块", u"User %s Login Failed!" % self.TW_OWN_ID, self.log)
def __init__(self, Sinaweibo, logger=None): self.log = logger self.sw = Sinaweibo try: self.client = Twython(wb_cfg.TW_APP_KEY, wb_cfg.TW_APP_SECRET, \ wb_cfg.TW_AC_TOKEN, wb_cfg.TW_AC_TOKEN_SECRET) log_info(u"推特模块", u"User %s Login Successful!" % wb_cfg.TW_OWN_ID, self.log) except Exception: log_warn(u"推特模块", u"User %s Login Failed!" % self.TW_OWN_ID, self.log)
def get_toutiao(self): fetch_result = [] try: request = urllib2.Request(tt_main_url, headers = tt_headers) html = urllib2.urlopen(request).read() except UnicodeDecodeError: log_info(u"开发者头条模块", u"GBK/Unicode Code Decode Error!", self.log) return except Exception: log_warn(u"开发者头条模块", u"Unkown Error!", self.log) return soup = BeautifulSoup(html) contents = soup.findAll('div', attrs = {"class":"content"} ) for item in contents: title = item.find('h3', attrs = {"class":"title"}) summary = item.find('p', attrs = {"class":"summary"}) if title and summary: if title.a.string and title.a['href'] and summary.a.string: #str_msg = u"{'title':'%s', 'summary':'%s', 'link':'%s'}" % (title.a.string, summary.a.string, title.a['href']) fetch_item = dict(TITLE=title.a.string.encode('utf-8'), SUMMARY=summary.a.string.encode('utf-8'), LINK=title.a['href'].encode('utf-8')) fetch_result.append(fetch_item) log_info(u"开发者头条模块", u"Todady's Toutiao Fetched Finished!", self.log) log_info(u"开发者头条模块", u"We've Fetched %s items!" % len(fetch_result), self.log) try: fp = codecs.open(wb_cfg.TTFILE, 'ab',encoding = 'utf-8') msg = repr(fetch_result) fp.write(msg) except Exception: log_info(u"开发者头条模块",u"Read Write File Error", self.log) finally: fp.close()
def repost_to_sina(self, Sinaweibo=None): if not self.sw: log_info(u"推特模块", u"Sinaweibo object is null, just return!") return statuses = self.get_home_timeline() if statuses: for item in statuses: if not item['user']['screen_name'] in TW_WHITE_LIST: log_info( u"推特模块", u"Not in the twitter repost_white list, skip [%d]!\n" % item['id'], self.log) continue elif item['user']['screen_name'] == "taozhijiang": log_info(u"推特模块", u"Recycle, Give up!\n") break else: try: ret = self.sw.post_statuses(str_msg=item['text']) except Exception, e: log_warn(u"推特模块", u"Runtime Error:%s" % e, self.log) continue log_info(u"推特模块", u"Repost item for [%d]!\n" % item['id'], self.log) #等待一会儿 time.sleep(random.randint(200, 400))
def __init__ (self, username = wb_cfg.USER, passwd = wb_cfg.PASSWD, logger = None): self.APP_KEY = wb_cfg.APP_KEY self.APP_SECRET = wb_cfg.APP_SECRET self.CALLBACK = wb_cfg.CALLBACK self.USER = username self.PASSWD = passwd self.UID = wb_cfg.UID self.log = logger try: self.client = Client(self.APP_KEY, self.APP_SECRET, self.CALLBACK, token=None, username=self.USER, password=self.PASSWD) log_info(u"微博模块", u"User %s Login Successful!" % self.USER, self.log) except Exception: log_warn(u"微博模块", u"User %s Login Failed!" % self.USER, self.log)
def __init__(self, username=wb_cfg.USER, passwd=wb_cfg.PASSWD, logger=None): self.APP_KEY = wb_cfg.APP_KEY self.APP_SECRET = wb_cfg.APP_SECRET self.CALLBACK = wb_cfg.CALLBACK self.USER = username self.PASSWD = passwd self.UID = wb_cfg.UID self.log = logger try: self.client = Client(self.APP_KEY, self.APP_SECRET, self.CALLBACK, token=None, username=self.USER, password=self.PASSWD) log_info(u"微博模块", u"User %s Login Successful!" % self.USER, self.log) except Exception: log_warn(u"微博模块", u"User %s Login Failed!" % self.USER, self.log)
def repost_to_sina(self, Sinaweibo=None): if not self.sw: log_info(u"推特模块", u"Sinaweibo object is null, just return!") return statuses = self.get_home_timeline() if statuses: for item in statuses: if not item["user"]["screen_name"] in TW_WHITE_LIST: log_info(u"推特模块", u"Not in the twitter repost_white list, skip [%d]!\n" % item["id"], self.log) continue elif item["user"]["screen_name"] == "taozhijiang": log_info(u"推特模块", u"Recycle, Give up!\n") break else: try: ret = self.sw.post_statuses(str_msg=item["text"]) except Exception, e: log_warn(u"推特模块", u"Runtime Error:%s" % e, self.log) continue log_info(u"推特模块", u"Repost item for [%d]!\n" % item["id"], self.log) # 等待一会儿 time.sleep(random.randint(200, 400))
from jd_utils import log_info, log_warn from sinaweibo import SinaWeibo from twitter_me import Twitter_Me import jd_utils import jd_logger import wb_cfg from toutiao_spider import Toutiao if __name__ == "__main__": if not os.path.exists(wb_cfg.LOG_PATH): os.makedirs(wb_cfg.LOG_PATH) main_log = jd_logger.Jd_Logger(u"微博机器人", verbose = False) log_info(u"MAIN",u"Application Starting Up!", main_log) if len(os.sys.argv) < 2: log_warn(u"MAIN",u"No arguments specified, give up!", main_log) sys.exit(-1) app_t = os.sys.argv[1] if app_t == "twitter": sw = SinaWeibo() t = Twitter_Me(sw) t.repost_to_sina() elif app_t == "toutiao": sw = SinaWeibo(logger = main_log) #好友圈转发
from jd_utils import log_info, log_warn from sinaweibo import SinaWeibo from twitter_me import Twitter_Me import jd_utils import jd_logger import wb_cfg from toutiao_spider import Toutiao if __name__ == "__main__": if not os.path.exists(wb_cfg.LOG_PATH): os.makedirs(wb_cfg.LOG_PATH) main_log = jd_logger.Jd_Logger(u"微博机器人", verbose=False) log_info(u"MAIN", u"Application Starting Up!", main_log) if len(os.sys.argv) < 2: log_warn(u"MAIN", u"No arguments specified, give up!", main_log) sys.exit(-1) app_t = os.sys.argv[1] if app_t == "twitter": sw = SinaWeibo() t = Twitter_Me(sw) t.repost_to_sina() elif app_t == "toutiao": sw = SinaWeibo(logger=main_log) #好友圈转发