timesearch.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  1. '''
  2. This is the main launch file for Timesearch.
  3. When you run `python timesearch.py get_submissions -r subredditname` or any
  4. other command, your arguments will go to the timesearch_modules file as
  5. appropriate for your command.
  6. '''
  7. import argparse
  8. import sys
  9. from voussoirkit import betterhelp
  10. from voussoirkit import vlogging
  11. from timesearch_modules import exceptions
  12. # NOTE: Originally I wanted the docstring for each module to be within their
  13. # file. However, this means that composing the global helptext would require
  14. # importing those modules, which will subsequently import PRAW and a whole lot
  15. # of other things. This made TS very slow to load which is okay when you're
  16. # actually using it but really terrible when you're just viewing the help text.
  17. def breakdown_gateway(args):
  18. from timesearch_modules import breakdown
  19. breakdown.breakdown_argparse(args)
  20. def get_comments_gateway(args):
  21. from timesearch_modules import get_comments
  22. get_comments.get_comments_argparse(args)
  23. def get_styles_gateway(args):
  24. from timesearch_modules import get_styles
  25. get_styles.get_styles_argparse(args)
  26. def get_wiki_gateway(args):
  27. from timesearch_modules import get_wiki
  28. get_wiki.get_wiki_argparse(args)
  29. def livestream_gateway(args):
  30. from timesearch_modules import livestream
  31. livestream.livestream_argparse(args)
  32. def merge_db_gateway(args):
  33. from timesearch_modules import merge_db
  34. merge_db.merge_db_argparse(args)
  35. def offline_reading_gateway(args):
  36. from timesearch_modules import offline_reading
  37. offline_reading.offline_reading_argparse(args)
  38. def index_gateway(args):
  39. from timesearch_modules import index
  40. index.index_argparse(args)
  41. def get_submissions_gateway(args):
  42. from timesearch_modules import get_submissions
  43. get_submissions.get_submissions_argparse(args)
  44. @vlogging.main_decorator
  45. def main(argv):
  46. parser = argparse.ArgumentParser(
  47. description='''
  48. The subreddit archiver
  49. The basics:
  50. 1. Collect a subreddit's submissions
  51. timesearch get_submissions -r subredditname
  52. 2. Collect the comments for those submissions
  53. timesearch get_comments -r subredditname
  54. 3. Stay up to date
  55. timesearch livestream -r subredditname
  56. ''',
  57. )
  58. subparsers = parser.add_subparsers()
  59. # BREAKDOWN
  60. p_breakdown = subparsers.add_parser(
  61. 'breakdown',
  62. description='''
  63. Generate the comment / submission counts for users in a subreddit, or
  64. the subreddits that a user posts to.
  65. Automatically dumps into a <database>_breakdown.json file
  66. in the same directory as the database.
  67. ''',
  68. )
  69. p_breakdown.add_argument(
  70. '--sort',
  71. dest='sort',
  72. type=str,
  73. default=None,
  74. help='''
  75. Sort the output by one property.
  76. Should be one of "name", "submissions", "comments", "total_posts".
  77. ''',
  78. )
  79. p_breakdown.add_argument(
  80. '-r',
  81. '--subreddit',
  82. dest='subreddit',
  83. default=None,
  84. help='''
  85. The subreddit database to break down.
  86. ''',
  87. )
  88. p_breakdown.add_argument(
  89. '-u',
  90. '--user',
  91. dest='username',
  92. default=None,
  93. help='''
  94. The username database to break down.
  95. ''',
  96. )
  97. p_breakdown.set_defaults(func=breakdown_gateway)
  98. # GET_COMMENTS
  99. p_get_comments = subparsers.add_parser(
  100. 'get_comments',
  101. aliases=['get-comments', 'commentaugment'],
  102. description='''
  103. Collect comments on a subreddit or comments made by a user.
  104. ''',
  105. )
  106. p_get_comments.add_argument(
  107. '-r',
  108. '--subreddit',
  109. dest='subreddit',
  110. default=None,
  111. )
  112. p_get_comments.add_argument(
  113. '-s',
  114. '--specific',
  115. dest='specific_submission',
  116. default=None,
  117. help='''
  118. Given a submission ID like t3_xxxxxx, scan only that submission.
  119. ''',
  120. )
  121. p_get_comments.add_argument(
  122. '-u',
  123. '--user',
  124. dest='username',
  125. default=None,
  126. )
  127. p_get_comments.add_argument(
  128. '--dont_supplement',
  129. '--dont-supplement',
  130. dest='do_supplement',
  131. action='store_false',
  132. help='''
  133. If provided, trust the pushshift data and do not fetch live copies
  134. from reddit.
  135. ''',
  136. )
  137. p_get_comments.add_argument(
  138. '--lower',
  139. dest='lower',
  140. default='update',
  141. help='''
  142. If a number - the unix timestamp to start at.
  143. If "update" - continue from latest comment in db.
  144. WARNING: If at some point you collected comments for a particular
  145. submission which was ahead of the rest of your comments, using "update"
  146. will start from that later submission, and you will miss the stuff in
  147. between that specific post and the past.
  148. ''',
  149. )
  150. p_get_comments.add_argument(
  151. '--upper',
  152. dest='upper',
  153. default=None,
  154. help='''
  155. If a number - the unix timestamp to stop at.
  156. If not provided - stop at current time.
  157. ''',
  158. )
  159. p_get_comments.set_defaults(func=get_comments_gateway)
  160. # GET_STYLES
  161. p_get_styles = subparsers.add_parser(
  162. 'get_styles',
  163. aliases=['get-styles', 'getstyles'],
  164. help='''
  165. Collect the stylesheet, and css images.
  166. ''',
  167. )
  168. p_get_styles.add_argument(
  169. '-r',
  170. '--subreddit',
  171. dest='subreddit',
  172. )
  173. p_get_styles.set_defaults(func=get_styles_gateway)
  174. # GET_WIKI
  175. p_get_wiki = subparsers.add_parser(
  176. 'get_wiki',
  177. aliases=['get-wiki', 'getwiki'],
  178. description='''
  179. Collect all available wiki pages.
  180. ''',
  181. )
  182. p_get_wiki.add_argument(
  183. '-r',
  184. '--subreddit',
  185. dest='subreddit',
  186. )
  187. p_get_wiki.set_defaults(func=get_wiki_gateway)
  188. # LIVESTREAM
  189. p_livestream = subparsers.add_parser(
  190. 'livestream',
  191. description='''
  192. Continously collect submissions and/or comments.
  193. ''',
  194. )
  195. p_livestream.add_argument(
  196. '--once',
  197. dest='once',
  198. action='store_true',
  199. help='''
  200. If provided, only do a single loop. Otherwise go forever.
  201. ''',
  202. )
  203. p_livestream.add_argument(
  204. '-c',
  205. '--comments',
  206. dest='comments',
  207. action='store_true',
  208. help='''
  209. If provided, do collect comments. Otherwise don't.
  210. If submissions and comments are BOTH left unspecified, then they will
  211. BOTH be collected.
  212. ''',
  213. )
  214. p_livestream.add_argument(
  215. '--limit',
  216. dest='limit',
  217. type=int,
  218. default=None,
  219. help='''
  220. Number of items to fetch per request.
  221. ''',
  222. )
  223. p_livestream.add_argument(
  224. '-r',
  225. '--subreddit',
  226. dest='subreddit',
  227. default=None,
  228. help='''
  229. The subreddit to collect from.
  230. ''',
  231. )
  232. p_livestream.add_argument(
  233. '-s',
  234. '--submissions',
  235. dest='submissions',
  236. action='store_true',
  237. help='''
  238. If provided, do collect submissions. Otherwise don't.
  239. If submissions and comments are BOTH left unspecified, then they will
  240. BOTH be collected.
  241. ''',
  242. )
  243. p_livestream.add_argument(
  244. '-u',
  245. '--user',
  246. dest='username',
  247. default=None,
  248. help='''
  249. The redditor to collect from.
  250. ''',
  251. )
  252. p_livestream.add_argument(
  253. '-w',
  254. '--wait',
  255. dest='sleepy',
  256. default=30,
  257. help='''
  258. The number of seconds to wait between cycles.
  259. ''',
  260. )
  261. p_livestream.set_defaults(func=livestream_gateway)
  262. # MERGEDB'
  263. p_merge_db = subparsers.add_parser(
  264. 'merge_db',
  265. aliases=['merge-db', 'mergedb'],
  266. description='''
  267. Copy all new posts from one timesearch database into another.
  268. ''',
  269. )
  270. p_merge_db.examples = [
  271. '--from redditdev1.db --to redditdev2.db',
  272. ]
  273. p_merge_db.add_argument(
  274. '--from',
  275. dest='from_db_path',
  276. required=True,
  277. help='''
  278. The database file containing the posts you wish to copy.
  279. ''',
  280. )
  281. p_merge_db.add_argument(
  282. '--to',
  283. dest='to_db_path',
  284. required=True,
  285. help='''
  286. The database file to which you will copy the posts.
  287. The database is modified in-place.
  288. Existing posts will be ignored and not updated.
  289. ''',
  290. )
  291. p_merge_db.set_defaults(func=merge_db_gateway)
  292. # OFFLINE_READING
  293. p_offline_reading = subparsers.add_parser(
  294. 'offline_reading',
  295. aliases=['offline-reading'],
  296. description='''
  297. Render submissions and comment threads to HTML via Markdown.
  298. ''',
  299. )
  300. p_offline_reading.add_argument(
  301. '-r',
  302. '--subreddit',
  303. dest='subreddit',
  304. default=None,
  305. )
  306. p_offline_reading.add_argument(
  307. '-s',
  308. '--specific',
  309. dest='specific_submission',
  310. default=None,
  311. type=str,
  312. help='''
  313. Given a submission ID like t3_xxxxxx, render only that submission.
  314. Otherwise render every submission in the database.
  315. ''',
  316. )
  317. p_offline_reading.add_argument(
  318. '-u',
  319. '--user',
  320. dest='username',
  321. default=None,
  322. )
  323. p_offline_reading.set_defaults(func=offline_reading_gateway)
  324. # INDEX
  325. p_index = subparsers.add_parser(
  326. 'index',
  327. aliases=['redmash'],
  328. description='''
  329. Dump submission listings to a plaintext or HTML file.
  330. ''',
  331. )
  332. p_index.examples = [
  333. {
  334. 'args': '-r botwatch --date',
  335. 'comment': 'Does only the date file.'
  336. },
  337. {
  338. 'args': '-r botwatch --score --title',
  339. 'comment': 'Does both the score and title files.'
  340. },
  341. {
  342. 'args': '-r botwatch --score --score_threshold 50',
  343. 'comment': 'Only shows submissions with >= 50 points.'
  344. },
  345. {
  346. 'args': '-r botwatch --all',
  347. 'comment': 'Performs all of the different mashes.'
  348. },
  349. ]
  350. p_index.add_argument(
  351. '-r',
  352. '--subreddit',
  353. dest='subreddit',
  354. default=None,
  355. help='''
  356. The subreddit database to dump.
  357. ''',
  358. )
  359. p_index.add_argument(
  360. '-u',
  361. '--user',
  362. dest='username',
  363. default=None,
  364. help='''
  365. The username database to dump.
  366. ''',
  367. )
  368. p_index.add_argument(
  369. '--all',
  370. dest='do_all',
  371. action='store_true',
  372. help='''
  373. Perform all of the indexes listed below.
  374. ''',
  375. )
  376. p_index.add_argument(
  377. '--author',
  378. dest='do_author',
  379. action='store_true',
  380. help='''
  381. For subreddit databases only.
  382. Perform an index sorted by author.
  383. ''',
  384. )
  385. p_index.add_argument(
  386. '--date',
  387. dest='do_date',
  388. action='store_true',
  389. help='''
  390. Perform an index sorted by date.
  391. ''',
  392. )
  393. p_index.add_argument(
  394. '--flair',
  395. dest='do_flair',
  396. action='store_true',
  397. help='''
  398. Perform an index sorted by flair.
  399. ''',
  400. )
  401. p_index.add_argument(
  402. '--html',
  403. dest='html',
  404. action='store_true',
  405. help='''
  406. Write HTML files instead of plain text.
  407. ''',
  408. )
  409. p_index.add_argument(
  410. '--score',
  411. dest='do_score',
  412. action='store_true',
  413. help='''
  414. Perform an index sorted by score.
  415. ''',
  416. )
  417. p_index.add_argument(
  418. '--sub',
  419. dest='do_subreddit',
  420. action='store_true',
  421. help='''
  422. For username databases only.
  423. Perform an index sorted by subreddit.
  424. ''',
  425. )
  426. p_index.add_argument(
  427. '--title',
  428. dest='do_title',
  429. action='store_true',
  430. help='''
  431. Perform an index sorted by title.
  432. ''',
  433. )
  434. p_index.add_argument(
  435. '--offline',
  436. dest='offline',
  437. action='store_true',
  438. help='''
  439. The links in the index will point to the files generated by
  440. offline_reading. That is, `../offline_reading/fullname.html` instead
  441. of `http://redd.it/id`. This will NOT trigger offline_reading to
  442. generate the files now, so you must run that tool separately.
  443. ''',
  444. )
  445. p_index.add_argument(
  446. '--score_threshold',
  447. '--score-threshold',
  448. dest='score_threshold',
  449. type=int,
  450. default=0,
  451. help='''
  452. Only index posts with at least this many points.
  453. Applies to ALL indexes!
  454. ''',
  455. )
  456. p_index.set_defaults(func=index_gateway)
  457. # GET_SUBMISSIONS
  458. p_get_submissions = subparsers.add_parser(
  459. 'get_submissions',
  460. aliases=['get-submissions', 'timesearch'],
  461. description='''
  462. Collect submissions from the subreddit across all of history, or
  463. Collect submissions by a user (as many as possible).
  464. ''',
  465. )
  466. p_get_submissions.add_argument(
  467. '--lower',
  468. dest='lower',
  469. default='update',
  470. help='''
  471. If a number - the unix timestamp to start at.
  472. If "update" - continue from latest submission in db.
  473. ''',
  474. )
  475. p_get_submissions.add_argument(
  476. '-r',
  477. '--subreddit',
  478. dest='subreddit',
  479. type=str,
  480. default=None,
  481. help='''
  482. The subreddit to scan. Mutually exclusive with username.
  483. ''',
  484. )
  485. p_get_submissions.add_argument(
  486. '-u',
  487. '--user',
  488. dest='username',
  489. type=str,
  490. default=None,
  491. help='''
  492. The user to scan. Mutually exclusive with subreddit.
  493. ''',
  494. )
  495. p_get_submissions.add_argument(
  496. '--upper',
  497. dest='upper',
  498. default=None,
  499. help='''
  500. If a number - the unix timestamp to stop at.
  501. If not provided - stop at current time.
  502. ''',
  503. )
  504. p_get_submissions.add_argument(
  505. '--dont_supplement',
  506. '--dont-supplement',
  507. dest='do_supplement',
  508. action='store_false',
  509. help='''
  510. If provided, trust the pushshift data and do not fetch live copies
  511. from reddit.
  512. ''',
  513. )
  514. p_get_submissions.set_defaults(func=get_submissions_gateway)
  515. try:
  516. return betterhelp.go(parser, argv)
  517. except exceptions.DatabaseNotFound as exc:
  518. message = str(exc)
  519. message += '\nHave you used any of the other utilities to collect data?'
  520. print(message)
  521. return 1
  522. if __name__ == '__main__':
  523. raise SystemExit(main(sys.argv[1:]))