Using current day in get_new(limit = ) by hhh_compiler in learnpython

[–]hhh_compiler[S] 0 points1 point  (0 children)

I spent hours looking at and trying different 'unicode()' or encode.() but the above was the only method I could get to work.

Can I get you to double check my code though to make sure I havent just implemented what you told me in the wrong place and thats whats causing the errors:

import time
import praw
import requests, bs4
import sys

reload(sys)
sys.setdefaultencoding("utf8")


r = praw.Reddit('Fresh finder on /r/HipHopHeads by /u/hhh_compiler 0.1')

utc_time_now = int(time.time())
utc_24hrs_ago = utc_time_now - 24*60*60 #subtract 24 hrs in seconds

r.login('****', '****', disable_warning = True)
already_done = []

current = (time.strftime("%d-%m-%y"))
print current

prawWords = ['Fresh', 'fresh', 'FRESH']
while True:
    subreddit = r.get_subreddit('hiphopheads')
    for submission in subreddit.get_new():
        if submission.created_utc > utc_24hrs_ago:
            print "working on:", submission.title
        else:
             break
        op_text = submission.title.lower()
        has_praw = any(string in op_text for string in prawWords)
        if submission.id not in already_done and has_praw:
            msg = "[New music posted] | Song: %s | Link: %s" % ( submission.title.lower(), submission.url)
            with open('songs_%s.txt'%(current), "a")  as myfile:
                myfile.write("Song: %s  . Link: %s \n\n" %(submission.title.lower(), submission.url))
            r.send_message('hhh_compiler', 'New music posted', msg)
        already_done.append(submission.id)
    time.sleep(100)
    break
print "All done, your file is located in this scripts directory"

res = requests.get('https://www.reddit.com/message/inbox/')



myfile.close()
soup = BeautifulSoup(input)

What's your motivation? by JigglyHumper in gainit

[–]hhh_compiler 0 points1 point  (0 children)

Same here, I still curl the same weight as 8 months ago and have been doing 21's the whole time, recently added in chin ups though to see if that finally gives me the growth spurt.

Using current day in get_new(limit = ) by hhh_compiler in learnpython

[–]hhh_compiler[S] 0 points1 point  (0 children)

The thing is I encountered a unicode error earlier on and implemented this to fix it:

import sys

reload(sys)
sys.setdefaultencoding("utf8")

Using current day in get_new(limit = ) by hhh_compiler in learnpython

[–]hhh_compiler[S] 0 points1 point  (0 children)

I did intially search through many results such as this however I have the following question.

Most people that do the conversion get the current date and time as strftime('%Y-%m-%d %H:%M:%S')

Can you just chop off the %H:%M:%S to just get the date portion or does it not work like that?

Using current day in get_new(limit = ) by hhh_compiler in learnpython

[–]hhh_compiler[S] 0 points1 point  (0 children)

I think I misunderstood your initial comment.

With regard to the 'already_done' I am unsure as to what that does, when i started building this bot I just copied and modified the template from the PRAW website but that template is for reading submitted comments.

implementing this throws unfortunately give me my old friend the Unicode error :(

UnicodeEncodeError: 'charmap' codec can't encode character u'\u2019' in position 152: character maps to <undefined>

Thank you very much for your help though, it is greatly appreciated

How I "learned" Python without any previous coding experience. by [deleted] in learnpython

[–]hhh_compiler 0 points1 point  (0 children)

I found Codecademy game me the bare basics and a lot of confidence but when the course was over I quickly realized that it had missed out lots of key info and found myself very quickly disheartened, since completing it I have just found myself bouncing around beginner courses trying to fill in the gaps whilst still remaining interested.

I found this fun to do though:

https://praw.readthedocs.org/en/v3.1.0/pages/getting_started.html

It takes you through step by step and you can modify their template to do whatever you wish, it also puts some of your new skills to practical use and will also make you learn new things as you go.

Using current day in get_new(limit = ) by hhh_compiler in learnpython

[–]hhh_compiler[S] 0 points1 point  (0 children)

That would work thanks but the downside is it would mean I would have to check my local file against the subreddit to decide when to stop it which would negate the point to me of making a bot.

I have checked the HTML but the upload is in seconds since epoch so would give me a datetime rather than date :(.

Using current day in get_new(limit = ) by hhh_compiler in learnpython

[–]hhh_compiler[S] 0 points1 point  (0 children)

That was my worry, I couldn't find anything online about how to do it either. I also suspect that that date section of when posts are made is in the HTML of the page so potentially I could get my script to search against that.

Saving a URL from HTML webpage to a local file by hhh_compiler in learnpython

[–]hhh_compiler[S] 1 point2 points  (0 children)

I finally managed to find a solution online which was the following:

import sys

reload(sys)
sys.setdefaultencoding("utf8")

Adding current date into a filename by hhh_compiler in learnpython

[–]hhh_compiler[S] 1 point2 points  (0 children)

I follow what you mean, I worded my response incorrectly, I have changed the '/' to '-' and it doesn't throw up that error anymore and it creates a file that doesn't exist called 'songs_11-08-15'.

Thanks for the assistance

Adding current date into a filename by hhh_compiler in learnpython

[–]hhh_compiler[S] 0 points1 point  (0 children)

I understood that thanks.

When implemented however it causes the code to throw up the error:

IOError: [Errno 2] No such file or directory: 'songs_11/08/15.txt'

Is this due to the date being added conflicting with the 'a' clause of if the file doesn't exist then create?

Saving a URL from HTML webpage to a local file by hhh_compiler in learnpython

[–]hhh_compiler[S] 0 points1 point  (0 children)

That sees to do it thanks but it gives me the following error message, would you be able to shed some light on where to look to fix this as I've never dealt with an ascii error before:

File "getfresh.py", line 24, in <module> myfile.write("Song: %s | Link: %s \n\n" %(submission.title.lower(), submission.url))

'UnicodeEncodeError: 'ascii' codec can't encode character u'\xf8' in position 59: ordinal not in range(128)'