mirror of
https://github.com/timberjoegithub/GoogleScrape.git
synced 2026-07-22 00:19:48 +00:00
clean up some linting and make more resiliant to xls file missing
This commit is contained in:
@@ -42,7 +42,7 @@ from sqlalchemy.ext.declarative import declarative_base
|
|||||||
#import mysql-connector-python
|
#import mysql-connector-python
|
||||||
Base = declarative_base()
|
Base = declarative_base()
|
||||||
|
|
||||||
#########################################################################################################################################################
|
##################################################################################################
|
||||||
|
|
||||||
def preload():
|
def preload():
|
||||||
file=pathlib.Path("./config/joeteststeele_uuid_and_cookie.json")
|
file=pathlib.Path("./config/joeteststeele_uuid_and_cookie.json")
|
||||||
@@ -50,10 +50,9 @@ def preload():
|
|||||||
pathlib.Path.unlink(file)
|
pathlib.Path.unlink(file)
|
||||||
global today
|
global today
|
||||||
today = datetime.today().strftime('%Y-%m-%d')
|
today = datetime.today().strftime('%Y-%m-%d')
|
||||||
|
|
||||||
return
|
return
|
||||||
|
|
||||||
#########################################################################################################################################################
|
##################################################################################################
|
||||||
|
|
||||||
class Users(Base):
|
class Users(Base):
|
||||||
__tablename__ = 'userConfig'
|
__tablename__ = 'userConfig'
|
||||||
@@ -80,7 +79,7 @@ class Users(Base):
|
|||||||
instagrampass = sqlalchemy.Column(sqlalchemy.String(length=32, collation="utf8"))
|
instagrampass = sqlalchemy.Column(sqlalchemy.String(length=32, collation="utf8"))
|
||||||
#active = sqlalchemy.Column(sqlalchemy.Boolean, default=True)
|
#active = sqlalchemy.Column(sqlalchemy.Boolean, default=True)
|
||||||
|
|
||||||
#########################################################################################################################################################
|
##################################################################################################
|
||||||
|
|
||||||
class Posts(Base):
|
class Posts(Base):
|
||||||
__tablename__ = 'posts'
|
__tablename__ = 'posts'
|
||||||
@@ -96,7 +95,7 @@ class Posts(Base):
|
|||||||
dictPostComplete = sqlalchemy.Column(sqlalchemy.String(length=128, collation="utf8"))
|
dictPostComplete = sqlalchemy.Column(sqlalchemy.String(length=128, collation="utf8"))
|
||||||
#active = sqlalchemy.Column(sqlalchemy.Boolean, default=True)
|
#active = sqlalchemy.Column(sqlalchemy.Boolean, default=True)
|
||||||
|
|
||||||
#########################################################################################################################################################
|
##################################################################################################
|
||||||
|
|
||||||
def authconnect():
|
def authconnect():
|
||||||
connections = {}
|
connections = {}
|
||||||
@@ -120,9 +119,12 @@ def authconnect():
|
|||||||
if (os.path.exists(xls)):
|
if (os.path.exists(xls)):
|
||||||
wb = load_workbook(filename = xls)
|
wb = load_workbook(filename = xls)
|
||||||
xlswbDF = pd.read_excel(xls)
|
xlswbDF = pd.read_excel(xls)
|
||||||
if (os.path.exists('./GoogleScrape/'+ xls)):
|
else:
|
||||||
wb = load_workbook(filename = './GoogleScrape/'+ xls)
|
if (os.path.exists('./GoogleScrape/'+ xls)):
|
||||||
xlswbDF = pd.read_excel('./GoogleScrape/'+ xls)
|
wb = load_workbook(filename = './GoogleScrape/'+ xls)
|
||||||
|
xlswbDF = pd.read_excel('./GoogleScrape/'+ xls)
|
||||||
|
else:
|
||||||
|
input("Not able to find xls file Press any key to continue...")
|
||||||
ws = wb['Sheet1']
|
ws = wb['Sheet1']
|
||||||
#xlswbDF = pd.read_excel(xls)
|
#xlswbDF = pd.read_excel(xls)
|
||||||
connections.update({'xlsdf':xlswbDF})
|
connections.update({'xlsdf':xlswbDF})
|
||||||
@@ -165,7 +167,7 @@ def authconnect():
|
|||||||
print(' Connecting to Instagram ...')
|
print(' Connecting to Instagram ...')
|
||||||
return connections
|
return connections
|
||||||
|
|
||||||
#########################################################################################################################################################
|
##################################################################################################
|
||||||
|
|
||||||
# Grab a count of how far we need to scroll
|
# Grab a count of how far we need to scroll
|
||||||
def counter(driver):
|
def counter(driver):
|
||||||
@@ -175,7 +177,7 @@ def counter(driver):
|
|||||||
result = result[0].split('\n')
|
result = result[0].split('\n')
|
||||||
return int(int(result[0])/10)+1
|
return int(int(result[0])/10)+1
|
||||||
|
|
||||||
#########################################################################################################################################################
|
##################################################################################################
|
||||||
|
|
||||||
def post_facebook(title, content, date, rating, address, picslist, instasession):
|
def post_facebook(title, content, date, rating, address, picslist, instasession):
|
||||||
#msg = 'Purple Ombre Bob Lace Wig Natural Human Hair now available on https://lace-wigs.co.za/'
|
#msg = 'Purple Ombre Bob Lace Wig Natural Human Hair now available on https://lace-wigs.co.za/'
|
||||||
@@ -196,7 +198,8 @@ def post_facebook(title, content, date, rating, address, picslist, instasession)
|
|||||||
print(' Facebook response: ',r.text, img_payload)
|
print(' Facebook response: ',r.text, img_payload)
|
||||||
return (r)
|
return (r)
|
||||||
|
|
||||||
#########################################################################################################################################################
|
##################################################################################################
|
||||||
|
|
||||||
def postImage(group_id, img,auth_token):
|
def postImage(group_id, img,auth_token):
|
||||||
files={}
|
files={}
|
||||||
url = f"https://graph.facebook.com/{group_id}/photos?access_token=" + auth_token
|
url = f"https://graph.facebook.com/{group_id}/photos?access_token=" + auth_token
|
||||||
@@ -205,16 +208,16 @@ def postImage(group_id, img,auth_token):
|
|||||||
data = {
|
data = {
|
||||||
"published" : False
|
"published" : False
|
||||||
}
|
}
|
||||||
try: r = requests.post(url, files=files, data=data).json()
|
try:
|
||||||
#r = requests.post(url, files=files, data=data).json()
|
r = requests.post(url, files=files, data=data).json()
|
||||||
except Exception as error:
|
except Exception as error:
|
||||||
print(" An error getting date occurred:", type(error).c) # An error occurred:
|
print(" An error getting date occurred:", type(error).c) # An error occurred:
|
||||||
r = False
|
r = False
|
||||||
# print (r)
|
|
||||||
# print ('r id = ',r['id'])
|
|
||||||
time.sleep(facebooksleep)
|
time.sleep(facebooksleep)
|
||||||
return r
|
return r
|
||||||
|
|
||||||
|
##################################################################################################
|
||||||
|
|
||||||
def postVideo(group_id, video_path,auth_token,title, content, date, rating, address):
|
def postVideo(group_id, video_path,auth_token,title, content, date, rating, address):
|
||||||
url = f"https://graph-video.facebook.com/{group_id}/videos?access_token=" + auth_token
|
url = f"https://graph-video.facebook.com/{group_id}/videos?access_token=" + auth_token
|
||||||
files={}
|
files={}
|
||||||
@@ -237,7 +240,7 @@ def postVideo(group_id, video_path,auth_token,title, content, date, rating, addr
|
|||||||
print ('r id = ',r['id'])
|
print ('r id = ',r['id'])
|
||||||
return r
|
return r
|
||||||
|
|
||||||
|
##################################################################################################
|
||||||
|
|
||||||
def post_facebook2(title, content, date, rating, address, picslist, instasession):
|
def post_facebook2(title, content, date, rating, address, picslist, instasession):
|
||||||
#msg = 'Purple Ombre Bob Lace Wig Natural Human Hair now available on https://lace-wigs.co.za/'
|
#msg = 'Purple Ombre Bob Lace Wig Natural Human Hair now available on https://lace-wigs.co.za/'
|
||||||
@@ -291,6 +294,7 @@ def post_facebook2(title, content, date, rating, address, picslist, instasession
|
|||||||
print(' Facebook response: ',r)
|
print(' Facebook response: ',r)
|
||||||
return (r)
|
return (r)
|
||||||
|
|
||||||
|
##################################################################################################
|
||||||
|
|
||||||
def post_facebook3(title, content, date, rating, address, picslist, instasession):
|
def post_facebook3(title, content, date, rating, address, picslist, instasession):
|
||||||
pics = ((picslist[1:-1]).replace("'","")).split(",")
|
pics = ((picslist[1:-1]).replace("'","")).split(",")
|
||||||
@@ -314,42 +318,16 @@ def post_facebook3(title, content, date, rating, address, picslist, instasession
|
|||||||
imgs_id.append(post_id['id'])
|
imgs_id.append(post_id['id'])
|
||||||
except Exception as error:
|
except Exception as error:
|
||||||
print(" An error occurred:", type(error).c) # An error occurred:
|
print(" An error occurred:", type(error).c) # An error occurred:
|
||||||
# if (imgs_pic):
|
|
||||||
# try:
|
|
||||||
# post_id = postImage(group_id ,imgs_pic,auth_token)
|
|
||||||
# imgs_id.append(post_id['id'])
|
|
||||||
# except Exception as error:
|
|
||||||
# print(" An error occurred:", type(error)) # An error occurred:
|
|
||||||
# try:
|
|
||||||
# imgs_id.append(post_id['id'])
|
|
||||||
# except Exception as error:
|
|
||||||
# print(" An error occurred:", type(error).c) # An error occurred:
|
|
||||||
# args=dict()
|
|
||||||
# # args['title']= title
|
|
||||||
# args["message"]=title + " "+ content
|
|
||||||
# for img_id in imgs_id:
|
|
||||||
# key="attached_media["+str(imgs_id.index(img_id))+"]"
|
|
||||||
# args[key]="{'media_fbid': '"+img_id+"'}"
|
|
||||||
# #url = f"https://graph.facebook.com/me/feed?access_token=" + auth_token
|
|
||||||
# url = f"https://graph.facebook.com/{group_id}/feed?access_token=" + auth_token
|
|
||||||
# #print ("r = request.post(" +url+", data="+args+")")
|
|
||||||
# try: r = requests.post(url, data=args)
|
|
||||||
# #try: r = requests.post(url, data=args).json()
|
|
||||||
# except Exception as error:
|
|
||||||
# print(" An error getting date occurred:", type(error).c) # An error occurred:
|
|
||||||
# r = False
|
|
||||||
time.sleep(facebooksleep)
|
time.sleep(facebooksleep)
|
||||||
print(' Facebook response: ',post_id)
|
print(' Facebook response: ',post_id)
|
||||||
return (True)
|
return (True)
|
||||||
|
|
||||||
#########################################################################################################################################################
|
##################################################################################################
|
||||||
|
|
||||||
|
|
||||||
def get_data(driver,outputs ):
|
def get_data(driver,outputs ):
|
||||||
|
|
||||||
# curl -X GET -H 'Content-Type: application/json' -H "X-Goog-Api-Key: API_KEY" -H "X-Goog-FieldMask: id,displayName,formattedAddress,plusCode" https://places.googleapis.com/v1/places/ChIJj61dQgK6j4AR4GeTYWZsKWw #placeId #websiteUri AIzaSyB_hglzM7N8HzjXwi1dF1E8WYTql3akS7Q
|
# curl -X GET -H 'Content-Type: application/json' -H "X-Goog-Api-Key: API_KEY" -H "X-Goog-FieldMask: id,displayName,formattedAddress,plusCode" https://places.googleapis.com/v1/places/ChIJj61dQgK6j4AR4GeTYWZsKWw #placeId #websiteUri AIzaSyB_hglzM7N8HzjXwi1dF1E8WYTql3akS7Q
|
||||||
|
|
||||||
|
|
||||||
"""
|
"""
|
||||||
this function get main text, score, name
|
this function get main text, score, name
|
||||||
"""
|
"""
|
||||||
@@ -438,7 +416,7 @@ def get_data(driver,outputs ):
|
|||||||
|
|
||||||
return lst_data
|
return lst_data
|
||||||
|
|
||||||
#########################################################################################################################################################
|
##################################################################################################
|
||||||
|
|
||||||
# Do the scrolling
|
# Do the scrolling
|
||||||
def scrolling(counter,driver):
|
def scrolling(counter,driver):
|
||||||
@@ -458,7 +436,7 @@ def scrolling(counter,driver):
|
|||||||
print(f"Error while scrolling: {e}")
|
print(f"Error while scrolling: {e}")
|
||||||
break
|
break
|
||||||
|
|
||||||
#########################################################################################################################################################
|
##################################################################################################
|
||||||
|
|
||||||
def write_to_xlsx(webdata, outputs):
|
def write_to_xlsx(webdata, outputs):
|
||||||
print('Start to write to excel...')
|
print('Start to write to excel...')
|
||||||
@@ -530,7 +508,7 @@ def write_to_xlsx2(data, outputs):
|
|||||||
df.to_excel(xls)
|
df.to_excel(xls)
|
||||||
return data
|
return data
|
||||||
|
|
||||||
#########################################################################################################################################################
|
##################################################################################################
|
||||||
|
|
||||||
def database_read(data):
|
def database_read(data):
|
||||||
from sqlalchemy import create_engine
|
from sqlalchemy import create_engine
|
||||||
@@ -540,7 +518,7 @@ def database_read(data):
|
|||||||
df = pd.read_sql('SELECT * FROM table_name', con=db_connection)
|
df = pd.read_sql('SELECT * FROM table_name', con=db_connection)
|
||||||
return df
|
return df
|
||||||
|
|
||||||
#########################################################################################################################################################
|
##################################################################################################
|
||||||
|
|
||||||
|
|
||||||
def check_web_media(filename,headers):
|
def check_web_media(filename,headers):
|
||||||
@@ -555,7 +533,7 @@ def check_web_media(filename,headers):
|
|||||||
print(' No existing media with same name in Wordpress media folder: ' + filename)
|
print(' No existing media with same name in Wordpress media folder: ' + filename)
|
||||||
return False, False
|
return False, False
|
||||||
|
|
||||||
#########################################################################################################################################################
|
##################################################################################################
|
||||||
|
|
||||||
def check_web_post(postname,postdate,headers):
|
def check_web_post(postname,postdate,headers):
|
||||||
response = requests.get(wpAPI+"/posts?search="+postname, headers=headers)
|
response = requests.get(wpAPI+"/posts?search="+postname, headers=headers)
|
||||||
@@ -569,7 +547,7 @@ def check_web_post(postname,postdate,headers):
|
|||||||
print('No existing post with same name: ' + postname)
|
print('No existing post with same name: ' + postname)
|
||||||
return False
|
return False
|
||||||
|
|
||||||
#########################################################################################################################################################
|
##################################################################################################
|
||||||
|
|
||||||
def is_port_open(host, port):
|
def is_port_open(host, port):
|
||||||
try:
|
try:
|
||||||
@@ -580,7 +558,7 @@ def is_port_open(host, port):
|
|||||||
print ('Could not open port to website: ', host, type(error))
|
print ('Could not open port to website: ', host, type(error))
|
||||||
return False
|
return False
|
||||||
|
|
||||||
#########################################################################################################################################################
|
##################################################################################################
|
||||||
|
|
||||||
def check_media(filename, headers):
|
def check_media(filename, headers):
|
||||||
# Regex gilename to format like in WordPress media name
|
# Regex gilename to format like in WordPress media name
|
||||||
@@ -596,7 +574,7 @@ def check_media(filename, headers):
|
|||||||
print(' No existing media with same name in Wordpress media folder: ' + filename)
|
print(' No existing media with same name in Wordpress media folder: ' + filename)
|
||||||
return False, False
|
return False, False
|
||||||
|
|
||||||
#########################################################################################################################################################
|
##################################################################################################
|
||||||
|
|
||||||
def check_post(postname,postdate,headers2):
|
def check_post(postname,postdate,headers2):
|
||||||
response = requests.get(wpAPI+"/posts?search="+postname, headers=headers2)
|
response = requests.get(wpAPI+"/posts?search="+postname, headers=headers2)
|
||||||
@@ -613,7 +591,7 @@ def check_post(postname,postdate,headers2):
|
|||||||
print('No existing post with same name: ' + postname)
|
print('No existing post with same name: ' + postname)
|
||||||
return False
|
return False
|
||||||
|
|
||||||
#########################################################################################################################################################
|
##################################################################################################
|
||||||
|
|
||||||
def post_x(title, content, date, rating, address, picslist, instasession):
|
def post_x(title, content, date, rating, address, picslist, instasession):
|
||||||
pics = ((picslist[1:-1]).replace("'","")).split(",")
|
pics = ((picslist[1:-1]).replace("'","")).split(",")
|
||||||
@@ -664,7 +642,7 @@ def post_x(title, content, date, rating, address, picslist, instasession):
|
|||||||
print(json.dumps(json_response, indent=4, sort_keys=True))
|
print(json.dumps(json_response, indent=4, sort_keys=True))
|
||||||
return
|
return
|
||||||
|
|
||||||
#########################################################################################################################################################
|
##################################################################################################
|
||||||
|
|
||||||
|
|
||||||
def post_to_wp(title, content, headers,date, rating,address, picslist):
|
def post_to_wp(title, content, headers,date, rating,address, picslist):
|
||||||
@@ -863,7 +841,7 @@ def post_to_wp(title, content, headers,date, rating,address, picslist):
|
|||||||
print(" An error writing images to the post " + post_response.title + ' occurred:", type(error).__name__) # An error occurred')
|
print(" An error writing images to the post " + post_response.title + ' occurred:", type(error).__name__) # An error occurred')
|
||||||
return NewPost
|
return NewPost
|
||||||
|
|
||||||
#########################################################################################################################################################
|
##################################################################################################
|
||||||
|
|
||||||
def make_video(inphotos):
|
def make_video(inphotos):
|
||||||
# Load the photos from the folder
|
# Load the photos from the folder
|
||||||
@@ -895,10 +873,10 @@ def make_video(inphotos):
|
|||||||
return outputvideo, output
|
return outputvideo, output
|
||||||
return False, False
|
return False, False
|
||||||
|
|
||||||
#########################################################################################################################################################
|
##################################################################################################
|
||||||
|
|
||||||
|
|
||||||
#########################################################################################################################################################
|
##################################################################################################
|
||||||
|
|
||||||
def post_to_instagram2 (title, content, date, rating, address, picslist, instasession):
|
def post_to_instagram2 (title, content, date, rating, address, picslist, instasession):
|
||||||
#post_to_instagram2(processrow[1].value, processrow[2].value ,processrow[7].value, processrow[3].value, processrow[8].value, processrow[5].value,outputs['instagram'])
|
#post_to_instagram2(processrow[1].value, processrow[2].value ,processrow[7].value, processrow[3].value, processrow[8].value, processrow[5].value,outputs['instagram'])
|
||||||
@@ -947,14 +925,14 @@ def post_to_instagram2 (title, content, date, rating, address, picslist, instase
|
|||||||
else:
|
else:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
#########################################################################################################################################################
|
##################################################################################################
|
||||||
|
|
||||||
def clearlist (list):
|
def clearlist (list):
|
||||||
for listelement in list:
|
for listelement in list:
|
||||||
listelement.clear
|
listelement.clear
|
||||||
return list
|
return list
|
||||||
|
|
||||||
#########################################################################################################################################################
|
##################################################################################################
|
||||||
|
|
||||||
def hastags (address, name):
|
def hastags (address, name):
|
||||||
nameNoSpaces = re.sub( r'[^a-zA-Z]','',name)
|
nameNoSpaces = re.sub( r'[^a-zA-Z]','',name)
|
||||||
@@ -972,7 +950,7 @@ def hastags (address, name):
|
|||||||
# inphotos[0].rsplit(r'/', 1)
|
# inphotos[0].rsplit(r'/', 1)
|
||||||
return fulltag
|
return fulltag
|
||||||
|
|
||||||
#########################################################################################################################################################
|
##################################################################################################
|
||||||
|
|
||||||
def process_reviews(outputs):
|
def process_reviews(outputs):
|
||||||
# Process
|
# Process
|
||||||
@@ -1145,7 +1123,7 @@ def process_reviews(outputs):
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
#########################################################################################################################################################
|
##################################################################################################
|
||||||
|
|
||||||
# def junction(name,namedict, outputs, writtento, processrow ):
|
# def junction(name,namedict, outputs, writtento, processrow ):
|
||||||
# if name == "xtwitter":
|
# if name == "xtwitter":
|
||||||
@@ -1191,7 +1169,7 @@ def socials(name, namedict,outputs, writtento, processrow,funct):# namecount, na
|
|||||||
print (' '+name+': Skipping posting for ',processrow[1].value,' previously written')
|
print (' '+name+': Skipping posting for ',processrow[1].value,' previously written')
|
||||||
return namedict
|
return namedict
|
||||||
|
|
||||||
#########################################################################################################################################################
|
##################################################################################################
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
print('starting ...')
|
print('starting ...')
|
||||||
@@ -1201,5 +1179,5 @@ if __name__ == "__main__":
|
|||||||
process_reviews(outputs)
|
process_reviews(outputs)
|
||||||
print('Done!')
|
print('Done!')
|
||||||
|
|
||||||
#########################################################################################################################################################
|
##################################################################################################
|
||||||
|
|
||||||
Reference in New Issue
Block a user