瀏覽代碼

Bugfixes, allow saving of broken comments and warn instead of error only

Cammy 7 年之前
父節點
當前提交
bd84963d76
共有 2 個文件被更改,包括 51 次插入28 次删除
  1. 40 23
      pyinstalive/comments.py
  2. 11 5
      pyinstalive/downloader.py

+ 40 - 23
pyinstalive/comments.py

@@ -78,14 +78,17 @@ class CommentsDownloader(object):
 		starting_offset = 0
 		encoding_tag = self.broadcast.get('encoding_tag')
 		while True:
-			comments_res = self.api.replay_broadcast_comments(
-				self.broadcast.get('id'), starting_offset=starting_offset, encoding_tag=encoding_tag)
-			starting_offset = comments_res.get('ending_offset', 0)
-			comments = comments_res.get('comments', [])
-			comments_collected.extend(comments)
-			if not comments_res.get('comments') or not starting_offset:
-				break
-			time.sleep(4)
+			try:
+				comments_res = self.api.replay_broadcast_comments(
+					self.broadcast.get('id'), starting_offset=starting_offset, encoding_tag=encoding_tag)
+				starting_offset = comments_res.get('ending_offset', 0)
+				comments = comments_res.get('comments', [])
+				comments_collected.extend(comments)
+				if not comments_res.get('comments') or not starting_offset:
+					break
+				time.sleep(4)
+			except:
+				pass
 
 		if comments_collected:
 			self.broadcast['comments'] = comments_collected
@@ -120,6 +123,8 @@ class CommentsDownloader(object):
 			subtitles_timeline[created_at_utc] = comment_list
 
 		if subtitles_timeline:
+			comment_errors = 0
+			total_comments = 0
 			timestamps = sorted(subtitles_timeline.keys())
 			mememe = False
 			subs = []
@@ -129,29 +134,41 @@ class CommentsDownloader(object):
 				if clip_start < 0:
 					clip_start = 0
 
-				log = ''
+				comments_log = ''
 				for c in t:
-					if python_version.startswith('3'):
-						if (c.get('user', {}).get('is_verified')):
-							log += '{}{}\n\n'.format(time.strftime('%H:%M:%S\n', time.gmtime(clip_start)), '{} {}: {}'.format(c.get('user', {}).get('username'), "(v)", c.get('text')))
-						else:
-							log += '{}{}\n\n'.format(time.strftime('%H:%M:%S\n', time.gmtime(clip_start)), '{}: {}'.format(c.get('user', {}).get('username'), c.get('text')))
-					else:
-						if not wide_build:
+					try:
+						if python_version.startswith('3'):
 							if (c.get('user', {}).get('is_verified')):
-								log += '{}{}\n\n'.format(time.strftime('%H:%M:%S\n', time.gmtime(clip_start)), '{} {}: {}'.format(c.get('user', {}).get('username'), "(v)", c.get('text').encode('ascii', 'ignore')))
+								comments_log+= '{}{}\n\n'.format(time.strftime('%H:%M:%S\n', time.gmtime(clip_start)), '{} {}: {}'.format(c.get('user', {}).get('username'), "(v)", c.get('text')))
 							else:
-								log += '{}{}\n\n'.format(time.strftime('%H:%M:%S\n', time.gmtime(clip_start)), '{}: {}'.format(c.get('user', {}).get('username'), c.get('text').encode('ascii', 'ignore')))
+								comments_log += '{}{}\n\n'.format(time.strftime('%H:%M:%S\n', time.gmtime(clip_start)), '{}: {}'.format(c.get('user', {}).get('username'), c.get('text')))
 						else:
+							if not wide_build:
+								if (c.get('user', {}).get('is_verified')):
+									comments_log += '{}{}\n\n'.format(time.strftime('%H:%M:%S\n', time.gmtime(clip_start)), '{} {}: {}'.format(c.get('user', {}).get('username'), "(v)", c.get('text').encode('ascii', 'ignore')))
+								else:
+									comments_log += '{}{}\n\n'.format(time.strftime('%H:%M:%S\n', time.gmtime(clip_start)), '{}: {}'.format(c.get('user', {}).get('username'), c.get('text').encode('ascii', 'ignore')))
+							else:
+								if (c.get('user', {}).get('is_verified')):
+									comments_log += '{}{}\n\n'.format(time.strftime('%H:%M:%S\n', time.gmtime(clip_start)), '{} {}: {}'.format(c.get('user', {}).get('username'), "(v)", c.get('text')))
+								else:
+									comments_log += '{}{}\n\n'.format(time.strftime('%H:%M:%S\n', time.gmtime(clip_start)), '{}: {}'.format(c.get('user', {}).get('username'), c.get('text')))
+					except:
+						comment_errors += 1
+						total_comments += 1
+						try:
 							if (c.get('user', {}).get('is_verified')):
-								log += '{}{}\n\n'.format(time.strftime('%H:%M:%S\n', time.gmtime(clip_start)), '{} {}: {}'.format(c.get('user', {}).get('username'), "(v)", c.get('text')))
+								comments_log += '{}{}\n\n'.format(time.strftime('%H:%M:%S\n', time.gmtime(clip_start)), '{} {}: {}'.format(c.get('user', {}).get('username'), "(v)", c.get('text').encode('ascii', 'ignore')))
 							else:
-								log += '{}{}\n\n'.format(time.strftime('%H:%M:%S\n', time.gmtime(clip_start)), '{}: {}'.format(c.get('user', {}).get('username'), c.get('text')))
-
-				subs.append(log)
+								comments_log += '{}{}\n\n'.format(time.strftime('%H:%M:%S\n', time.gmtime(clip_start)), '{}: {}'.format(c.get('user', {}).get('username'), c.get('text').encode('ascii', 'ignore')))
+						except:
+							pass
 
+				subs.append(comments_log)
+				
 			with codecs.open(log_file, 'w', 'utf-8-sig') as log_outfile:
 				if python_version.startswith('2') and not wide_build:
 					log_outfile.write('This log was generated using Python {:s} without wide unicode support. This means characters such as emojis are not saved.\nUser comments without any text usually are comments that only had emojis.\nBuild Python 2 with the --enable-unicode=ucs4 flag or use Python 3 for full unicode support.\n\n'.format(python_version) + ''.join(subs))
 				else:
-					log_outfile.write(''.join(subs))
+					log_outfile.write(''.join(subs))
+			return comment_errors, total_comments

+ 11 - 5
pyinstalive/downloader.py

@@ -364,15 +364,18 @@ def get_replay_comments(instagram_api, broadcast, comments_json_file, broadcast_
 		try:
 			if comments_downloader.comments:
 				comments_log_file = comments_json_file.replace('.json', '.log')
-				CommentsDownloader.generate_log(
+				comment_errors, total_comments = CommentsDownloader.generate_log(
 					comments_downloader.comments, broadcast.get('published_time'), comments_log_file,
 					comments_delay=0)
-				if len(comments_downloader.comments) == 1:
+				if total_comments == 1:
 					log("[I] Successfully saved 1 comment to logfile.", "GREEN")
 					seperator("GREEN")
 					return True
 				else:
-					log("[I] Successfully saved {} comments to logfile.".format(len(comments_downloader.comments)), "GREEN")
+					if comment_errors:
+						log("[W] Successfully saved {:s} comments to logfile but {:s} comments are (partially) missing.".format(str(total_comments), str(comment_errors)), "YELLOW")
+					else:
+						log("[I] Successfully saved {:s} comments to logfile.".format(str(total_comments)), "GREEN")
 					seperator("GREEN")
 					return True
 			else:
@@ -407,7 +410,7 @@ def get_live_comments(instagram_api, broadcast, comments_json_file, broadcast_do
 			if comments_downloader.comments:
 				comments_downloader.save()
 				comments_log_file = comments_json_file.replace('.json', '.log')
-				CommentsDownloader.generate_log(
+				comment_errors, total_comments = CommentsDownloader.generate_log(
 					comments_downloader.comments, settings.current_time, comments_log_file,
 					comments_delay=broadcast_downloader.initial_buffered_duration)
 				if len(comments_downloader.comments) == 1:
@@ -415,7 +418,10 @@ def get_live_comments(instagram_api, broadcast, comments_json_file, broadcast_do
 					seperator("GREEN")
 					return True
 				else:
-					log("[I] Successfully saved {} comments to logfile.".format(len(comments_downloader.comments)), "GREEN")
+					if comment_errors:
+						log("[W] Successfully saved {:s} comments to logfile but {:s} comments are (partially) missing.".format(str(total_comments), str(comment_errors)), "YELLOW")
+					else:
+						log("[I] Successfully saved {:s} comments to logfile.".format(str(total_comments)), "GREEN")
 					seperator("GREEN")
 					return True
 			else: