def delete_photo(cookiefile, qqid, photo_json, sid): skey = get_cookie_value(cookiefile, "skey") gtk = get_gtk(skey) qqid = int(qqid) if qqid > 2000000000: url = DELETE_URL % gtk elif qqid > 1100000000: url = DELETE_URL2 % gtk elif qqid > 1000000000: url = DELETE_URL3 % gtk elif qqid < 200000000: url = DELETE_URL2 % gtk else: url = DELETE_URL4 % gtk data = generate_delete(qqid, photo_json['albumid'], photo_json['lloc']) logger.info("Deleting failed photo %s %s", qqid, url) result = post(url, data, cookiefile=cookiefile).decode('gbk').encode('utf8') logger.debug("Deleting %s result: %s", qqid, result) delete_json = extract_json_from_html(result, 'frameElement.callback') if (delete_json['code'] < 0): logger.warn("Deleting %s failed %s code %s, %s", qqid, delete_json['message'].encode('utf8'), delete_json['code'], delete_json['subcode']) if delete_json['code'] == -3000: log_paperboy('Need login(del) xks %s' % sid) return result
def upload_photo2(cookiefile, full_filename, qqid, sid): skey = get_cookie_value(cookiefile, "skey") boundary = "----" + gen_boundary() qqid = int(qqid) if qqid > 2000000000: urls = [UPLOAD_URL % boundary, UPLOAD_URL2 % boundary, UPLOAD_URL3 % boundary, UPLOAD_URL4 % boundary] elif qqid > 1100000000: urls = [UPLOAD_URL2 % boundary, UPLOAD_URL % boundary, UPLOAD_URL3 % boundary, UPLOAD_URL4 % boundary] elif qqid > 1000000000: urls = [UPLOAD_URL3 % boundary, UPLOAD_URL2 % boundary, UPLOAD_URL % boundary, UPLOAD_URL4 % boundary] elif qqid < 200000000: urls = [UPLOAD_URL2 % boundary, UPLOAD_URL % boundary, UPLOAD_URL3 % boundary, UPLOAD_URL4 % boundary] else: urls = [UPLOAD_URL4 % boundary, UPLOAD_URL2 % boundary, UPLOAD_URL3 % boundary, UPLOAD_URL % boundary] photo_json = {} for url in urls: datagen, headers = generate_multipart_photo(qqid, skey, full_filename, boundary) headers['User-Agent'] = DEFAULT_UA headers['Accept'] = 'text/x-json,application/json;q=0.9,*/*;q=0.8' headers['Accept-Language'] = 'en-US,en;q=0.5' logger.info("Uploading photo %s %s -> %s", qqid, full_filename, url) result = post(url, datagen, headers=headers, cookiefile=cookiefile, is_accept_ending=True, ext_handlers=get_handlers()) logger.debug("Uploaded %s %s -> %s : result %s", qqid, full_filename, url, result) photo_json = simplejson.loads(result.replace("_Callback(","").replace(");",""))['data'] if photo_json.has_key('error'): logger.warn("Post failed qq %s -> %s %s %s", qqid, url, photo_json['error'], photo_json['msg'].encode('utf8')) if photo_json['error'] == -503: log_paperboy("Need login(photo) xks %s" % sid) break else: break return photo_json
def upload_photo(cookiefile, full_filename, qqid): skey = get_cookie_value(cookiefile, "skey") boundary = "----" + gen_boundary() qqid = int(qqid) if qqid > 2000000000: url = UPLOAD_URL % boundary elif qqid > 1100000000: url = UPLOAD_URL2 % boundary elif qqid > 1000000000: url = UPLOAD_URL3 % boundary elif qqid < 200000000: url = UPLOAD_URL2 % boundary else: url = UPLOAD_URL4 % boundary datagen, headers = generate_multipart_photo(qqid, skey, full_filename, boundary) headers['User-Agent'] = DEFAULT_UA headers['Accept'] = 'text/x-json,application/json;q=0.9,*/*;q=0.8' headers['Accept-Language'] = 'en-US,en;q=0.5' logger.info("Uploading photo %s %s -> %s", qqid, full_filename, url) result = post(url, datagen, headers=headers, cookiefile=cookiefile, is_accept_ending=True, ext_handlers=get_handlers()) logger.debug("Uploaded %s %s -> %s : result %s", qqid, full_filename, url, result) return result
def upload_photo2(cookiefile, full_filename, qqid, sid): skey = get_cookie_value(cookiefile, "skey") boundary = "----" + gen_boundary() qqid = int(qqid) if qqid > 2000000000: urls = [ UPLOAD_URL % boundary, UPLOAD_URL2 % boundary, UPLOAD_URL3 % boundary, UPLOAD_URL4 % boundary ] elif qqid > 1100000000: urls = [ UPLOAD_URL2 % boundary, UPLOAD_URL % boundary, UPLOAD_URL3 % boundary, UPLOAD_URL4 % boundary ] elif qqid > 1000000000: urls = [ UPLOAD_URL3 % boundary, UPLOAD_URL2 % boundary, UPLOAD_URL % boundary, UPLOAD_URL4 % boundary ] elif qqid < 200000000: urls = [ UPLOAD_URL2 % boundary, UPLOAD_URL % boundary, UPLOAD_URL3 % boundary, UPLOAD_URL4 % boundary ] else: urls = [ UPLOAD_URL4 % boundary, UPLOAD_URL2 % boundary, UPLOAD_URL3 % boundary, UPLOAD_URL % boundary ] photo_json = {} for url in urls: datagen, headers = generate_multipart_photo(qqid, skey, full_filename, boundary) headers['User-Agent'] = DEFAULT_UA headers['Accept'] = 'text/x-json,application/json;q=0.9,*/*;q=0.8' headers['Accept-Language'] = 'en-US,en;q=0.5' logger.info("Uploading photo %s %s -> %s", qqid, full_filename, url) result = post(url, datagen, headers=headers, cookiefile=cookiefile, is_accept_ending=True, ext_handlers=get_handlers()) logger.debug("Uploaded %s %s -> %s : result %s", qqid, full_filename, url, result) photo_json = simplejson.loads( result.replace("_Callback(", "").replace(");", ""))['data'] if photo_json.has_key('error'): logger.warn("Post failed qq %s -> %s %s %s", qqid, url, photo_json['error'], photo_json['msg'].encode('utf8')) if photo_json['error'] == -503: log_paperboy("Need login(photo) xks %s" % sid) break else: break return photo_json
def post_shuoshuo(cookiefile, photofile, content, sid=0, schedule_ts=0, post_id=0): if type(cookiefile) is unicode: cookiefile = cookiefile.encode('utf8') if type(photofile) is unicode: photofile = photofile.encode('utf8') if type(content) is unicode: content = content.encode('utf8') qqid = 0 result = False try: qqid = get_cookie_value(cookiefile, "ptui_loginuin") photo_json = upload_photo2(cookiefile, photofile, qqid, sid) if photo_json.has_key('error'): logger.error("Post failed qq %s, xks %s, %s, %s, %s --> %s reason %s", qqid, sid, cookiefile, photofile, content, photo_json['error'], photo_json['msg'].encode('utf8')) log_post_error(post_id, "%s : %s" % (photo_json['error'], photo_json['msg'].encode('utf8'))) return False albumid = photo_json['albumid'] photoid = photo_json['lloc'] width = photo_json['width'] height = photo_json['height'] special_url = photo_json['micro']['url'] #TODO: save into wb_photojson if failed post_result = post_content(cookiefile, qqid, content, albumid, photoid, width, height, special_url, schedule_ts) #import pdb; pdb.set_trace() if (not schedule_ts and post_result.find("content_box") > 0): result = True elif schedule_ts: content_json = extract_json_from_html(post_result, 'frameElement.callback') if content_json['code'] < 0: logger.warn("Post failed qq %s, xks %s, %s, %s, %s, %s, %s, %s", qqid, sid, cookiefile, photofile, content, content_json['message'].encode('utf8'), content_json['code'], content_json['subcode']) log_post_error(post_id, "%s,%s : %s" % (content_json['code'], content_json['subcode'], content_json['message'].encode('utf8'))) if content_json['code'] == -3000: log_paperboy('Need login(content) xks %s' % sid) delete_photo(cookiefile, qqid, photo_json, sid) elif content_json.has_key('richinfo'): result = True else: logger.warn("Post failed qq %s, xks %s, %s, %s, %s", qqid, sid, cookiefile, photofile, content) delete_photo(cookiefile, qqid, photo_json, sid) log_post_error(post_id, "-2 : %s" % post_result) if (not schedule_ts and post_result.find("content_box") > 0) or (schedule_ts and post_result.find("richinfo") > 0): result = True except KeyboardInterrupt: raise except: logger.error("Post failed qq %s, xks %s, %s, %s, %s --> reason %s", qqid, sid, cookiefile, photofile, content, traceback.format_exc()) log_post_error(post_id, "-1 : exception" ) return result
def post_content(cookiefile, qqid, content, albumid, photoid, photo_width, photo_height, special_url=None, schedule_ts=0): skey = get_cookie_value(cookiefile, "skey") gtk = get_gtk(skey) if schedule_ts: url = SHUOSHUO_TIMER_URL % gtk else: url = SHUOSHUO_URL % gtk data = generate_content(qqid, content, albumid, photoid, photo_width, photo_height, special_url, schedule_ts) logger.info("Posting content %s with photo", qqid) result = post(url, data, cookiefile=cookiefile) logger.debug("Posting %s result: %s, timer %s", qqid, result, schedule_ts) return result
def post_content_wget(cookies_path, data): skey = get_cookie_value(cookies_path, "skey") gtk = get_gtk(skey) cmd = """wget -S -U "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0)" --load-cookies %s --post-data "%s" "http://taotao.qq.com/cgi-bin/emotion_cgi_publish_v6?g_tk=%s" """ % (cookies_path, data, gtk) return os.system(cmd)
def post_content_wget(cookies_path, data): skey = get_cookie_value(cookies_path, "skey") gtk = get_gtk(skey) cmd = """wget -S -U "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0)" --load-cookies %s --post-data "%s" "http://taotao.qq.com/cgi-bin/emotion_cgi_publish_v6?g_tk=%s" """ % ( cookies_path, data, gtk) return os.system(cmd)
def post_shuoshuo(cookiefile, photofile, content, sid=0, schedule_ts=0, post_id=0): if type(cookiefile) is unicode: cookiefile = cookiefile.encode('utf8') if type(photofile) is unicode: photofile = photofile.encode('utf8') if type(content) is unicode: content = content.encode('utf8') qqid = 0 result = False try: qqid = get_cookie_value(cookiefile, "ptui_loginuin") photo_json = upload_photo2(cookiefile, photofile, qqid, sid) if photo_json.has_key('error'): logger.error( "Post failed qq %s, xks %s, %s, %s, %s --> %s reason %s", qqid, sid, cookiefile, photofile, content, photo_json['error'], photo_json['msg'].encode('utf8')) log_post_error( post_id, "%s : %s" % (photo_json['error'], photo_json['msg'].encode('utf8'))) return False albumid = photo_json['albumid'] photoid = photo_json['lloc'] width = photo_json['width'] height = photo_json['height'] special_url = photo_json['micro']['url'] #TODO: save into wb_photojson if failed post_result = post_content(cookiefile, qqid, content, albumid, photoid, width, height, special_url, schedule_ts) #import pdb; pdb.set_trace() if (not schedule_ts and post_result.find("content_box") > 0): result = True elif schedule_ts: content_json = extract_json_from_html(post_result, 'frameElement.callback') if content_json['code'] < 0: logger.warn( "Post failed qq %s, xks %s, %s, %s, %s, %s, %s, %s", qqid, sid, cookiefile, photofile, content, content_json['message'].encode('utf8'), content_json['code'], content_json['subcode']) log_post_error( post_id, "%s,%s : %s" % (content_json['code'], content_json['subcode'], content_json['message'].encode('utf8'))) if content_json['code'] == -3000: log_paperboy('Need login(content) xks %s' % sid) delete_photo(cookiefile, qqid, photo_json, sid) elif content_json.has_key('richinfo'): result = True else: logger.warn("Post failed qq %s, xks %s, %s, %s, %s", qqid, sid, cookiefile, photofile, content) delete_photo(cookiefile, qqid, photo_json, sid) log_post_error(post_id, "-2 : %s" % post_result) if (not schedule_ts and post_result.find("content_box") > 0) or ( schedule_ts and post_result.find("richinfo") > 0): result = True except KeyboardInterrupt: raise except: logger.error("Post failed qq %s, xks %s, %s, %s, %s --> reason %s", qqid, sid, cookiefile, photofile, content, traceback.format_exc()) log_post_error(post_id, "-1 : exception") return result