Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- # coding: utf8
- import json
- import os
- import operator
- import glob
- import twitter
- import time
- import datetime
- import cv2
- from PIL import Image
- import numpy as np
- import selenium
- maxtweetlength = 140
- from selenium import webdriver
- from selenium.webdriver.common.keys import Keys
- from sys import platform
- if platform == "win32":
- driver = webdriver.Chrome('PATH_TO_CHROMEDRIVER')
- elif platform == "linux" or platform == "linux2":
- import pyvirtualdisplay
- display = pyvirtualdisplay.Display(visible=0, size=(1920,1080))
- display.start()
- driver = webdriver.Chrome('PATH_TO_CHROMEDRIVER')
- driver.get("http://derstandard.at")
- time.sleep(2)
- driver.find_elements_by_xpath('//*/body')[0].send_keys(Keys.F11)
- time.sleep(2)
- privacyelements = driver.find_elements_by_xpath('//*[@id="privacypolicy"]')
- try:
- if len(privacyelements) > 0:
- button = privacyelements[0].find_element_by_xpath('./button')
- print(button.value_of_css_property('background-color'))
- button.click()
- except selenium.common.exceptions.ElementNotVisibleException as e:
- print("No privacy element")
- #connect to twitter
- TOKEN=
- TOKEN_KEY=
- CON_SEC=
- CON_SEC_KEY=
- my_auth = twitter.OAuth(TOKEN,TOKEN_KEY,CON_SEC,CON_SEC_KEY)
- t_upload = twitter.Twitter(domain='upload.twitter.com', auth=my_auth)
- t = twitter.Twitter(auth=my_auth)
- ts = time.time()
- date =datetime.datetime.fromtimestamp(ts).strftime('%Y%m%d')
- def main():
- #change to directory
- os.chdir(os.path.dirname(os.path.realpath(__file__)))
- filelist = []
- try:
- os.chdir("data")
- except OSError as e:
- print("no data directory found, aborting")
- for file in glob.glob("*.json"):
- try:
- filelist.append(int(file[:-5]))
- except ValueError:
- continue
- try:
- print(str(max(filelist)))
- except ValueError:
- print("No logs found. Seems like running for the first time.")
- latestfile = str(max(filelist))+".json"
- with open(latestfile, 'r') as file:
- comments = json.load(file)
- #just make sure that all comments have a like as int
- for item in comments:
- if item["likes"] == "":
- item["likes"] = 0
- for item in comments:
- if item["likes"] == "-":
- item["likes"] = 0
- for item in comments:
- item["likes"] = int(item["likes"])
- #remove duplicates
- comments_reduced = []
- seen = set()
- for d in comments:
- t = tuple(d.items())
- if t not in seen:
- seen.add(t)
- comments_reduced.append(d)
- #sort comments by number of likes
- comments_sorted = sorted(comments_reduced, key=lambda x: int(operator.itemgetter("likes")(x)))
- os.chdir(os.path.dirname(os.path.realpath(__file__)))
- try:
- os.mkdir("images")
- except OSError as e:
- print("images already existing")
- os.chdir("images")
- #First the Flops
- i = 3
- while i > 0:
- mystring= "Flop"+str(i)
- screenandpost(comments_sorted[i-1], mystring)
- i -= 1
- time.sleep(5)
- #Now the Tops
- #zB 100 Einträge. Wir starten bei index 97
- i = 3
- while i > 0:
- mystring= "Top"+str(i)
- screenandpost(comments_sorted[-i], mystring)
- i -= 1
- time.sleep(5)
- driver.close()
- if platform == "linux" or platform =="linux2":
- display.stop()
- def screenandpost(comment, str):
- """
- Input: Gets an article element and somthing like #Flop1
- Output: Makes a screenshot, crops it
- """
- url = "https://derstandard.at/permalink/p/"+comment["url"]
- driver.get(url)
- time.sleep(9)
- tempfile = "tmp"+date+"-"+str+".png"
- driver.get_screenshot_as_file(tempfile)
- image=cv2.imread(tempfile)
- lower = np.array([50, 190, 195])
- upper = np.array([85, 200, 255])
- shapeMask = cv2.inRange(image, lower, upper)
- cnts = cv2.findContours(shapeMask.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)[1]
- (x, y, w, h) = (278, 163, 278+651, 195+241)
- for c in cnts:
- peri = cv2.arcLength(c, True)
- approx = cv2.approxPolyDP(c, 0.04 * peri, True)
- if len(approx) == 4:
- # there are many results, but just one is big (which is the window we want)
- #will need a calculation of the area and if it significant, then the picture should be processed
- if cv2.contourArea(c) > 20000:
- (x, y, w, h) = cv2.boundingRect(approx)
- w = x + w
- h = y + h
- x = 220
- y = y - 35
- screenshot = Image.open(tempfile)
- post_crop = screenshot.crop((x, y, w, h))
- post_crop.save(date+"-"+str+".png")
- with open(date+"-"+str+".png", "rb") as imagefile:
- imagedata = imagefile.read()
- #id_img1 = t_upload.media.upload(media=imagedata)["media_id_string"]
- #print(id_img1)
- msg = "#"+str+" #standardforum #oesterreich #politik #medien "+url
- params = {"media[]": imagedata, "status": msg}
- t.statuses.update_with_media(**params)
- def prepareAndTweet(msg):
- """
- Input: A string
- Output: Tweets the string
- """
- #connect to twitter
- TOKEN=
- TOKEN_KEY=
- CON_SEC=
- CON_SEC_KEY=
- my_auth = twitter.OAuth(TOKEN,TOKEN_KEY,CON_SEC,CON_SEC_KEY)
- twit = twitter.Twitter(auth=my_auth)
- twit.statuses.update(status=msg)
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement