Advertisement
Guest User

Untitled

a guest
Oct 6th, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.56 KB | None | 0 0
  1. from bs4 import BeautifulSoup
  2. import praw
  3. import datetime
  4. import sqlite3
  5. import time
  6. #run on python 2 or 3
  7. try:
  8. import urllib.request as urllib2
  9. except ImportError:
  10. import urllib2
  11. import re
  12.  
  13. colsql = sqlite3.connect('collegalnotices233.db')
  14. colsql.text_factory = str
  15. colcur = colsql.cursor()
  16. colcur.execute('CREATE TABLE IF NOT EXISTS notices(date TEXT, url TEXT, content TEXT)')
  17. colsql.commit()
  18.  
  19.  
  20.  
  21.  
  22. r = urllib2.Request("http://classifieds.columbian.com/legals/")
  23. r.add_header('User-Agent', 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:42.0) Gecko/20100101 Firefox/42.0')
  24. colulegal = BeautifulSoup(urllib2.build_opener().open(r).read())
  25.  
  26. x = colulegal.find_all("a", class_="view_post")
  27.  
  28. colpostcontent = 'Recent Clark County Legal Notices:\n\n\n\n\n'
  29.  
  30. for each in x:
  31. noticesdb = colcur.execute("SELECT url FROM notices").fetchall()
  32. date = time.strftime("%Y %m %d %H:%M:%S %A")
  33. print each['href']
  34. newr = urllib2.Request('http://classifieds.columbian.com/' + str(each['href']))
  35. newr.add_header('User-Agent', 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:42.0) Gecko/20100101 Firefox/42.0')
  36. newsoup = BeautifulSoup(urllib2.build_opener().open(newr).read())
  37. '''for each in newsoup.find_all(class_='panel panel-primary'):
  38. print each'''
  39. if str(each['href']) in str(noticesdb):
  40. print str() + "\n \n in database"
  41. else:
  42. colcur.execute('INSERT INTO notices VALUES(?, ?, ?)', [date, each['href'], str(newsoup.find_all(class_="ad-content-container")[0].get_text(' ',strip=True).encode('utf8'))])
  43. colsql.commit()
  44. print str() + "\n \n added to database"
  45. colpostcontent += '\n \n' + newsoup.find_all(class_="ad-content-container")[0].get_text(' ',strip=True).encode('utf8') + '\n\n\n\n\n'
  46.  
  47. print colpostcontent
  48.  
  49.  
  50. PTITLE = "Clark County Legal Notices for " + time.strftime("%A, %B %d %Y")
  51. SUBREDDIT = "legalnoticesPDX"
  52. WAITS = 40000
  53. PTIME = "05:00"
  54. #reddit part
  55.  
  56. r = praw.Reddit(client_id=APIKEY,
  57. client_secret=APIKEYGOESHERE,
  58. redirect_uri='http://localhost:8080',
  59. user_agent='/r/legalnoticesPDX poster', username=USERNAME, password=PASSWORD)
  60. ptime = PTIME.split(':')
  61. ptime = (60*int(ptime[0])) + int(ptime[1])
  62.  
  63. sql = sqlite3.connect('collegalpostsfinal1.db')
  64. sql.text_factory = str
  65. cur = sql.cursor()
  66. cur.execute('CREATE TABLE IF NOT EXISTS posts(ID TEXT, STAMP TEXT, CREATED TEXT, POST TEXT)')
  67. sql.commit()
  68.  
  69. def dailypost():
  70. now = datetime.datetime.now()
  71. daystamp = datetime.datetime.strftime(now, "%d%b%Y")
  72. cur.execute('SELECT * FROM posts WHERE STAMP=?', [daystamp])
  73. nowtime = (60*now.hour) + now.minute
  74. print('Now: ' + str(nowtime) + ' ' + datetime.datetime.strftime(now, "%H:%M"))
  75. print('Pst: ' + str(ptime) + ' ' + PTIME)
  76. if not cur.fetchone():
  77. diff = nowtime-ptime
  78. if diff > 0:
  79. print('t+ ' + str(abs(diff)) + ' minutes')
  80. makepost(now, daystamp)
  81. else:
  82. print('t- ' + str(diff) + ' minutes')
  83. makepost(now, daystamp)
  84. else:
  85. print("Already made today's post")
  86.  
  87.  
  88.  
  89.  
  90. def makepost(now, daystamp):
  91. print('Making post...')
  92. newpost = r.subreddit(SUBREDDIT).submit(title=PTITLE, selftext=colpostcontent, send_replies=True)
  93. print('Success')
  94. cur.execute('INSERT INTO posts VALUES(?, ?, ?, ?)', [daystamp, newpost.id, newpost.created_utc, str(colpostcontent)])
  95. sql.commit()
  96.  
  97.  
  98. while True:
  99. try:
  100. dailypost()
  101. except Exception as e:
  102. print("ERROR:", e)
  103. #print('Sleeping ' + str(WAITS) + ' seconds.\n')
  104. time.sleep(5)
  105. print ('quitting...')
  106. quit()
  107. #time.sleep(WAITS)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement