Python reads information in newsgroups

From , 3 Years ago, written in Python, viewed 223 times.
URL https://pastebin.vip/view/36fa3ecc
  1. # Hello, this script is written in Python - http://www.python.org
  2. #
  3. # newsarchiver 1.1p - Newsgroup archiver
  4. #
  5. # Purpose:
  6. #    This script will download all available message from the desired Usenet group
  7. #    and save them as plain text files. Usefull for bulk group archiving.
  8. #
  9. # Usage:
  10. #
  11. #    Syntaxe   : python newsarchiver.py <groupname> [firstArticle]
  12. #
  13. #      where   groupname is the group name (eg. comp.lang.python)
  14. #              firstArticle is the article number to fetch from (optional)
  15. #
  16. #    Example 1 : python newsarchiver.py comp.lang.python
  17. #                (fetch all article available from comp.lang.python)
  18. #
  19. #    Example 2 : python newsarchiver.py comp.lang.python 108224
  20. #                (fetch all article available from comp.lang.python
  21. #                 starting with article number 108224)
  22. #
  23. #    Password will be asked when the script is run.
  24. #
  25. #    Server name, login and destination directory are hardcoded.
  26. #    Tweak the lines below the 'import' statement to suit your needs.
  27. #    Variable names should be self-explanatory.
  28. #
  29. #    Then run this script with and enter your password for the corresponding login.
  30. #    This script will then connect to news server and start fetching messages.
  31. #
  32. #    You can stop this script at anytime (break with CTRL+C)
  33. #    and re-run it later to continue to fetch messages.
  34. #    This script will not fetch message it has already fetched.
  35. #
  36. #    All messages will be saved as individual files in the form:
  37. #        groupname_messageNumber
  38. #    (with dots replaced by underscores)
  39. #    ( eg : comp_lang_python_104208 )
  40. #
  41. #    Keep in mind that 'messageNumber' is server-dependant.
  42. #    (If you change newsserver, the messageNumber will be different : you will
  43. #    have to erase all files and fetch them all to have a coherent fileset)
  44. #    The messageNumber matches the Xref reference number in each message.
  45. #
  46. #    Group must exist on server.
  47. #
  48. # Changes:
  49. #    1.0p : - first version
  50. #    1.1p : - added group name and first article number as command-line parameters.
  51. #           - added help screen
  52. #
  53. # Author's comments:
  54. #    Oh my, I wouldn't beleive this would be so easy to program... thanks to Python !
  55. #
  56. # Credits:
  57. #    I created this script for 2 purposes:
  58. #        - train Python programming (this is one of my first Python scripts)
  59. #        - archive comp.lang.python and other interesting newsgroups.
  60. #
  61. #    This author of this script is Sebastien SAUVAGE <sebsauvage at sebsauvage dot net>
  62. #                                  http://sebsauvage.net
  63. #    Other quick & dirty Python stuff is likely to be available at http://sebsauvage.net/python/
  64. #
  65. # Legal:
  66. #   This script is public domain. Feel free to re-use and tweak the code.
  67. #
  68. import os.path,nntplib,string,getpass,sys
  69.  
  70. destination = 'c:\\ngarchive\\'   # do not forget the trailing [back]slash !
  71. newsserver  = '127.0.0.1'
  72. loginname   = 'sebsauvage'
  73.  
  74. if len( sys.argv ) < 2:
  75.     print '>>> newsArchiver 1.1p\n'
  76.     print '    *** IMPORTANT ***'
  77.     print '    See comments in code for more information before running this script !'
  78.     print '    (News server address and login name are hardcoded :'
  79.     print '     you need to tailor them before using this script.)'
  80.     print '    News server',newsserver,"will be used with login '"+loginname+"'"
  81.     print '    Destination path is',destination,'\n'
  82.     print '    Syntax    : python newsarchiver.py <groupname> [firstArticle]\n'
  83.     print '    Example 1 : python newsarchiver.py comp.lang.python'
  84.     print '                (fetch all article available from comp.lang.python)\n'
  85.     print '    Example 2 : python newsarchiver.py comp.lang.python 108224'
  86.     print '                (fetch all article available from comp.lang.python'
  87.     print '                 starting with article number 108224)\n'
  88.     sys.exit()
  89.  
  90. groupName   = sys.argv[1]
  91. firstArticle = 0
  92. if len( sys.argv ) > 2:
  93.     try:
  94.         firstArticle = int(sys.argv[2])
  95.     except:
  96.         print 'Error : firstArticle parameters must be numeric.'
  97.         sys.exit()
  98.  
  99. loginpassword = getpass.getpass('>>> Please enter password for login '+loginname+'@'+newsserver+' : ')
  100.  
  101. print '>>> Connecting to news server',newsserver,'...'
  102. try:
  103.     ns = nntplib.NNTP(newsserver,119,loginname,loginpassword)
  104. except:
  105.     print '>>> Could not connect to news server.'
  106. else:
  107.     print '>>> News server welcomes us:'
  108.     print ns.getwelcome()
  109.     print '>>> Accessing group', groupName
  110.     try:
  111.         group = ns.group(groupName)
  112.     except:
  113.         print '>>> Could not open group',groupName
  114.     else:
  115.         count = group[1]  # nb of articles available on server
  116.         first = group[2]  # ID of first available article
  117.         last =  group[3]  # ID of last available article
  118.         print '>>>    Article count :',count
  119.         print '>>>    First         :',first
  120.         print '>>>    Last          :',last
  121.         if (firstArticle > int(first)) and (firstArticle <= int(last)):
  122.             first = str(firstArticle)
  123.             print '>>> Fetching from article',first
  124.         for articleNumber in range(int(first),int(last)+1):
  125.             fileName = destination+string.replace(groupName+'.'+str(articleNumber),'.','_')
  126.             if not os.path.isfile( fileName ):
  127.                 print '>>> Fetching article',articleNumber,'out of',last,'from',groupName
  128.                 try:
  129.                     article = ns.article(str(articleNumber))
  130.                 except:
  131.                     print '>>> Could not fetch article',articleNumber
  132.                 else:
  133.                     f=open(fileName, 'w+')
  134.                     for line in article[3]:
  135.                         f.write(line+'\n')
  136.                     f.close()
  137.     print '>>> Closing connection with news server...'
  138.     ns.quit()
  139.  
  140. #//python/5173

Reply to "Python reads information in newsgroups"

Here you can reply to the paste above

captcha

https://burned.cc - Burn After Reading Website