Advertisement
Guest User

Untitled

a guest
Aug 23rd, 2017
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.01 KB | None | 0 0
  1. #!/usr/bin/python
  2. # coding: utf8
  3.  
  4.  
  5. import json
  6. import os
  7. import operator
  8. import glob
  9. import twitter
  10. import time
  11. import datetime
  12. import cv2
  13. from PIL import Image
  14. import numpy as np
  15. import selenium
  16.  
  17. maxtweetlength = 140
  18.  
  19. from selenium import webdriver
  20. from selenium.webdriver.common.keys import Keys
  21. from sys import platform
  22.  
  23. if platform == "win32":
  24.     driver = webdriver.Chrome('PATH_TO_CHROMEDRIVER')
  25. elif platform == "linux" or platform == "linux2":
  26.     import pyvirtualdisplay
  27.     display = pyvirtualdisplay.Display(visible=0, size=(1920,1080))
  28.     display.start()
  29.     driver = webdriver.Chrome('PATH_TO_CHROMEDRIVER')
  30.    
  31. driver.get("http://derstandard.at")
  32. time.sleep(2)
  33. driver.find_elements_by_xpath('//*/body')[0].send_keys(Keys.F11)
  34. time.sleep(2)
  35. privacyelements = driver.find_elements_by_xpath('//*[@id="privacypolicy"]')
  36.  
  37. try:
  38.     if len(privacyelements) > 0:
  39.         button = privacyelements[0].find_element_by_xpath('./button')
  40.         print(button.value_of_css_property('background-color'))
  41.         button.click()     
  42. except selenium.common.exceptions.ElementNotVisibleException as e:
  43.     print("No privacy element")
  44.    
  45.    
  46. #connect to twitter
  47. TOKEN=
  48. TOKEN_KEY=
  49. CON_SEC=
  50. CON_SEC_KEY=
  51.  
  52. my_auth = twitter.OAuth(TOKEN,TOKEN_KEY,CON_SEC,CON_SEC_KEY)
  53.  
  54. t_upload = twitter.Twitter(domain='upload.twitter.com', auth=my_auth)
  55.  
  56. t = twitter.Twitter(auth=my_auth)
  57.  
  58. ts = time.time()
  59. date =datetime.datetime.fromtimestamp(ts).strftime('%Y%m%d')
  60.  
  61. def main():
  62.     #change to directory
  63.     os.chdir(os.path.dirname(os.path.realpath(__file__)))
  64.    
  65.     filelist = []
  66.     try:
  67.         os.chdir("data")
  68.  
  69.     except OSError as e:
  70.         print("no data directory found, aborting")
  71.        
  72.     for file in glob.glob("*.json"):
  73.         try:
  74.             filelist.append(int(file[:-5]))
  75.         except ValueError:
  76.             continue
  77.    
  78.     try:
  79.         print(str(max(filelist)))
  80.     except ValueError:
  81.         print("No logs found. Seems like running for the first time.")
  82.  
  83.     latestfile = str(max(filelist))+".json"
  84.    
  85.     with open(latestfile, 'r') as file:
  86.         comments = json.load(file)
  87.        
  88.     #just make sure that all comments have a like as int
  89.     for item in comments:
  90.         if item["likes"] == "":
  91.             item["likes"] = 0
  92.    
  93.     for item in comments:
  94.         if item["likes"] == "-":
  95.             item["likes"] = 0
  96.        
  97.     for item in comments:
  98.         item["likes"] = int(item["likes"])
  99.        
  100.     #remove duplicates
  101.    
  102.     comments_reduced = []
  103.     seen = set()
  104.    
  105.     for d in comments:
  106.         t = tuple(d.items())
  107.         if t not in seen:
  108.             seen.add(t)
  109.             comments_reduced.append(d)
  110.            
  111.    
  112.     #sort comments by number of likes
  113.     comments_sorted = sorted(comments_reduced, key=lambda x: int(operator.itemgetter("likes")(x)))
  114.    
  115.     os.chdir(os.path.dirname(os.path.realpath(__file__)))
  116.    
  117.     try:
  118.         os.mkdir("images")
  119.     except OSError as e:
  120.         print("images already existing")
  121.        
  122.     os.chdir("images")
  123.    
  124.     #First the Flops   
  125.     i = 3
  126.    
  127.     while i > 0:
  128.         mystring= "Flop"+str(i)
  129.         screenandpost(comments_sorted[i-1], mystring)
  130.         i -= 1
  131.         time.sleep(5)
  132.        
  133.     #Now the Tops
  134.    
  135.     #zB 100 Einträge. Wir starten bei index 97
  136.     i = 3
  137.    
  138.     while i > 0:
  139.         mystring= "Top"+str(i)
  140.         screenandpost(comments_sorted[-i], mystring)
  141.         i -= 1
  142.         time.sleep(5)
  143.    
  144.    
  145.     driver.close()
  146.     if platform == "linux" or platform =="linux2":
  147.         display.stop()
  148.  
  149. def screenandpost(comment, str):
  150.     """
  151.     Input: Gets an article element and somthing like #Flop1
  152.     Output: Makes a screenshot, crops it
  153.     """
  154.    
  155.     url = "https://derstandard.at/permalink/p/"+comment["url"]
  156.    
  157.     driver.get(url)
  158.    
  159.     time.sleep(9)
  160.  
  161.     tempfile = "tmp"+date+"-"+str+".png"
  162.    
  163.     driver.get_screenshot_as_file(tempfile)
  164.    
  165.     image=cv2.imread(tempfile)
  166.    
  167.     lower = np.array([50, 190, 195])
  168.     upper = np.array([85, 200, 255])
  169.     shapeMask = cv2.inRange(image, lower, upper)
  170.    
  171.     cnts = cv2.findContours(shapeMask.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)[1]
  172.    
  173.     (x, y, w, h) = (278, 163, 278+651, 195+241)
  174.    
  175.     for c in cnts:
  176.         peri = cv2.arcLength(c, True)
  177.         approx = cv2.approxPolyDP(c, 0.04 * peri, True)
  178.         if len(approx) == 4:
  179.             # there are many results, but just one is big (which is the window we want)
  180.             #will need a calculation of the area and if it significant, then the picture should be processed
  181.             if cv2.contourArea(c) > 20000
  182.                 (x, y, w, h) = cv2.boundingRect(approx)
  183.                 w = x + w
  184.                 h = y + h
  185.                 x = 220
  186.                 y = y - 35
  187.    
  188.     screenshot = Image.open(tempfile)
  189.    
  190.     post_crop = screenshot.crop((x, y, w, h))
  191.    
  192.     post_crop.save(date+"-"+str+".png")
  193.    
  194.     with open(date+"-"+str+".png", "rb") as imagefile:
  195.         imagedata = imagefile.read()
  196.    
  197.     #id_img1 = t_upload.media.upload(media=imagedata)["media_id_string"]
  198.     #print(id_img1)
  199.    
  200.     msg = "#"+str+" #standardforum #oesterreich #politik #medien "+url
  201.    
  202.     params = {"media[]": imagedata, "status": msg}
  203.    
  204.     t.statuses.update_with_media(**params)
  205.    
  206. def prepareAndTweet(msg):
  207.     """
  208.     Input: A string
  209.     Output: Tweets the string
  210.     """
  211.     #connect to twitter
  212.     TOKEN=
  213.     TOKEN_KEY=
  214.     CON_SEC=
  215.     CON_SEC_KEY=
  216.  
  217.     my_auth = twitter.OAuth(TOKEN,TOKEN_KEY,CON_SEC,CON_SEC_KEY)
  218.     twit = twitter.Twitter(auth=my_auth)
  219.    
  220.     twit.statuses.update(status=msg)
  221.    
  222.  
  223. if __name__ == "__main__":
  224.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement