From e6aa6b633962fa1bc4c4a8e931f16e91595a0bfe Mon Sep 17 00:00:00 2001 From: Joe Steele Date: Fri, 13 Sep 2024 18:06:43 -0400 Subject: [PATCH 1/2] fixed posting to wordpress and addiition to database --- social.py | 69 ++++++++++++++++++++++++++++++------------------------- 1 file changed, 38 insertions(+), 31 deletions(-) diff --git a/social.py b/social.py index f18aed4..0d33c44 100644 --- a/social.py +++ b/social.py @@ -521,8 +521,8 @@ def get_google_data(driver, local_outputs): score = "Unknown" print ('Error: ',error) # Grab more info from google maps entry on this particular review - if len(local_outputs['postssession'].query(Posts).filter(Posts.name == name,Posts.google\ - is not True).all()) == 0 or env.forcegoogleupdate or env.block_google_maps is not\ + if (len(local_outputs['postssession'].query(Posts).filter(Posts.name == name,Posts.google\ + is not True).all()) == 0 or env.forcegoogleupdate) and env.block_google_maps is not\ True: gmaps = googlemaps.Client(env.googleapipass) place_ids = gmaps.find_place(name+address, input_type = 'textquery', fields='') @@ -547,9 +547,13 @@ def get_google_data(driver, local_outputs): except KeyError as error: print('Error writing business details from google maps : ',error) else: - print (' Post was already in database, skipping update unless you activate override') if env.forcegoogleupdate: database_update_row(name,"google",True,"forceall",local_outputs) + if len(local_outputs['postssession'].query(Posts).filter(Posts.name == name,Posts.google\ + is not True).all()) > 0: + print (' Post was already in database, skipping update unless you activate override') + # else: + # database_update_row(name,"google",True,"forceall",local_outputs) pics= [] pics2 = [] more_specific_pics = [] @@ -766,9 +770,9 @@ def write_to_database(data, local_outputs): print("Encode Object into JSON formatted Data using jsonpickle") jsonposts = jsonpickle.encode(local_outputs['posts'], unpicklable=False) for processrow in data: - getdata = local_outputs['postssession'].query(Posts).filter(Posts.name == processrow[0]) +# getdata = local_outputs['postssession'].query(Posts).filter(Posts.name == processrow[0]) #if processrow[0] in local_outputs['posts']: - if getdata.count() >0: +# if getdata.count() >0: # if len(result) > 0: # print (' Row ',processrow[0],' already in database') # d2_row = Posts(name=processrow[0] ,comment=processrow[1],rating=processrow[2]\ @@ -797,33 +801,35 @@ def write_to_database(data, local_outputs): # print (' Row ',processrow[0],' added to Database') # # Append the above Python dictionary object as a row to the existing pandas DataFrame # # Using the DataFrame.append() function - try: + try: - getdata2 = local_outputs['postssession'].query(Posts).filter(Posts.name == processrow[0]) - if getdata2.count() >0: - print (' Row ',processrow[0],' already in Database') - if env.forcegoogleupdate: - print (' Row ',processrow[0],' updated in database due to forcegoogleupdate') - d2_dict = {'name':processrow[0] ,'comment':processrow[1],'rating':processrow[2]\ - ,'picsURL':str(processrow[3]),'picsLocalpath':str(processrow[4]),\ - 'source':processrow[5],'date':processrow[6],'address':processrow[7],\ - 'dictPostComplete':str(processrow[8])} - local_outputs['postssession'].query(Posts).filter(Posts.name == processrow[0]).update\ - (d2_dict) - local_outputs['postssession'].commit() - else: - d2_row = Posts(name=processrow[0] ,comment=processrow[1],rating=processrow[2]\ - ,picsURL=processrow[3],picsLocalpath=processrow[4],\ - source=processrow[5],date=processrow[6],address=processrow[7],\ - dictPostComplete=processrow[8]) - local_outputs['postssession'].add(d2_row) + getdata2 = local_outputs['postssession'].query(Posts).filter(Posts.name == processrow[0]) + if getdata2.count() >0: + print (' Row ',processrow[0],' already in Database') + if env.forcegoogleupdate: + print (' Row ',processrow[0],' updated in database due to forcegoogleupdate') + d2_dict = {'name':processrow[0] ,'comment':processrow[1],'rating':processrow[2]\ + ,'picsURL':str(processrow[3]),'picsLocalpath':str(processrow[4]),\ + 'source':processrow[5],'date':processrow[6],'address':processrow[7],\ + 'dictPostComplete':str(processrow[8])} + print (' Row ',processrow[0],' updated in Database') + local_outputs['postssession'].query(Posts).filter(Posts.name == processrow[0]).update\ + (d2_dict) local_outputs['postssession'].commit() - print (' Row ',processrow[0],' added to Database') - except AttributeError as error: - print(' Not able to write to post data table: ' , type(error)) - local_outputs['postssession'].rollback() - raise - processrow = [] + else: + d2_row = Posts(name=processrow[0] ,comment=processrow[1],rating=processrow[2]\ + ,picsURL=str(processrow[3]),picsLocalpath=str(processrow[4]),\ + source=processrow[5],date=processrow[6],address=processrow[7],\ + dictPostComplete=str(processrow[8])) + print (' Row ',processrow[0],' added to Database') + local_outputs['postssession'].add(d2_row) + local_outputs['postssession'].commit() + print (' Row ',processrow[0],' added to Database') + except AttributeError as error: + print(' Not able to write to post data table: ' , type(error)) + local_outputs['postssession'].rollback() + raise + processrow = [] df.to_excel(env.xls) return data @@ -2318,7 +2324,7 @@ def process_reviews2(outputs): writtento["threads"]==0 ) and (check_is_port_open(env.wpAPI)) and (env.web \ or env.instagram or env.yelp or env.xtwitter or env.tiktok or env.facebook or \ env.threads or env.google)and (processrow.comment is not None) : - if env.web and processrow.web is False or env.force_web_create is True: + if (env.web and processrow.web is False) or env.force_web_create is True: #if writtento["web"] == 0 : try: #date_string = date @@ -2336,6 +2342,7 @@ def process_reviews2(outputs): print ('Could not check to see post already exists',error) webcount=process_socials("web",processrow,outputs['web'],"post_to_wordpress",\ webcount, outputs) + webcount = 0 # force all web content get created asap if env.instagram and processrow.instagram is False: instagramcount = process_socials("instagram",processrow,outputs['web'],\ "post_to_instagram2",instagramcount, outputs) From 3e400170b0b98bf52e9ea9f820987028d15bb00e Mon Sep 17 00:00:00 2001 From: Joe Steele Date: Sun, 3 Nov 2024 12:32:21 -0500 Subject: [PATCH 2/2] linting --- social.py | 104 ++++++++++++++++++++++++++++-------------------------- 1 file changed, 54 insertions(+), 50 deletions(-) diff --git a/social.py b/social.py index 0d33c44..37dd5d3 100644 --- a/social.py +++ b/social.py @@ -175,12 +175,12 @@ def get_auth_connect(): c_column = sheet.max_column #out_data=[] #import inspect - local_dict=Posts + #local_dict=Posts column_list = [] my_list = [] my_list_data = [] - my_post = Posts - my_dict = {} + #my_post = Posts + #my_dict = {} for x in inspect.getmembers(Posts): if not (x[0].startswith('_') or 'metadata' in x[0]): column_list.append(x[0]) @@ -296,9 +296,9 @@ def get_hastags(address, name, hashtype): else: defaulttags = "\n\n\n#"+name_no_spaces+\ " #foodie #music #food #travel #drinks #instagood #feedme #joeeatswhat @timberjoe" - citytag = "#"+city - statetag = "#"+state - ziptag = "#"+zip_code + citytag = f"#{city}" + statetag = f"#{state}" + ziptag = f"#{zip_code}" if statetag in ['FL', 'fl','Fl']: statetag += '#Florida' if statetag == 'OR': @@ -549,10 +549,10 @@ def get_google_data(driver, local_outputs): else: if env.forcegoogleupdate: database_update_row(name,"google",True,"forceall",local_outputs) - if len(local_outputs['postssession'].query(Posts).filter(Posts.name == name,Posts.google\ - is not True).all()) > 0: - print (' Post was already in database, skipping update unless you activate override') - # else: + if len(local_outputs['postssession'].query(Posts).filter(Posts.name == name,\ + Posts.google is not True).all()) > 0: + print (' Post was already in database, skipping update unless overriden') + # else: # database_update_row(name,"google",True,"forceall",local_outputs) pics= [] pics2 = [] @@ -566,8 +566,8 @@ def get_google_data(driver, local_outputs): # Walk through all the pictures and videos for a given review lmpics = '' for lmpics in more_specific_pics: - # Grab URL from style definiton (long multivalue string), and remove the -p-k so that - # it is full size + # Grab URL from style definiton (long multivalue string), and remove the -p-k + # so that it is full size urlmedia = re.sub(r'=\S*-p-k-no', '=-no', (re.findall(r"['\"](.*?)['\"]", lmpics.get_attribute("style")))[0]) print (' Pic URL : ',urlmedia) @@ -584,13 +584,14 @@ def get_google_data(driver, local_outputs): #print (' Visited: ',visitdate) # Check to see if it has a sub div, which represents the label with the video length # displayed, this will be done - # because videos are represented by pictures in the main dialogue, so we need to click - # through and grab the video URL + # because videos are represented by pictures in the main dialogue, so we need + # to click through and grab the video URL if lmpics.find_elements(By.CSS_SELECTOR,'div.fontLabelMedium.e5A3N') : ext='.mp4' lmpics.click() time.sleep(2) - # After we click the right side is rendered in an inframe, Store iframe web element + # After we click the right side is rendered in an iframe Store iframe + # web element iframe = driver.find_element(By.TAG_NAME, "iframe") # switch to selected iframe driver.switch_to.frame(iframe) @@ -614,8 +615,17 @@ def get_google_data(driver, local_outputs): pics_local_path = "./Output/Pics/"+cleanname+"/"+visitdate+'/'+filename pics2.append(pics_local_path) if env.forcegoogleupdate: - database_update_row(name,"review_id",current_url,"forceall",local_outputs)# Add the correct extension to the file name - if len(pics2) > 0 and not os.path.isfile("./Output/Pics/"+cleanname+"/"+visitdate+'/'+cleanname+'-montage.mp4'): + database_update_row(name,"review_id",current_url,"forceall",local_outputs) + # Add the correct extension to the file name + if pics2 and not os.path.isfile( + "./Output/Pics/" + + cleanname + + "/" + + visitdate + + '/' + + cleanname + + '-montage.mp4' + ): make_montage_video_from_google(pics2,cleanname) for loop in pics2: if "montage.mp4" in loop: @@ -803,7 +813,8 @@ def write_to_database(data, local_outputs): # # Using the DataFrame.append() function try: - getdata2 = local_outputs['postssession'].query(Posts).filter(Posts.name == processrow[0]) + getdata2 = local_outputs['postssession'].query(Posts).filter(Posts.name \ + == processrow[0]) if getdata2.count() >0: print (' Row ',processrow[0],' already in Database') if env.forcegoogleupdate: @@ -813,8 +824,8 @@ def write_to_database(data, local_outputs): 'source':processrow[5],'date':processrow[6],'address':processrow[7],\ 'dictPostComplete':str(processrow[8])} print (' Row ',processrow[0],' updated in Database') - local_outputs['postssession'].query(Posts).filter(Posts.name == processrow[0]).update\ - (d2_dict) + local_outputs['postssession'].query(Posts).filter(Posts.name == processrow[0]).\ + update(d2_dict) local_outputs['postssession'].commit() else: d2_row = Posts(name=processrow[0] ,comment=processrow[1],rating=processrow[2]\ @@ -1104,21 +1115,24 @@ def post_to_x2(title, content, headers,date, rating, address, picslist,local_out imgs_pic.append(img.strip()) if imgs_vid: try: - video_path = imgs_vid[0] # Message to post along with the video attrib_list = local_outputs['postssession'].query(Posts).filter(Posts.name == title)\ .all() - business_url = attrib_list[0].businessurl - wpurl = attrib_list[0].wpurl - if wpurl: # Don't post of website URL does not exist yet - if business_url: #Sometimes Business URL does not exist, so account for it - status_message=str(title)+': My Review - '+wpurl+'\n Business website: '+\ - business_url + '\n' + if wpurl := attrib_list[0].wpurl: + if business_url := attrib_list[0].businessurl: + status_message = ( + f'{str(title)}: My Review - ' + + wpurl + + '\n Business website: ' + + business_url + + '\n' + ) else: - status_message = str(title) + ': My Review - '+ wpurl + '\n' + status_message = f'{str(title)}: My Review - ' + wpurl + '\n' status_message2 = status_message +' '+str(get_hastags(address, title, 'short'))\ +' ' status_message_short = status_message2[:279] + video_path = imgs_vid[0] # Upload video media = client_v1.media_upload(filename=video_path) # Post tweet with video @@ -1152,9 +1166,7 @@ def post_to_threads(title, content,headers, date, rating, address, picslist, loc wpurl = attrib_list[0].wpurl auth_token = env.threads_access_token group_id = env.threads_page_id - imgs_id = [] imgs_vid = [] - imgs_pic = [] if wpurl and (attrib_list[0].picsLocalpath != '[]'): if business_url: status_message = ( @@ -1162,12 +1174,14 @@ def post_to_threads(title, content,headers, date, rating, address, picslist, loc + ' \n\n') + content else: status_message = f'{str(title)}: My Review - ' + wpurl + ' \n\n' + content + imgs_pic = [] for img in img_list: if 'montage.mp4' in img: imgs_vid.append(img.strip()) else: imgs_pic.append(img.strip()) if imgs_vid: + imgs_id = [] try: post_id = post_threads_video(group_id, imgs_vid,auth_token,title, status_message, date, rating, address) @@ -1204,9 +1218,7 @@ def post_facebook3(title, content,headers, date, rating, address, picslist, loca pics = ((picslist[1:-1]).replace("'","")).split(",") group_id = env.facebookpageID auth_token = env.facebookpass - imgs_id = [] imgs_vid = [] - imgs_pic = [] img_list = pics attrib_list = local_outputs['postssession'].query(Posts).filter(Posts.name == title).all() business_url = attrib_list[0].businessurl @@ -1218,12 +1230,14 @@ def post_facebook3(title, content,headers, date, rating, address, picslist, loca + ' \n\n') + content else: status_message = f'{str(title)}: My Review - ' + wpurl + ' \n\n' + content + imgs_pic = [] for img in img_list: if 'montage.mp4' in img: imgs_vid.append(img.strip()) else: imgs_pic.append(img.strip()) if imgs_vid: + imgs_id = [] try: post_id = post_facebook_video(group_id, imgs_vid,auth_token,title, status_message, date, rating, address) @@ -1331,23 +1345,13 @@ def post_to_tiktok(title, content, headers2, date, rating, address, picslist, lo # If you want to schedule your video, replace 'schedule_timestamp' with the Unix timestamp. # Leave it as None if you want to upload immediately. - schedule_time = None # or Unix timestamp (e.g., 1672592400) + #schedule_time = None # or Unix timestamp (e.g., 1672592400) pics = ((picslist[1:-1]).replace("'","").replace(" ","")).split(",") - if any(element.endswith("montage.mp4") for element in pics): - for pic in pics: - if 'montage.mp4' in pic: - file_path = pic - else: + if not any(element.endswith("montage.mp4") for element in pics): return False - #content = content + get_hastags(address, title) - #pics = ((picslist[1:-1].replace(",","")).replace("'","")).split(" ") -# video, outputmontage = make_montage_video_from_google(pics) - # try: - # instasession.video_upload(outputmontage, data) - # except AttributeError as error: - # print(" An error occurred uploading video to Instagram:", type(error).__name__) - # return False - # return True + for pic in pics: + if 'montage.mp4' in pic: + file_path = pic # """ # client_key string The unique identification key # provisioned to the partner. @@ -1468,17 +1472,17 @@ def post_to_tiktok(title, content, headers2, date, rating, address, picslist, lo 'video': file_path, 'desc': content+tags } - + post_response = requests.post('https://api.tiktok.com/open-api/v2/aweme/post/',headers=post_json_header,\ data=post_json_body,timeout=env.request_timeout) - + # headers = ['Content-Type: application/x-www-form-urlencoded','Cache-Control: no-cache'] # data = ['client_key='+env.tiktok_client_key,'client_secret='+env.tiktok_client_secret,\ # 'code='+env.tiktok_app_id,'grant_type=authorization_code','redirect_uri=http://www.joeeatswhat.com'] # allheaders = headers + data - + # try: # data2 = {'source_info':{'source':"FILE_UPLOAD",file_path}} # response = requests.post('https://open.tiktokapis.com/v2/oauth/token/,headers:',allheaders,\